(Note that this was written after I spent a few months learning Vim. If you’re just stuck in Vim and want to get out, see What to do when you get stuck in a Vim editor).
I’m publishing my notes on the things that are useful in Vim but that I keep forgetting. My notes are split into four sections, so I’ll publish four posts:
- Miscellaneous Vim Stuff
- Vertical Columns in Vim (“visual blocks”)
- Navigating files, lines, blocks
- Searching in Vim
This is the third post, and it’s about navigating files, lines and blocks in Vim.
Navigating lines
- Go to end of line: $ or A (which also puts you in insert mode)
- Go to start of line: 0
- Remap Ctrl+a and Ctrl+e to take you to start / end of line while in Insert mode: https://coderwall.com/p/fd_bea/vim-jump-to-end-of-line-while-in-insert-mode
Navigating files
- Go to top of file: gg
- Go to end of file: shift + g
- Go to line number: line-number + shift + g
- Navigation within chars / lines: j = down, k = up, h = left, l = right
Navigating blocks delineated by {}, (), [], <> or “”
- The diff between % and [{ always confuses me cos it’s not explained well in Vim Adventures:
- % will take you to the matching bracket if you are already ON a bracket. It only works on {}, () and [] (not <>)
- [{ will search backwards for the enclosing { if you are already IN a {} block – so it takes you to the start of your current scope
- ]} will move forwards and take you to the end of your current scope
- ]{ is meaningless (I think)
- [( and )] will also work in the same way
- [< does not work, [“ does not work, and [[ does something totally different
- If you want to move back to blocks that enclose your current block, use numbers
- So for instance, 3[{ will take you to the beginning of this snippet of code if your cursor is in the innermost scope:
- {
- {
- {I am here}
- }
- {
- }
- In Vim Adventures type :help [{
If you want to navigate inside a block delineated by [], <> or “”
- You can use % to find the matching brace if you are on [ or ] but not < or >
- You can use visual mode to select the contents of {}, [], (), <> or “”
- Use a to select the contents AND the delineators
- Use i to select the inner contents (ie without the delineators)
- Use the OPENING delineator to indicate what your scope is
- These are all the possible commands: va{, vi{, va[, vi[, va(, vi(, va<, vi<, va”, vi”
- If you want to select blocks that enclose your current block, use numbers
- So for instance, v3a{ will select this whole snippet of code if your cursor is in the innermost scope:
- {
- {
- {I am here}
- }
- {
- }
- Once you’re in visual mode you can use commands like i, p, c, a, s
- In Vim Adventures type :help a{ or help i{
- You can use c to select the contents of {}, [], (), <> or “” and then it will put you into Insert mode to replace what was there
- Same principles as with v (see above)
- These are all the possible commands: ca{, ci{, ca[, ci[, ca(, ci(, ca<, ci<, ca”, ci”
- As with v you can use numbers to select multiple enclosing blocks (see above) but the number comes BEFORE c, like this: 3ca{
- (There are others too, like caw and ciw for words – in Vim Adventures type :help aw and :help iw)
- You can use c to select the contents of {}, [], (), <> or “” and then it will put you into Insert mode to replace what was there
One thought on “Navigating files, lines and blocks in Vim”