DEV Community

Igor Irianto
Igor Irianto

Posted on • Updated on • Originally published at irian.to

Moving around in Vim

Follow @learnvim for more Vim tips and tricks!

Being able to freely move around in a file is an indispensable vim skill.

Below is a list of the commands I use to move around in a file. I will also share some tips to move around efficiently and how you can start applying it.

Table of Contents

Setup Your Numbers

Before starting, I find it helpful to have number and relativenumber set on vim. You can do it by running :set relativenumber number or having this on .vimrc:

set relativenumber
set number
Enter fullscreen mode Exit fullscreen mode

This tells my current position and displays n lines above/ below my current position.

Character navigation

Navigating around text:

h  left
j  down
k  up
l  right
Enter fullscreen mode Exit fullscreen mode

Word navigation

w  move forward to the beginning of next word
W  move forward to the beginning of next WORD*
e  move forward one word to the end of next word
E  move forward one word to the end of next WORD
b  move backward to beginning of previous word
B  move backward to beginning of previous WORD
ge  move backward to end of previous word
gE  move backward to end of previous WORD
Enter fullscreen mode Exit fullscreen mode

* From :h WORD: A WORD consists of a sequence of non-blank characters, separated with white space. An empty line is also considered to be a WORD.

Match navigation

%  Navigate to another match, usually works for (), [], {}
Enter fullscreen mode Exit fullscreen mode

(with matchit.vim on), we can now toggle within methods.

Block navigation

{ Jump to prev paragraph
} jump to next paragraph
( Jump to prev sentence
) Jump to next sentence
Enter fullscreen mode Exit fullscreen mode

File Line navigation

gg  go to first line
G  go to last line
nG  go to line n
n%  go to n% in file
``  go to last jump position
Enter fullscreen mode Exit fullscreen mode

Btw, you can see how many lines in a file with CTRL-G.

Current line navigation

0  go to first character of current line
^  go to first nonblank char of current line
n|  go column n of current line
g_  go to last non-blank char of current line
$  go to last char of current line
Enter fullscreen mode Exit fullscreen mode

Screen navigation

H  go to top of screen
M  go to medium screen
L  go to bottom of screen
nH  go n line from top
nL  go n line from bottom
Enter fullscreen mode Exit fullscreen mode

Scrolling screen

Ctrl-e  scroll down lines
Ctrl-d  scroll down half screen
Ctrl-f  scroll down whole screen
Ctrl-y  scroll up lines
Ctrl-u  scroll up half screen
Ctrl-b  scroll up whole screen
Enter fullscreen mode Exit fullscreen mode

Function/ Module Navigation

]m  go to the start of next method
[m  go to the start of previous method
]M  go to the end of next method
[M  go to the end of previous method
]]  go to next class/ module
[[  go to previous class/module
Enter fullscreen mode Exit fullscreen mode

For more information on function/module navigation, this vid by Drew Neil is very informative!

Search navigation

/  Search forward for a match
?  Search backward for a match
n  Repeat last search (same direction as previous search)
N  Repeat last search (opposite direction as previous search)
f  Search forward for a match in the same line
F  Search backward for a match in the same line
t  Search forward for a match in the same line, stopping before match
T  Search backward for a match in the same line, stopping before match
;  Repeat last search in the same line
,  Repeat last search in the same line backwards
*  Quickly search for word under cursor forward
#  Quickly search for word under cursor backward
Enter fullscreen mode Exit fullscreen mode

Phew! Some items above I use almost every time, some I hardly ever used, but it's good to know they exist. Find one that works for you.

General tips on navigating

When moving around in Vim, it is important to see patterns inside a file. Moving in vim reminds me of painting, start with the broadest stroke you know.

  • Is the word you're targeting slightly past the halfway length of the file? Start with 50% then go down with j.
  • Is it on line 73? This is awesome, jump directly there with 73G
  • Is the text 3 paragraphs down? Do }}} instead of mashing j buttons.
  • Do you know that it is near a unique keyword const uniqueKeyword = 'UNIQUE'? Search-jump with /uniqueKeyword

If when you arrive on target line, the target word is still far near the end of the sentence, you can approach it with (w) - or if possible, look for unique letter around that target word. For example, if the sentence is:

I ate a fried fish next to a zebra today
Enter fullscreen mode Exit fullscreen mode

Assume that you are starting with your cursor on "I". You want to edit "a" before zebra. Start by "find z" (fz) because "z" is not a commonly used letter then backtrack with b. Typing fzb(3 keystrokes) is faster than going to end then backtrack $bbb (4 keystrokes) and it is faster than wwwwwww (7 keystrokes). The last thing we want to do it pressing a lot of l's. Can you think of a pattern that get you there with less keystrokes? This is what makes vim fun!

To get better at it, spend a few days playing vimgolf. Trust me, you will learn a lot.

How to apply this cheatsheet

You might be thinking: "O geez, there are so many of them! How can I possibly remember all of them?"

Here are my personal tips:

  • DO NOT try to commit all of them into memory in one sitting.
  • Learn 5-6 of them today, use them every day for a week! Don't rush - what's the hurry?
  • Learn another 5-6 next week while using the ones you learned previously.
  • Do vimgolf and see how you fare on your own, then learn from other golfers.
  • It took me almost a year to learn the above. I am still learning something new every week about Vim. Doing Vim is a long term commitment.
  • Learn to utilize help :h.

If you are brand new to Vim and feel overwhelmed where to start, here are some commands you should learn first:

Basic navigations:

h
j
k
l
Enter fullscreen mode Exit fullscreen mode

Word Navigation:

w
b
Enter fullscreen mode Exit fullscreen mode

Jump to line n

nG  // ex: 1G, G, 73G
Enter fullscreen mode Exit fullscreen mode

Searches:

/
? 
n
Enter fullscreen mode Exit fullscreen mode

These 10 should put you in a really good position to move around in a file.Then slowly add more to your arsenal.

Thanks for reading! Appreciate you making it this far. If you have more tips or questions, or found a mistakeΒ πŸ˜…, please feel free to comment below!

Cheat sheet - you can do it

Top comments (9)

Collapse
 
fidelve profile image
FidelVe

One I use quite often is:

:!<command>

This will run a shell command, show you the result and then go back to vim, is pretty useful when you need to run a shell command without opening another shell window or closing vim

Collapse
 
miniscruff profile image
miniscruff

Ohh yes and using % will insert your open buffer file.
:!black % will run python black on your open file

Collapse
 
iggredible profile image
Igor Irianto • Edited

Haha yup! I actually made a note to dig into this. This command alone unlock myriads of possibilities.

Going off topic- something I learned these past 3-4 weeks thought I would share!

  • :!! repeat the last :!command :)
  • :r prints output into vim - :r !ls for example, prints the output of ls command - I am sure will come in super handy!
Thread Thread
 
miniscruff profile image
miniscruff

Oh yea always forget !!.
I also gotta learn the other special characters I think # means something as well.

Collapse
 
miniscruff profile image
miniscruff • Edited

Two of my favorites
q/ show your history of searches
q: show your history of commands

Collapse
 
bakadesu profile image
バカです • Edited

Can you explain?

code:/ ? :q ?

What did you mean?

Collapse
 
miniscruff profile image
miniscruff

Sorry it is actually...
q/ for search history.

Press those keys to get a buffer that shows your history of commands or searches

Collapse
 
codenutt profile image
Jared

Awesome list. Thank you!

Collapse
 
iggredible profile image
Igor Irianto

Hope it helps!! 😁