Vim 101: Quick Movement

Alex R. Young
Apr 8, 2012 · 3 min read

This is the first Vim 101 post, in which we take a look at fundamental Vim techniques. If you’re a beginner or would like to brush up on fundamental skills, then this is the series for you.

When I’m trying to teach people Vim, I find they’re often confused about how to efficiently move around within files. Using GUI editors with mouse wheels or trackpad gestures can feel fast, and GUI versions of Vim support this.

However, cursor movement in Vim is typically controlled with the keyboard. Quickly moving around can feel awkward at first, but there are many ways to move the cursor and screen that become second nature before long.

Screen Positioning

In command mode, I often find myself scrolling the screen by half increments using and . The equivalent full screen commands are and . Page up and page down may also work depending on your configuration.

Pressing and then return will move the current line to the top of the screen. A number will move that line number to the top of the screen, so pressing and then return will make the top of the screen start at line 50. Conversely, will move the current line to the centre of the screen, and will move to the bottom.

To move within a screen without scrolling it, is useful because it'll move the cursor to the top line. shifts it to the middle, and then moves it to the last line.

All of these commands are easy to remember with some simple mnemonics:

  • up
  • down
  • forward
  • back
  • Home
  • Middle
  • Last

Word Movement

To move to the end of the next word, use . The opposite is , which moves backwards a word. The command also moves forwards, but leaves the cursor at the start of the word.

Typing , , or will perform a similar operation, but won't count symbols or punctuation as separate words. When I'm programming I find myself using these versions.

Text Block Movement

To move to the beginning of the next or previous sentence, and can be used. Paragraphs can be navigated using and . The definition of a paragraph is based on nroff macros (read if you want to figure out what that means). This generally works quite well for navigating between large space-separated blocks in a programming language.

Another useful command is , which moves to the next or previous related item. Items include brackets, opening and closing c-style comments, and C preprocessor conditionals. I find myself using a lot depending on the programming language.

Line Movement

Typing would move to the 44th line in the file. goes to the last line, and goes to the first line.

The useful thing about this command is typing `` moves back to the last position. This also moves back to the last edit if an edit was made, or the position before a search was started.

Character Movement

The and commands are extremely useful for navigating within a line. Pressing will move the next occurrence of within the current line. Meanwhile, will move backwards. A numerical count argument can also be supplied, so will go to the second occurrence of after the current cursor position.

To move before the matching character, and can be used. The last can be repeated by pressing .

Conclusion

It’s hard to get used to these commands, but try making your own mnemonics to memorise them, or create your own cheat sheet. Most people get comfortable with a subset that suit their workflow.

Remember that will display detailed help for all of these commands — for example, .

usevim

A blog about Vim

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store