Move around in a file with vim

In this article, we would cover how to move around in a file with vim. vim (a text editor) is an improved version of vi. Purpose of writing this article is to assist you in navigating inside a very large text file.

We can easily navigate inside the file which has around 3-4 pages through navigation keys on keyboard. But, when it comes to larger files with numerous pages, navigation keys wouldn’t be of much help. That’s where various key combinations which can be used with vim comes to the fore.

vim has got two modes – command and input. To move around in a file, we need to stay in the command mode. If you are in the input mode, press Esc key to get back to command mode.

We discuss important key combinations next.

Move around in a file with vim

To open a file using vim

vim <file_name>

The file opens in command mode. You would find cursor right the beginning.

Now, to reach at the end of file – use key combination – G or Shift + g

to again get back at the beginning of file – 1G or 1+Shift + g

This shows that, we can go to any line number using the above. So, let’s say we want to go the line number 200. Use key combination in that case – 200G or 200 + Shift + g

Now, consider a scenario wherein we need to move page wise forward and backward. Using line numbers or move to the beginning/end of the file key combinations wouldn’t be of any use in that case. Therefore, to move forward page-wise (or, one page with every key input), use – Ctrl + f

And, to move backward one page with every key input, use – Ctrl + b

In addition to, we can also move half a page with every key input. To move forward half a page, use – Ctrl + d

and, to move backward half a page, use – Ctrl + u

Once you have finished working with the file, to exit vim use – :q option.

In conclusion, we have discussed how to move around in a file with vim. If you want to know how to edit files with vim, then follow the link.