I used to work with Nano text editor often, but I have heard awesome things about Vi/Vim and really wanted to give them a try (mainly because they looked cool, and I was also curious to see what was so great about them). Using Vim for the first time scared me—I did not want to mess anything up! But once I got the hang of it, things became much easier and I could appreciate the editor's powerful capabilities. Well, I sort of gave up others, but I'm happy being stuck with Vim. Some people feel difficult to use Vim.
In this blog post, I will walk through Vim (based on my personal experience) just enough so you can get by with it as an editor on a Linux system. This will neither make you an expert nor even scratch the surface of many of Vim's powerful capabilities. But the starting point always matters, and I want to make the beginning experience as easy as possible, and you can explore the rest on your own.
Concept
It is very important to understand the concept in Vim, possibly the most important to remember: Vim has multiple modes. Here are three you need to know to do Vim basics:
Mode Description
Normal ---> Default; for navigation and simple editing
Insert ---> For explicitly inserting and modifying text
Command Line --> For operations like saving, exiting, etc.
Vim has other modes, like Visual, Select, and Ex-Mode, but Normal, Insert, and Command-Line modes are good enough for us.
Vim Command Reference
save: :w
save and exit: :wq
exit: :q
force: ! (example :w! :q!)
vertical split: open a document and then type :vsplit /path-to-document/document and this will open the specified document and split the screen so you can see both documents.
copy: y
copy a line: yy
paste: p
cut: d
cut a line: dd
Creating file
vim mahesh.txt
(you will reach at Vim normal mode, to add the text you need to be in insert mode for that type i) now you can add text
Quiting file (Shift + :q)
:q
Saving file
:w
Save & Quit
:wq
Redoing text
Ctrl+R
Undoing text
Ctrl+U
Copying word
Go to visual mode typing, you will reach at Visual mode of Vim, then point to specific word click arrow button for selecting/marking + Y key( copying), return back to Normal mode (Escape key) click "P" to paste wherever you want.
Adding Lines
Return back to Normal mode (Escape key) point to specific line type "o" you will a get an extra empty line where you can add your additional text)
Searching words
Return back to Normal mode (Escape key) type forward slash "/" followed by your keyword
/systemd
You can press n key to get the next occurrence in the backward direction. You can also press N key to get the next occurrence in the forward direction.
Highlight the Words When Search
You can enable highlight words when searching by using the :set hlsearch command. To enable it, inside the normal mode of Vim enter the following command.
:set hlsearch
To stop highlighting words when search, enter the following command:
:set !hlsearch
Replacing words
If you want to replace a particular word in a file you can use the following command remember Return back to Normal mode (Escape key)
:%s/oldword/newword + Enter key
To delete all lines
first "gg" then "dG"
Paste from outside
Cntrl=+Shift+V
To reach the bottom line
Esc+G
To delete all lines
first "gg" then "dG"
Paste from outside
Cntrl=+Shift+V
To reach the bottom line
Esc+G