Photo courtesy of Håkan Dahlström
This article is written by SathiyaMoorthy.
This article is part of the ongoing Vi / Vim Tips and Tricks series. Navigation is a vital part of text editing. To be very productive, you should be aware of all possible navigation shortcuts in your editor. In this article, let us review the following 8 Vi / Vim navigation options.
- Line navigation
- Screen navigation
- Word navigation
- Special navigation
- Paragraph navigation
- Search navigation
- Code navigation
- Navigation from command line
1. Vim Line Navigation
Following are the four navigation that can be done line by line.
- k – navigate upwards
- j – navigate downwards
- l – navigate right side
- h – navigate left side
By using the repeat factor in VIM we can do this operation for N times. For example, when you want to
go down by 10 lines, then type “10j”.
Within a line if you want to navigate to different position, you have 4 other options.
- 0 – go to the starting of the current line.
- ^ – go to the first non blank character of the line.
- $ – go to the end of the current line.
- g_ – go to the last non blank character of the line.
2. Vim Screen Navigation
Following are the three navigation which can be done in relation to text shown in the screen.
- H – Go to the first line of current screen.
- M – Go to the middle line of current screen.
- L – Go to the last line of current screen.
- ctrl+f – Jump forward one full screen.
- ctrl+b – Jump backwards one full screen
- ctrl+d – Jump forward (down) a half screen
- ctrl+u – Jump back (up) one half screen
3. Vim Special Navigation
You may want to do some special navigation inside a file, which are:
- N% – Go to the Nth percentage line of the file.
- NG – Go to the Nth line of the file.
- G – Go to the end of the file.
- `” – Go to the position where you were in NORMAL MODE while last closing the file.
- `^ – Go to the position where you were in INSERT MODE while last closing the file.
- g – Go to the beginning of the file.
4. Vim Word Navigation
You may want to do several navigation in relation to the words, such as:
- e – go to the end of the current word.
- E – go to the end of the current WORD.
- b – go to the previous (before) word.
- B – go to the previous (before) WORD.
- w – go to the next word.
- W – go to the next WORD.
WORD – WORD consists of a sequence of non-blank characters, separated with white space.
word – word consists of a sequence of letters, digits and underscores.
Example to show the difference between WORD and word
- 192.168.1.1 – single WORD
- 192.168.1.1 – seven words.
5. Vim Paragraph Navigation
- { – Go to the beginning of the current paragraph. By pressing { again and again move to the previous paragraph beginnings.
- } – Go to the end of the current paragraph. By pressing } again and again move to the next paragraph end, and again.
6. Vim Search Navigation
- /i – Search for a pattern which will you take you to the next occurrence of it.
- ?i – Search for a pattern which will you take you to the previous occurrence of it.
- * – Go to the next occurrence of the current word under the cursor.
- # – Go to the previous occurrence of the current word under the cursor.
7. Vim Code Navigation
% – Go to the matching braces, or parenthesis inside code.
8. Vim Navigation from Command Line
Vim +N filename: Go to the Nth line of the file after opening it.
vim +10 /etc/passwd
Vim +/pattern filename: Go to the particular pattern’s line inside the file, first occurrence from first. In the following example, it will open the README file and jump to the first occurrence of the word “install”.
vim +/install README
Vim +?patten filename: Go to the particular pattern’s line inside the file, first occurrence from last. In the following example, it will open the README file and jump to the last occurrence of the word “bug”.
vim +?bug README
Recommended Reading
Vim 101 Hacks, by Ramesh Natarajan. I’m a command-line junkie. So, naturally I’m a huge fan of Vi and Vim editors. Several years back, when I wrote lot of C code on Linux, I used to read all available Vim editor tips and tricks. Based on my Vim editor experience, I’ve written Vim 101 Hacks eBook that contains 101 practical examples on various advanced Vim features that will make you fast and productive in the Vim editor. Even if you’ve been using Vi and Vim Editors for several years and have not read this book, please do yourself a favor and read this book. You’ll be amazed with the capabilities of Vim editor.
Comments on this entry are closed.
Navigation using marks is very important without it vim is as good or bad as Emacs. A mark can be put using the letter m followed by any other latter. A small letter used for a mark is scoped within the file, while a capital letter used for a mark is scoped across files. To jump to a mark use `a or ‘a, I prefer the `a since it takes me to the exact position where I left the mark. To look at where the marks are use :marks in the ed mode.
overall steps for mark navigation are;
Put mark on a line using ma go to some other place edit and then jump back using `a
Say I am in some file I put a mark using mA go to some other file in same buffer and then jump back using `A, this is really cool I get to jump back to previous file with just 2 keystrokes.
Finally, ctags are also very important for code navigation without ctags vim is as bad as Eclipse, use Ctrl-] to jump to a function definition and Ctrl-t to comeback from the tag stack.
Cheers,
sorry forgot Ctrl-O and Ctrl-I, try it
Another line navigation command:
N| – go to column N
2. Vim Screen Navigation
. . .
* ctrl+f – Jump forward one full screen.
* ctrl+b – Jump backwards one full screen
* ctrl+d – Jump forward (down) a half screen
* ctrl+u – Jump back (up) one half screen
3. Vim Special Navigation
. . .
* g – Go to the beginning of the file.
Misc
* ctrl+g – Report cursor position within the file in the status line at the bottom of the screen.
Okay.. I’ve been using vim for a while and I thought I knew most of the goofy little shortcuts.
very cool. Screen shortcuts I wasn’t aware of and some of the special navigation shortcuts are new. Thanks for the post.
@vimer
Thanks for your comment, and also for the short introduction about the MARKS, for which we have a detailed article at How To Add Bookmarks Inside Vi and Vim Editor
@Ken,
Thanks a lot for your input. I’ve added the shortcuts that you’ve suggested to the article.
Under code navigation, you mention the good old % key for matching braces and parentheses. You get a whole world of extra functionality by enabling the matchit plugin and filetype plugins. In brief:
:runtime macros/matchit.vim
:filetype plugin on
Now, open an HTML file (for example), put the cursor on a tag (inside, not on, the angle brackets) and hit % to jump to the closing tag. For details,
:help matchit-install
:help :filetype-plugin-on
Nice post it will really help beginners to have their hands on short hand..
g – Go to the beginning of the file.
shoud be gg….
Hi Ramesh,
Thanks for all your work of this site, and thanks for this topic.
BTW, “bellszhu” seems to be right,
[Go to the beginning of the file] is either:
gg
1G
1gg
but not a simple g
— Philippe,
Versailles, France
Excellent article. Well surveyed and categorized. I liked it a lot for the clear structure in learning and remembering!
g; = go the previous change you made.
g, = go to the next change you made (after going backwards, vim can’t tell the future just yet).
honestly life altering key bindings.
CTRL+O and CTRL+I are similar but they send you back and forth between jumps vs changes.
More:
:h changelist
:h jumplist