Vim Essential Commands
Basic And Intermediate Commands Every Linux User Must Know.
By Prajwal Basnet | Oct 14 2022 | 7 minute read
Introduction
Vim a.k.a VI is one of the most popular text editor which was intially designed for unix but now due to its rising popularity and easy to use features - it is avilable in most operating systems. The puropse of writing this article is to highlight essential vim commands for your everyday needs ( Bookmark this site for easy reference later ).
I wrote this post considering the reader's knowledge in mind. You can start from the beginning if you have no prior knowledge of vim, but if you consider yourself an intermediate user who is familiar with the basics of navigation, editing, and modes and occasionally forgets commands, you can start from the intermediate section of this guide.
Begginer Section
Vim modes
To make your life easier, vim has three modes - Command/Normal, Insert and visual mode.
Command mode is the default mode that you will encounter once you open vim ( Keyword don't work as you expected but don't panic). In this mode, you can execute commands like undo and redo, find and replace, save and quit etc. you will be doing most of your editing work from this mode.
Insert mode is the mode that allows you to enter text. You can enter this mode by pressing i
. Again, to enter Command mode press esc
- from where you can save or quit file. you will only use this mode for writing.
Visual mode is similar to select items by clicking and dragging mouse but with keyboard. It is mostly used when copying or deleting a large number of lines. The below commands assist you in most cases, so you don't have to remember all the things.
shift + v - to visualize whole line (then you can move up and down).
ctrl + v - to visualize a letter.
Esc - to exit visual mode.
v - to enter visual mode (you can use ctrl + v instead of this most of the time).
In a nutsell, Esc to enter command mode, i to enter insert mode and v to enter visual mode.
Installation
One of the biggest advantage of using vim is - it is installed by default in most of the linux or unix operating system. Even if it is not installed by default, you can easily get from your operating system package manager like :
For debain based O.S:
sudo apt-get install vim
For CentOs/Fedora:
sudo yum install vim
if you want to install vim in vscode, simply install it from extension market (I prefer to download vim extension uploaded by vscodevim).
To open a file in vim
Vim filename
Working with Files
All these commands are entered in Command mode( by pressing esc if you are in insert mode). Vim commands to work with files:
:w - to save current file.
:wq - to save and quit current file.
q! - to quit file without saving.
:help - to get help
Editing Commands
The below commands are usually used with the combination of visual mode.
Remember y is only used to copy within vim - not in system clipboard.
yy (yank) - to copy a line
y$ - to copy from present position of cursor to end of line.
p(paste) - to paste .
dd - to delete a line.
x - to delete single words.
"+y - to copy in system clipboard
u - to undo
ctrl + r - to redo
. - repeat last action
gg - to go to top of the file.
G -to go to bttom of the file.
Combinations
gg + shift v + G = to copy whole file.
shift v + 15(number) + k (or up arrow) + d - to delete 15 words above from cursor.
N(number) + y + j - to copy N number of line below from cursor.
Intermediate section
Vim Essential Commands
If you have mastered the above commands, Then welcome to the tech world. Now, you will be expected to do things in a geeky way. Till now you might have been using arrow, mouse, or using lengthy keywords strikes to jump from one text to another, but from now you will be using below mentioned command to move from one text to another text.
Tips - keep your index fingers on small hump in f and j.
ctrl + p - to suggest word.
f + keyword - to find next keyword
g + ctrl + G = to see word count
gqq = to Split/Format one big lines into multiple small lines.
g^ = to move to the start of the line, when one long line got into multiple lines.
shift + > - to indent four lines(one tab) to right (used after visualizing texts).
shift + ~ - to change case (used after visualizing texts)
Advance Navigation
h - move the cursor to left .
l -move the cursor to right.
k -move the cursor up.
j -move the cursor down.
Moving within a screen
ctrl + y - move up without moving cursor.
ctrl + e - move down without moving cursor.
ctrl + b (back) - move cursor one page back.
ctrl + f (forrward) - move cursor one page forward.
H (high) - Put the cursor at top of screen.
M (middle) - put the cursor at middle of screen
L (low) - put the cursor at lowest point (bottom) of screen.
{ - move to start of previous paragraph.
} - move to start of next paragraph.
G* - to search current word (to reverse G#)
Line editing
^ (caret) - move to beginning of line.
$ - move to end of line.
A - Directly enter insert mode from end of line.
I - to insert from beggining of line.
0 - to move at the start of line.
Word editing
b (back) - put the cursor at start of previous word.
w (front) - put the cursor at start of next word.
Command line vim
Commands are used to attain many functionalities of vim like to find and replace, set number of lines etc. so, it is vital to learn below commands to master vim
/keyword - search keyword form the file then to navigate to next word press "n" and to previous press "N" .
:set number - to set line number in file.
%s/pattern/replacement/g - to replace all patter with replacement at once (you can use regex aswell as string in pattern and replacment).
%s/pattern/replacement/gc - replace all occurance and confirms each one.
:sp filename - split screen horizantolly and to switch tabs ctrl + w + j/k.
:vsp filename - split screen vertically and to switch tabs ctrl + w +h/l.
Combos
ctrl v + j/k(up/down) + shift I + #(comment) + Esc comment all the line at once.
dap - delete around paragraph.
visual + ctrl a - to increment number (ctrl +x - to decrement).
gg + shift v + G + "+y = to copy all the content from vim to system clipboard
Conclusion
You should now have a better understanding of some more intricate ways vim can be assist you. Even though it may seem like a lot, it is just scratching the surface.
There are many functions that we haven't covered, including registers, vim plugins, macros, and the.vimrc file. As a result, if you want to learn more about vim, I've provided some more resources.The more you practice and use it every day, the more natural it will feel, and the more powerful it will become,
Recourse to learn vim
- Vim Adventures - Learn vim by playing game.
- The boook i recommend to learn vim " Practical Vim: Edit Text at the Speed of Thought ".
- Also checkout Learn-Vim - github guide to learn vim.
- Definetly checkout interactive tutorial from openvim.
- Also checkout lessons from vimgenius.
- Also check Luke Smith videos on youtube.