I tried learning
vi quite a few times in the last several years. Each time, I gave up after learning the basics. Being a Windows .NET developer, I just couldn’t find that many use cases for it. On the handful of occasions I found myself editing files on my MacBook,
nano was good enough. So I never really learned
Now that I am programming full-time on Ruby and Rails and learning Linux, I realize that
vi is an essential skill to have. So I finished the Gentle Introduction to vi chapter from The Linux Command Line, and happy to report that I feel much more comfortable editing files in
vi now than just a day before.
Of course, there’s a long way to go, but this book teaches you 20% of the shortcuts you’d be using 80% of the time, which is pretty neat. This post tries to summarize everything I learned, including why you should learn this editor.
Why Learn vi
viis always available. If you are working with a remote server,
viis guaranteed to be there.
viis lightweight and fast. A skilled
viuser never has to lift their fingers from the keyboard while editing.
vim is an enhanced replacement of
vi that stands for vi improved, written by Bram Moolenaar.
To the beginning of the current line.
To the first non-whitespace character on the current line.
To the end of the current line.
To the end of the file.
To the top of the file.
To the beginning of the next word or punctuation character.
To the beginning of the next word, ignoring punctuation characters.
To the beginning of the previous word or punctuation character.
To the beginning of the previous word, ignoring punctuation characters.
To line number. For example, 1G moves to the first line of the file.
To the last line of the file.
Switch to the insert mode.
Append text at the cursor position.
Append text at the end of the line.
Insert a blank line below
Insert blank line above
Delete the character the cursor is on.
3x for three characters.
Delete the current line.
Delete the word from the current cursor.
Delete from the current cursor up to the end of the line.
Delete from the cursor to the beginning.
From the current line to the end of the file.
Cut, Copy, and Paste
d commands cut text.
Copy the current line.
5yy to copy the current line and next four lines.
Copy the word from the cursor.
Copy the line from the cursor.
Copy from the line to the end of the file.
Paste text on the next line.
Search and Replace
Searches the current line for character
a and places the cursor there.
Searches for the word Example in the file. Press
n to move to the next search term.
Replace all occurrences of line with Line with a confirmation.
5u to undo the last five actions.
join line, i.e. cut and paste the next line at the end of the current line.
Exit vim. Press
:q! to exit without saving.
:wq / ZZ
Save and exit.
Learning the Linux command line, like becoming an accomplished pianist, is not something that we pick up in an afternoon. It takes years of practice.
Hope it helps.