Vim : A Powerful CLI Editor for Linux Users


Hi there, folks!
Have you ever used Notepad on a windows machine! It is the simplest text editor out there and almost everyone has come across it. But have you ever wondered - What about Linux ? Does it have it’s own text editors ?
The answer is a big YES, Linux has 2 types of text editors. GUI-based (Graphical User Interface) editors for ease of use, and CLI-based (Command Line Interface) editors for those who like working directly in the terminal.
GUI text editors in Linux :
Gedit
kate
Sublime Text
VS Code, etc.
CLI text editors in Linux :
Nano
Vim
Emacs, etc.
I started learning about vim editor first. This is how you open vim editor on terminal.
vim example.txt
Initially, when you open the vim editor, you are in the “normal“ mode by default. Here are some basic operations that I performed at start :
i
→ Insert mode. TypeESC
to return to Normal mode.x
→ Delete the character under the cursor.:wq
→ Save and Quit (:w
save,:q
quit)dd
→ Delete (and copy) the current line.p
→ Pasteh,j,k,l
→ It is used to move up, down, left, right in the vim editor. (h → left, l → right, k → up, j → down.)
Above basic operations made me comfortable to start with vim. Now, it was time to know more about vim. Following are some common operations that are performed on vim using keystrokes :
Insert mode variations:
a
→ insert after the cursorO
→ insert a new line before the current oneo
→ insert a new line after the current onecw
→ replace from the cursor to the end of the word
Basic moves
0
→ go to the first column^
→ go to the first non-blank character of the line$
→ go to the end of lineg_
→ go to the last non-blank character of line/pattern
→ search forpattern
within file content
Copy/Paste
P
→ paste before current cursor positionp
→ is paste after current cursor position.yy
→ copy the current line, easier but equivalent toddP
(delete + copy + paste)
Undo/Redo
u
→ undo changes<C-r>
→ redo changes
Load/Save/Quit/Change File (Buffer)
:e <path/to/file>
→ open a file on given path
:w
→ save
:saveas <path/to/file>
→ save to<path/to/file>
:x
,:wq
→ save and quit (:x
only save if necessary)
:q!
→ quit without saving, also::qa!
to quit even if there are modified hidden buffers.
:bn
→ show next file in buffer:
bp
→ show previous file in buffer
Above commands are necessary for the survival. After this, I started to have fun with the operations in vim editor. Following commands made me feel better at using vim :
.
→ (dot) will repeat the last command,N<command> → will repeat the command N times.
2dd
→ will delete 2 lines3p
→ will paste the text 3 times100ianiket [ESC]
→ will write “aniket aniket aniket aniket aniket aniket …….” for 100 times.
Using vim till this point was a cool experience. Learning above operations made me curios about what more operations can be done on vim. I realised that moving efficiently within vim is important. Following are some moves that will make using vim easy :
N
G
→ Go to line N (8G will take you on 8th line in file)gg
→ shortcut for1G
- go to the start of the fileG
→ Go to last lineWord moves:
w
→ go to the start of the following word,e
→ go to the end of this word.
%
: Go to the corresponding(
,{
,[
.*
: go to next occurrence of the word under the cursor#
: go to next occurrence of the word under the cursor
Now, it was time to go into more depth. Here is a general way to perform most of the operations and above commands will help you using it :
<start position><command><end position>
For example : 0y$
means
0
→ go to the beginning of this liney
→ yank from here$
→ up to the end of this line
This format can be used with not just yank(y), but almost all other tasks such as delete(d), visual select(v), upper case(gU), lower case(gu), etc…..
I thought that’s it, there is not much to learn about vim now. But I was wrong. I got to know about these killer features :
0
→ go to column 0^
→ go to first character on the line$
→ go to the last columng_
→ go to the last character on the linefa
→ go to next occurrence of the lettera
on the line.,
(resp.;
) will find the next (resp. previous) occurrence.t,
→ go to just before the character,
.3fa
→ find the 3rd occurrence ofa
on this line.
Summary
So, I learnt these things about vim :
Inserting, deleting text in vim files. navigating with
h,j,k,l
Saving, opening vim files with commands like:e, :wq, :q!, :saveas
, etc.Insert mode variations, basic moves, copy & paste, undo & redo.
Commands to repeat tasks. Move efficiently within vim using
NG, gg, G,
etc.Combining tasks to work efficiently using a general format :
<start position><command><end position>
example :
0y$
,0d$
,0V$
Subscribe to my newsletter
Read articles from Aniket More directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Aniket More
Aniket More
Hi, I'm Aniket. I'm passionate about coding responsive and dynamic web applications using React, TypeScript, HTML, and CSS. Proficient in Core Java and OOP concepts. Currently exploring Linux and diving into the exciting world of DevOps. Sharing my learning journey and projects to inspire others.