Vim is as easy as it is terrifying
“Oh, what is this, I can’t go out of this, ummm … let’s close the terminal.”
Most of us have been here, a terminal window from where you can’t go out, go back to the command line or edit a file. Vim — an editor, basically or to be precise a text editor, is known to be as one of the favorite editors for programmers but I always have wondered ‘why and how can this terrifying thing can be a favorite?’ until I started using vim myself.
Well, this is more of a guide to get you started with Vim and master it. Vim is very advanced but you can work out using the first part of the story. However, advanced controls will be soon updated in the second part of the story.
Vim is pre-installed in all Linux distributions. However, if you happen to not have it, here’s how to install.
As we have Vim ready, let’s get started.
Exiting
Getting out of Vim is the most important thing here so that you can start over. There are several ways to do so but they all have something auxiliary as commands can be grouped.
:q - quit, won't work in case of unsaved changes
:q! or ZQ - discard any unsaved changes and quit
:w - save the file
:w !sudo tee % - save the current file with sudo
:wq or :x or ZZ - save and quit
Global Commands
:help 'keyword' - open help any keyword, no quotes
:close - close current tab/pane
:saveas file - save file as, specify the filetype
K - open man page for word beside the cursor
Navigation
Intuitively you’ll manage here with the arrow keys but there’s something more to navigation in Vim
Note that — Any prefixed number to any command will repeat it that many times
Basic
k - cursor up
j - cursor down
h - cursor left
l - cursor rightQuick Shortcuts
H - jump to top of the screen
M - jump to middle of the screen
L - jump to bottom of the screen
0 - jump to the start of the line
$ - jump to the end of the line
gg - go to the first line of the file
G - go to the last line of the file
} - jump to next paragraph/block/function
{ - jump to previous paragraph/block/function
zz - center cursor on the screen
Insert-Mode
As you can see that we use the keyboard to navigate across the file, you must be wondering how to make changes to the file. Well, for that we have the Insert Mode. Whenever you are inside the insert mode, the keyboard input is going to type in the characters.
i - insert before the cursor
I - insert at the beginning of the line
a - append after the cursor
A - append at the end of the line
o - open a new line below the current line
O - open a new line above the current line
ea - append at the end of the word
Esc - exit insert mode
Editing
Editing is the thing which may give a slight feeling of usual editors as here, we have some visual operations using the Visual Mode. However, the basic functionality remains the same. Again, we are covering the most important ones, so as to pace up learning.
r - replace a single character
gwip - reflow paragraph
cc - replace entire line
C - replace to the end of the line
ciw - replace entire word
cw - replace to the end of the word
s - delete character and substitute text
S - delete line and substitute text
u - undo
Ctrl + r - redo
. - repeat the last commandVisual Mode
v - start visual mode, mark lines, then do a command
V - start linewise visual mode
o - move to other end of marked area
Ctrl + v - start visual block mode
O - move to other corner of block
aw - mark a word
Esc - exit visual modeVisual Commands
> - shift text right
< - shift text left
y - copy (aka yank) marked text
d - delete marked text
~ - switch the caseCut, Copy and Paste
(Prefixed number to any command will repeat it that many times)dd - cut a line
D - cut to the end of the line
x - cut character
yy - copy a line
y$ - copy to end of line
p - paste the clipboard after cursor
P - paste before cursor
dw - cut the characters of the word from the cursor position to the start of the next word
yw - copy the characters of the word from the cursor position to the start of the next word
Search and replace
/pattern - search for pattern
?pattern - search backward for pattern
:noh - remove highlighting of search matches
:%s/old/new/g - replace all old with new throughout file
:%s/old/new/gc - replace all old with new throughout file with confirmations
That’s it! You’re now ready to utilize Vim to considerable extents.
In the second part of this story, we are going to have some advanced navigations, editing and working with multiple tabs/files.
And of course, customizations in Vim.
Stay tuned! Happy Vim-ming.
Vim: Advanced Mode
Coming soon!
This blog is written with reference to Vim Cheat Sheet.
For an interactive tutorial on Vim visit openvim.