Vim : A Powerful CLI Editor for Linux Users

Aniket MoreAniket More
5 min read

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. Type ESC 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 → Paste

  • h,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 cursor

    • O → insert a new line before the current one

    • o → insert a new line after the current one

    • cw → 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 line

    • g_ → go to the last non-blank character of line

    • /pattern → search for pattern within file content

  • Copy/Paste

    • P → paste before current cursor position

    • p → is paste after current cursor position.

    • yy → copy the current line, easier but equivalent to ddP (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 lines

  • 3p → will paste the text 3 times

  • 100ianiket [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 :

  • NG → Go to line N (8G will take you on 8th line in file)

  • gg → shortcut for 1G - go to the start of the file

  • G → Go to last line

  • Word moves:

    1. w → go to the start of the following word,

    2. 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 line

  • y → 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 column

  • g_ → go to the last character on the line

  • fa → go to next occurrence of the letter a on the line. , (resp. ;) will find the next (resp. previous) occurrence.

  • t, → go to just before the character ,.

  • 3fa → find the 3rd occurrence of a 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$

0
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.