simple intro to vim
Almost 3 years back, I googled the best text editor for linux and the first result was vim. Since then, I have been writing most of my code in vim.
Vim is totally different from other text editors i have used.It’s philosophy being that
we do most of our editing work either by copy / paste / replace or moving around the curser and not just typing text.
This is where most of the people give up vim , the seemingly difficult key language.
Default mode in vim is the command mode , which is used to enter commands that operates on text or control cursor motion. So you wouldn’t see any characters you typed in when you open a file in vim.
The input mode however, is used to write into file. Pressing “ i ” in command mode takes you to the input mode.
The ex mode is used for file handling and substitution.
Vim gives vast number of features which might seem overwhelming at first, but over the time, these features turn into a highly time saving gem.
Writing into a file
Any of the below keys take you to the input mode. And each of them has its own purpose.
i to insert the text before the cursor
a to insert the text after the cursor
I and A to insert the text at line extremes
o and O to open a new line after and before current line
r to replace a single character
s to change a single character
R and S to replace and change the entire line respectively
navigation is performed in command mode. Movement in the four directions is done using arrow keys or h j k l
word navigation is done using keys
w forward to beginning of a word.
e forward to end of a word.
b move back to beginning of previous word.
ge to move back to the end of previous word.
ctrl+f scrolls forward
ctrl+b scrolls backward
ctrl+d scrolls half page forward
ctrl+u scrolls half page backward
G takes the cursor to the end of the file
G when used with repeating factor , takes us to the line number mentioned
example 42G will take us directly to line 42
gg takes to the beginning of the file
Moving to line extremes
^ to move to the beginning of a line
$ to move to the end of the line
| combined with repeat factor is used to move to particular column
example 7| would take you to the 7th column
similarly you can use ex mode for absolute movement
:20 would take you to line number 20
:set nu makes the line number to set visible
x delete a character
dd delete a line
yy to yank the current line
p and P to paste the yanked or deleted line
use repeating factor to do multi line delete , yank etc
3dd to delete 3 lines from current position
3y to yank/copy 3 lines with the current line
5x is to delete 5 characters from cursor
dw to delete a word
. and u to repeat and undo the previous command
the /,n and . commands form a very useful trio for the interactive replacement work.
search and replace
/pattern search a pattern forward
?pattern search a pattern backward
repeating the last pattern search
n repeats the search in same direction of original search
N repeats the search in opposite direction of original search
substitution can also be done in the similar way
:%s/can’t/can/g [ whole file is considered ]
:1,42s/can’t/can/g [ only line 1 through 42 is considered ]
:%s/unknown//g [ deletes all the unknown word from the file ]
saving your work and quitting
these commands are run in the last line mode , to enter the last line mode press esc key
:x to save the buffer and exit the editor
:w to write the file and continue editing
:q to abort editing
:wq to save the file and quit the editor
Even though many text editors offer vim key code or a special vim-like mode for editing purpose, vim still stands out.Vim does one thing and does it right. It makes editing easier by prioritising navigation / deletion / copy / search / replace etc.Vim adds much more to your lunch box just by giving ya .vimrc for other customisation. Vim allows you to add lots of plugins to your editor and make your life much more easier.
If you are searching for a good text editor , you should definitely check out vim. If you already use vim , that’s awesome ! share me your .vimrc.