Shawn Lin

Vim Tricks 2: More editing tricks

2018-08-19

Now that you’ve learned the basics, you should start practicing these skills by using Vim as your primary text editor. In this post, we are going to go through more tricks with regards to moving around.

Screen tricks

What you can see in your editor is called a “screen”. Obviously, a long file can have contents that spans several “screen”s and you’ll need to be able to move around them. Some useful commands are:

  • ctrl
    ctrl F Foward one screen
    ctrl D Move Down half screen
    ctrl B Backward one screen
    ctrl U Move Up half screen
    ctrl E Move down one line
    ctrl Y Move up one line
  • Keep cursor in position

    zenter↵ Current line becomes the top line
    z. Current line to the center
    z- Current line to the bottom

Cursor tricks

Even on the same screen, moving cursor effeciently will be useful. We’ve already covered tricks like + and -. Here are some more:

By screen

H Move cursor to Home
M Move cursor to Middle
L Move cursor to bottom (I use High, Middle and Low to remember these three commands)
enter↵ or + Move cursor to first character of next line
- Move cursor to first charcter of previous line
^ Move curosr to first nonblank character of current line
1-9| Move to column number of the current line
G Go to the end of file
`` Move cursor to previous position

By content

( or ) beginning of current/next sentence (.?! marks the end of an sentence)
{ or } beginning of current/next paragraph (empty line)
[[ or ]] beginning of current/next section

Searching

Searching a document is perhaps the most common task one performs day to day. Search in Vim supports regex patterns, but we are only goint to cover the bascis here.

/ or ? initialize searchs. / does the search forwards, and ? does it backwards. For example, if I want to search the next occurance of “to”, I would type “/ to” and press enter. To find the previous one, I need to type in “? to”.
N or n handles multiple occurances. It repeats the last search in opposite/same direction.

Reference

  1. Arnold Robbins, Linda Lamb, and Elbert Hannah. 2008. Learning the Vi and Vim Editors (Seventh ed.). O’Reilly.
Tags: vim