How to navigate in Vim

Vim is always my favorite text editor. I've been using Vim for over 2 years and still learning it. If you want to use Vim, the first thing you will need to know is how to navigate in Vim, since it is text-based. In this article, I will show you some basic navigation shortcut keys to use in Vim.

How to navigate in Vim

1. Basic navigation

The most simple way to navigate around in vim is by using the arrow keys. Another method is use the four key:
- k: move up
- j: move down
- h: to the left
- l: to the right

In case you dont want to press the same key many times, a shortcut method is to use these key with a number. For example, if you want to move the cursor to the right skipping 12 letters, you can just hit "12l" or hit "12" then press the right arrow key.

2. Navigate within a line

If you want to navigate to different position within a line, you have 4 shortcut keys:
- 0: move to the beginning of the line
- ^: to the first character of the line ( not a space)
- $: to the end of the line (space included)
- g_ : to the last character of the line ( there is a hyphen here)

3. Screen navigation

Once you open a text file in Vim, you can navigate in relation to text shown in just the screen, not the entire file. Here are the shortcut keys:

- H: to the first line in the screen
- M: to the middle line in the screen
- L: to the last line in the 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

4. Navigate through paragraphs

You can use the 2 following key to navigate through paragraphs in Vim:
- {: to the beginning of a paragraph, you can press it again to move to the next paragraph
- }: to the end of a paragraph, and similarly you can press it many times to move to the next paragraphs

5. Open a file from a specific position

You can open a text file from a specific position with vim. For example, if you want to open a file from line #32, you can use the following command to open it:
 vim +32 / path/to/the/file  

You can also open a file from a specific word, for example, to open a file from a certain word, say "Newyork", you can just use the following command:
 vim +/Newyork /path/to/the/file  

And vim will open the file with the cursor at the word "Newyork".

6. Some other navigation shortcuts

- N%: Go to the Nth percentage line of the file.
- NG: Go to the Nth line of the file.
- G: to the end of the file.
- g: to the beginning of the file.
-  `": to where you were in normal mode last time
- `^: to where you were in insert mode last time
linux tips and tricks