Vim popular shortcuts


In this article I will walk you through some of the popular commands that you can use to get up fast and start using the Vim editor. Vim is the successor of Vi editor written by Bill Joy who is a VC now. Vim was written by Brian Moolenar as a charity ware, meaning all of its donations goes to the non - profit in Uganda I think. Even before reading any further, I recommend one to learn touch typing
, as this will significantly impact your muscle memory and speed like it did to me any many others who learnt it at a later stage in our lives. The other important aspect is if you are busy already, it is better to postpone learning this until a time when it is reasonable for you to learn. Learning vim or touch typing will significantly impact your learning and speed as well. There are a number of sites online that can help with learning or practicing touch typing. Having said this, let us begin or continue our journey into learning some of the popular shortcuts in vim.
Why one should learn vim?
Vim is a popular visual editor from almost the beginning of computers whose predecessor was vi. It has been used widely by mainly Open source developers since the development of *nix systems. One easiest way to get started with vim is to use the vimtutor
utility on *nix machines. It navigates through a bunch of lessons essential to start learning or improvising your vim skills as well.
Today's popular editor's like Visual Studio Code has vim support through its extensions.
Vim has four main modes Normal, Edit, Visual and Command mode. Default is Normal/ navigation mode, enter Edit mode by typing i or a, Visual mode by typing v or V and Command mode by only typing :
We will go through the commands for each of the modes below.
Edit mode
Editing of text is done in this mode and hence it will have no shortcuts.
i - insert or start typing or to enter edit mode before cursor
a - insert or start typing or to enter edit mode after cursor
I - go to beginning of line and edit
A - go to end of line and edit
o - creates new next line and you can start editing on the next line or below
O - creates new next line and you can start editing on the previous line or above
Visual Mode
Read only mode mainly for text selection or visualization.
v - begin visual mode from cursor
V - begin visual mode line by line from cursor
Ctrl+v - visual block
Normal mode
Read only mode mainly for navigation, searching, register access or update operations.
Navigation
Navigating the file in read only mode. h
, j
, k
and l
are recommended for navigation in normal mode over arrow keys in edit mode, you will find it why eventually.
gg - go to top of file
GG - go to end of file
G - go to beginning of file
K - man page entry if available
zz - bring current line to the center of the screen
h - navigate left
l - navigate right
k or - - navigate up
j or + - navigate down
Ctrl + u - navigate top
Ctrl + d - navigate down
Ctrl + f - navigate forward
Ctrl + b - navigate backward
} - navigate forward by para
{ - navigate backward by para
w - jump word
e - jump to end of word
b - jump word backwards
2w or 2e - jump two words or jump to end of two words
`' - go to last cursor position after opening a file
`. - go to last edit position after opening a file
0 or ^ - go to start of line
$ - go to end of line
^ - go to beginning of line
% - go to end or start of braces {} or [] or () .....
[{ - go to opening of braces
]} - go to ending of braces
Ctrl + i - go previous cursor positions even other files
Ctrl + o - go after cursor positions even other files. Navigates back to the called function or similar in code.
Ctrl + ] - on tag link goes to link. Navigates to calling function or similar in code.
Ctrl + t - goes back to link
6k - 6 lines up
7j - 7 lines down
7l - 7 letters right
6h - 6 letters left
6=j - format 6 lines below
6=k - format 6 lines above
q: - command mode in vim mode or history
Searching -
/word - search for word in forward (top to down) direction
?word - search for word in reverse (bottom to up) direction
n - search for next position of word
N - search for previous position of word/ back search
\ - find next word
# - find previous word
Text manipulation
r - replace letter
R - replace letter until ESC
~ - change word case under cursor
vii - select content in a funcion or a method or similar
yiw - copy word under cursor
Changing
Changing refers to changing text or cutting text and entering edit mode.
c - 2w or c2e - change to two words or end of two words
cw - change word from cursor till word
ce - change word from cursor till end
cc - change entire line
C - change word from cursor till end of line
ciw - change word with cursor on word
ci> - change within angle brackets or similar can be used for any quotes or brackets
Deleting
Deleting refers to deleting text or cutting text.
x - delete letter on cursor
X - delete letter before cursor ( backspace )
d - delete selection
dd or S - delete line
D - delete from cursor to end of line
Copy and pasting ( yanking )
y - yank ( copy ) selection
yy - yank line
p - paste after cursor
P - paste before cursor
3p - paste three times after cursor
Copy multiple texts in a file to the various registers
"ay - copy selected text to register a
"ap - paste selected text from register a
"*p - paste selected text from the system's clipboard which is stored in register *
. Useful alias for Shift + Ins
.
""p - paste selected text from vim's clipboard which is stored in register "
. - repeat last action
u - undo last action
Ctrl + r - redo last action
Command Mode
Mainly to perform file operations of the file and run shell commands as well.
:w - write to disk
:wq - write to disk and close editor
:q! - close editor but not write any changes
:! or :e! - undo changes or reload file
:tabedit filename - creates new tab opening filename in it
:gt - go to next tab
:gT - go to previous tab
!ls - execute shell commands, in this case ls
:sort - after selecting say a list of words or numbers in visual. Typing this sorts them in alphabetical or numerical ascending order
:colorscheme solarized - switching themes found in $vimruntime/colors. Others include morning
, evening
, peachpuff
, industry
, murphy
, blue
, darkblue
, desert
and all
:set background=light - or dark
depend on time of day for a specific theme.
:% streams the whole file to the next step
:%!cut -d ' ' -f 2 - streams the file contents to the cut command
:%s/word/new/gc - replace word with new in the whole file with confirmation each time. Regex patterns is supported here.
Ctrl + r + " - paste yanked text in command mode
:%norm dwWD - Run normal mode commands. Here like delete word, then go to next word and delete till the end of line.
Text manipulation or selection
For text manipulation or selection, there is this principle like the combination of [Action] [Area] [Object] . When learning you may want to begin with visual v
visual for action though like say vaw
which is select a word under the cursor.
[Action] - can be v
for select, c
for change (changed text will be in clipboard now ) and enter edit mode, d
for delete (deleted text will be in clipboard now ), 3v
three visualize, y
for yank or copy.
[Area] - can be i
inside or o
for outside,
[Object] - can be w
for word, i
for indent, "
for respective quote or (
for respective bracket or for between any kind of quotes or brackets.
eg. - yaw
, v3w
, vii
, vi<
, ciw
, c0
, v$
Text manipulation or selection with search
Search has two main letters f
forward inclusive and t
till exclusive. ;
and ,
will circle or continue front or backwards through the search. F
and T
will perform search in the backward directions.
eg. - vfn
, vTn
, 3vTn
Enable vi in your bash shell or terminal
Bash by default does not use vi
for edit or manipulation in shell. Append set -o vi at the end of your ~/.bashrc
and source it source ~/.bashrc
. In bash shell press <Esc>
to enter visual mode k
and j
will cycle through history. /
should do reverse search and ?
for forward.
VS Code vim plugin
gh - show documentation or warning or error information on cursor.
Ctrl + / - comment line of code.
Paste registers
" - Last deleted line is saved in this register
- - Last deleted text is saved in this register
+ - Last deleted line is saved in this register or even in *
0-9 - Last deleted lines is saved in these registers in descending order
reg - Access the register itself
"ay - Copy to the a
register
"ap - Paste from the a
register
Macros
Macros are used to repeat actions like record and play
qe - Will begin recording a macro to e
. q
to exit or stop.
@e - Will play recorded macro from e
FAQ
How to copy text to command mode?
After yanking the text and pressing : Do Ctrl+R and "
How to paste the previously yanked ( copied ) text?
"0p - pastes the second last copied
:reg shows the paste registers. Use "6p to paste the 6th register