How to use Vim Part 1
Vim is a great editor to manipulate text. Once you learn, you would see huge productivity in your work. I will go over a series of tutorials to cover VIM in depth. Starting with the basic one.
I will go over the basics of VIM in this one.
It is easy very easy to open a file in VIM. Just do
vi <filename>
Once you have the file open. You can use arrow key to up and down the file. If the file is empty you would see your cursor sitting at the same point.
Vim has 4 primary modes.
- Search Mode which is invoked by typing forward slash /
- Insert Mode which is invoked by typing character i
- Command Mode which is invoked by typing colon :
- Visual mode which is invoked by typing character v
I will cover first two modes in this one tutorial.
How to do search in VIM
To search anything, just type /<search word> as shown below.
Checkout the forward "/indentation" in the snapshot above. This will look for word indentation in the file. If it found the word the cursor will move to indentation word. Below snapshot shows that.
Search by default is case sensitive. You can search in case insensitive mode, For that you will have to go in "command mode" which I will cover in the next tutorial but if you are curious to know then type
:set ic
Note there is colon in the beginning.
How To Navigate in VIM
Before we jump in to insert mode, it is very important to talk about navigation in VIM.
One can use arrow keys to go up and down, left and right. But VIM is very keyboard friendly and to leverage true power of VIM, one should navigate the VIM way.
- To go up press k key
- To go down press j key
- To go left press h key
- To go right press l key (that is small case L)
How do you go to top of file in VIM
press small case gg two times
How do you go to bottom of file in VIM
press shift+g
How do you go to beginning of line in VIM
press 00
How do you go to end of line in VIM
shift+4
How to delete a current line in VIM
It is very easy to delete the line on which your cursor is. Just press d twice
dd
How to copy and paste in VIM
To copy a line, Go to the line which you want to copy. Then Press y twice
yy
To paste the copied line, there are two ways
- If you want to paste above the current line. Press Shift+p
- If you want to paste below the current line. Press p
How do you delete content from top of file to all the way end
- First go to the beginning of file by typing gg twice then
- press d follow by shift+G in quick succession
How to enter in Insert Mode in VIM
Insert mode is to enable writing in vim To get in to insert mode just type character "i" and cursor will start flicking and also you will see status changed at the bottom of vim to "insert (paste)" as shown below...
It also has word "paste" next to "INSERT" that means you can also paste anything in to VIM once you are in the insert mode. Once you are in the insert mode, you can't search. To go back to search mode, you will have to press esc key.
esc
Remember press the "esc" on your keyboard, do not type esc key.
How to exit in VIM
Exiting out of vim is very easy. Just do following
:q (colon q) follow by Enter Key
If you have made changes to file and not saved, you would see following message.
- If you choose by typing N, You will get out of the vim but your changes will be lost.
- If you want to save changes before exiting, just type Y, Your changes will be saved and you would get out of vim.
- If you don't want to be prompted by above message.
- Either save the changes before exiting by doing following...
- :w (colon w) follow by Enter key and then do :q (colon q) follow by Enter Key. You can also do :wq (colon wq) follow by Enter key.
- or if you want to ignore the changes type following...
- :q! (q exclamation) (follow by Enter Key)
Related Topics
vim comment out multiple lines