Skip to main content

Learning how to use vim: Basic Introduction

VIM - Command-Line Text Editor

Introduction

When I was started programming I switch lots of text-editor like gedit, nano, notepad++, sublime text, vs code, etc. But I never found any other editor like vim it is very powerful, lightweight(small in size), highly customizable, fast, easy(not really for new user) and many more features in one editor and it also works on Android smartphones.

What is VIM?

Vim is a built-in Unix command line text editor. It is an improved version of VI (vee eye). It is not similar to any other text editors. A new user will definitely get some difficulty in interacting with vim. The earliest version of vim is now 27 years old (!) and was created by Bram Moolenaar.

What makes vim different?

Vim has several modes, using theses mode we can read, write, replace and perform many operations. These modes are listed below:
  • Normal Mode
  • Insert Mode
  • Command Mode
  • Visual Mode
  • Replace Mode

How to use vim?

Normal mode

Type vim in terminal you will welcome to vim. By default, vim starts in "normal" mode. Normal mode can be accessed from other modes by pressing Esc or <C-[>.

In Normal mode, key presses don't work as one would expect. That is, they don't insert text into the document, instead certain key presses can:

Move the cursor

  • h move one character left
  • j move one character down
  • k move one row up
  • l moe one row down
As many vim commands, row movement can be prefixed by a number to move several at a time.
  • 4j move 4 rows down
  • 6k move 6 rows up

Basic word movement:  

  • w move to beginning of next word
  • b move to previous beginning of word
  • e move to end of word
  • W move to beginning of next word after a whitespace
  • B move to beginning of previous word before a whitespace
  • E move to end of word before a whitespace

Beginning/End of line movement:

  • 0 move to the beginning of the line
  • $ move to the end of the line

Insert Mode

This is the second most used mode and will be the most familiar behavior for most people. Once in insert mode, typing inserts characters just like a regular text editor. You can enter it by using an insert command from normal mode.

Insert commands include:

  • i for ’insert’, this immediately switches vim to insert mode
  • a for ’append’, this moves the cursor after the current character and enters insert mode
  • o inserts a new line below the current line and enters insert mode on the new line

These commands have an uppercase variety too:

  • I move the cursor to the beginning of the line and enters insert mode
  • A moves the cursor to the end of the line and enters insert mode
  • O inserts a new line above the current one and enters insert mode on the new line
There are so many more ways of inserting text in Vim that can’t be listed here but these are the simplest. Also, beware of staying in insert mode for too long; Vim is not designed to be used in insert mode all the time.
To leave insert mode and return to normal mode, press Esc or <C-[>.

Visual Mode

Visual mode is used to make selections of text, similar to how clicking and dragging with a mouse behaves. Selecting text allows commands to apply only to the selection, such as copying, deleting, replacing, and so on.

To make a text selection:

  • Press v to enter visual mode, this will also mark a starting selection point
  • Move the cursor to the desired end selection point; vim will provide a visual highlight of the text selection

Visual mode also has the following variants:

  • V to enter visual line mode, this will make text selections by line
  • <C-V> to enter visual block mode, this will make text selections by blocks; moving the cursor will make rectangle selections of the text
To leave visual mode and return to normal mode, press Esc or <C-[>.
The visual mode actually has multiple subtypes: visualblock-visual and linewise-visual
  • visual: like described above. Enter by pressing v
  • block-visual: select any rectangular region. Enter by pressing <ctrl>+v
  • linewise-visual: always select full lines. Enter by pressing <shift>+v

Command Mode

Command mode has a wide variety of commands and can do things that normal mode can’t do as easily. To enter command mode type ’:’ from normal mode and then type your command which should appear at the bottom of the window. For example, to do a global find and replace type :%s/foo/bar/g to replace all ‘foo’ with ‘bar’
  • : Enters command mode
  • % Means across all lines
  • s Means substitute
  • /foo is regex to find things to replace
  • /bar/ is regex to replace things with
  • /g means global, otherwise, it would only execute once per line
Vim has a number of other methods that you can read about in the help documentation, :h or :help.

Replace Mode

Replace mode allows you to replace existing text by directly typing over it. Before entering this mode, get into normal mode and put your cursor on top of the first character that you want to replace. Then press ‘R’ (capital R) to enter replace mode. Now whatever you type will replace the existing text. The cursor automatically moves to the next character just like in insert mode. The only difference is that every character you type will replace the existing one.


Signing off

When I was started to learning vim the experience I was got is similar to learn programming. In starting vim is difficult to use but you have to build a muscle memory to switch between different modes.

This is just a Basic Introduction to vim. There are too many things to learn in vim like Plugin, Short cuts, .vimrc, etc.


What I'm going to cover next time:

vim shortcuts and
building a .vimrc file

 

Comments