
I am not a hardcore Vim/Vi user. However, this tiny editor is beneficial for specific cases, such as editing files on a remote server 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 particular task in it, and I need to google it every time I am in such a situation. So, I decided to note down the commands whenever I use one and aggregate the Vim commands so that I can find them more quickly in one place if I forget them in the future. So, more or less, this blog post is my self-reminder. However, if you are in a situation like mine, you can bookmark this post, and maybe this can help you as well!
Entering And Exit From Edit Mode:
If you want to write or modify something, you must go into edit mode, as by default, it’s 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
Code language: PHP (php)
Most of the other commands below assume you are not in edit mode. So, make sure you are in that state before trying another command.
Show/Hide Line Number:
By default, when vi opens a file, the line number is not shown. If we need to show the line number for easy navigating purposes, the following command can help us achieve this.
#Show line number
:set number
#Hide line number
:set nonumber
Code language: CSS (css)
Go To Specific Line Number:
If we need to navigate to a specific line number at some point, which might be useful in a large file, the following command will be helpful.
:123
Code language: CSS (css)
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
Code language: PHP (php)
Save a file with the sudo command:
Sometimes, we forget to open a file as root or the owner, thus, we open it as read-only, but later I 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 an 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
Code language: JavaScript (javascript)
Delete all texts in vi:
To delete all contents of a file or from the 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
Code language: CSS (css)
If you are trying to delete a smaller chunk, follow the selection procedure described below in the 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 to successfully copy/cut something and paste it somewhere else while on the Vim editor.
# select starting point
v
//use arrows to move up/down/left/right
d #to cut
#or
y # to copy
p #paste
Code language: PHP (php)
Go To End Of File:
If we are just reading a large file and our concerned area is only the end, you will most likely want to jump to the end of the file. It’s easy as pressing the capital ‘G’.
# go to end of file
G
Code language: PHP (php)
Are any Major Vim Commands Missing?
Well, initially, the list contains only the commands I remembered so far that 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.
Discover more from CODESAMPLEZ.COM
Subscribe to get the latest posts sent to your email.
Leave a Reply