A Collection Of Frequently Used Vim Commands

I am not a hardcore Vim/Vi user. But this tiny piece of editor is very much helpful for certain cases, such as editing files on remote server instance while connected via terminal/ssh. Thus, I have occasionally used this whenever needed. However, I felt that, as I don’t use it very frequently, I often forget how to do a certain task in it and I need to google it every time I am in such situation. So, I decided to note down the commands whenever I use one and aggregate the vim commands, so that I can find it more quickly in one place if I forget in future. So, more or less this blog post is for my self reminder. However, if you are in a situation like me, you can bookmark this post and may be this can help you as well!

Entering To And Exit From Edit Mode:

If you want to write or modify something, its necessary to go into edit mode as by default its in read-only mode. To do so, just press ‘i’ to jump into edit mode and press ‘Esc’ to get out of edit mode.

i # edit mode
Esc # exit from edit mode

Most of the other commands below will assumes you are not in edit mode. So, make sure to be in that state before trying another command.

Show/Hide Line Number:

By default when vi open a file, there is no line number shown. If we need to show the line number for easy navigating purpose or so, the following command can help us achieving so.

#Show line number
:set number
#Hide line number
:set nonumber

Go To Specific Line Number:

If in some point, we need to navigate to a specific line number, which might be useful in case of a large file, the following command will be helpful.

:123

Search text:

If we need to search for a specific word/text, we can do it by entering the following commands:

/{text}
#(to navigate to next item)
n

Save a file with sudo command:

Sometimes, we forget to open a file as root or the owner, thus we open it as read-only, but later me decide to edit it. After making the changes, if you try to write the content with “:w”, it will fail because of the permission issue. Then, we will have to choose alternative way to write our changes instead of the painful “reverting->exit->open as owner/root, make the changes again->save” process. Use the command below:

:w !sudo tee % > /dev/null

delete all texts in vi:

To delete all contents of a file, or from current line to the end of the file, we can use the commands below:

shift v #to select current line
shift g #jump to eof
d #delete texts

Or if you are trying to delete a smaller chunk, follow the selection procedure described below in copy/cut section and press ‘d’.

Copy-Cut/Paste:

Copy/Cut/Paste is also a frequently used operation we need while writing/editing a file. Here are the series of commands you will need to perform in order to successfully copy/cut something and paste them somewhere else while on vim editor.

Go To End Of File:

In case we are just reading a file which is large and our concerned area is only the end. You will most likely want to jump to the end of the file in that case. It’s easy as pressing capital ‘G’.

# go to end of file
G

Is Any Major Vim Command Missing?

Well, initially the list contains only the commands I remembered so far I need to use often. There might be others that you have in mind/use often to make your days easier. Let me know about those and I will add them to the above list.