Vim notes - Navigation

4 minute read

There are lots of different ways to achieve one same goal in Vim. If you are pressing same key multiple times to achieve one goal, it is very likely that you are doing it wrong way. In this post I would like to collect various vim tips and goal pattern to use them.

This post is organized from a user’s perspective. To understand the mechanism of Vim , one might check Vim Galore

Navigation

Basic navigation

Using h-j-k-l to navigate the cursor is the first vim lesson for most people. h-j-k-l is still the fundamental way to navigate if your destination is close to your cursor or you need time to think about next operation

“h-j-k-l” is also useful if your context couldn’t be parsed by Vim to your wish, as “w” “b” “e” would give you wrong navigation in such circumstance.

You can use f[c] to move the next character c.

  • To move the next character t
    ft
    

This operation might be useful in certain circumstance especially if you are handling long word or typos. However counting characters between words is too difficult for me (And there is little chance the character not appear in between) so I would rather use search navigation than f[c] .

Word/Token navigation

If you are working with tokens/words separated with space. Navigating between tokens would be more efficient.

  • use w to move to the begin of next word
  • use b to move to the begin of the word
  • use e to move to the end of the word

From my point of view, “w” and “b” are the most used operations as they would move to the begin of the word therefore it’s easy to change or delete the word with “cw” or “dw”. (Of course you can still use “caw” or “daw” if you are in one word although motion is nolonger usable) “e” is usually followed by ‘a’ to append to the word. These word navigations could be combined with numbers to repeat several times

  • To move three words forward
    3w
    
  • To move two words backward
    2b
    

Attention: Definition of word based on plain english, not nessarily compatible with programming language

Still, I am not good at counting words quickly so I would prefer search navigation if the distance is large

the navigation operation can also be used as range identifier for edit operation

Additionally, you can use “%” to move to the matched parentheses, this could be more helpful if you are using some kind of auto-bracket functionalities.

Sometimes “%” might also help you find some bugs produced by wrongly matched parentheses.

Line navigation

  • use 0 to get to the begin of the line
  • use $ to get to the end of the line
  • use ^ to get first non-space token of the line
  • To move to the fourth word of the line one can use
    ^3w
    
  • To insert in front of the last word on the line (if the last word is not one character)
    $bi
    
  • use :[n] to jump to line n, you can also use arithmetic expression
    :15
    :28-13
    :12+3
    
  • use [n]j/[n]k to jump n lines below/above
    13j
    2k
    

line navigations are less used as range identifier since most edit operations have capitalized versions to cover the whole line

You can use “{“ or “}” to move to the prev or next paragraph, which is essentially the prev or next empty line.

This can be extremely useful in code navigation as long as you keep a good coding style. If you leave an empty line between every code block( function definition, class definition, Loop block, etc) you could easily navigate between theme by “{“ and “}”

This navigation can work with edit operation too

  • To delete this and next code block (delete single code block you can use “dap”)
    {d2}
    

Much faster than checking lines, isn’t it?

Search navigation

  • use /[word] to search for the word forward
  • use ?[word] to search for the word backward

press Enter would bring your cursor to the first match, then you can use ‘n’ to move to the next match or use ‘N’ to move the prev match

Attention: using ? would make “n” to move backward and “N” to move forward .

  • To find the first strangeword forward
    /strangeword <cr>
    
  • To find the second strangeword backward
    ?strangeword <cr> 2n
    

You can exchange “?” and “/” as well as exchanging “n” and “N”

To search for the current word you are in, you can use “*” and “#”

Other navigation

  • You can use “gg” to move to the begin of the document
  • You can use “G” to move to the end of the document
  • You can use “Ctrl-f” or “Ctrl-b” to move one page below or above
  • You can use “gD” to move to the definition of the word, which is basically the first appearance of the word.

Mark navigation

  • You can use “m[c]” to mark your current cursor position as mark c, and jump back to c use “`c”
    • You can also use “‘c” to jump back to the line of the mark
  • only a-z and A-Z is avaible for user
  • Uppercase mark is valid between files, lowercase mark is not.

Marks are not used to view two parts of one file simultaneously, use Split instead There are some vim-defined mark that are useful.

  • ”.” marks where the last change is made
  • ”^” marks where the last insertion is made
  • Check Vim Galore for more information

Updated: