Easiest way to learn vim commands in Step-by-Step with example

vim editor is having plenty of features. It contains a total of 12 different editing modes, out of all modes four modes are very important. This document will help you to understand its basic interface with all the important commands, how to change text colors based on keywords, how to add line number in the document, and many more in a lucid manner.

Introduction

vim is one of the most powerful and popular editors available. It is an advanced version of the vi editor which is written by Bram Moolenaar. vim is an acronym for vi IMproved. Furthermore, it seeks to provide the power of the de-facto (Means it is a cross-platform editor and available on all the popular platforms like Windows, Linux, Mac, and other UNIX variants.) Unix editor ‘Vi’, with a more complete feature set.


Advantages of mastering in vim editor.

The biggest advantage of learning vim is its availability in all the platforms. Once you are familiar with vim and its command then it is very easy to deal with anywhere. It is basically CLI (Command Line Interface) editor so easy to grasp by any age. Moreover, by using vim you are protecting your code by accidental mutations.


Different modes in vim.

One of Vim’s unique feature is its modality. Most editors have one mode (insert), where Vim has 12 different editing modes, 6 of which are variants of the 6 basic modes. I think the 4 important ones are normal, insert, command line, and visual.

  • Normal mode – used for editor commands. This is also the default mode unless the insert mode option is specified.
  • Visual mode – similar to normal mode, but used to highlight areas of text. Normal commands are run on the highlighted area, which for an instance can be used to move or edit a selection.
  • Select mode – works similarly to visual mode. However, if a printable character, carriage return, or newline (or line feed) is entered, Vim inserts the character, and starts insert mode.
  • Insert mode – similar to editing in most modern editors. In insert mode, buffers can be modified with the text inserted.
  • Command-line or Cmdline mode – supports a single line input at the bottom of the Vim window. Normal commands (beginning with : ), and some other specific letters corresponding to different actions (including pattern search and the filter command) activate this mode.
  • Ex mode – similarly to Cmdline mode, it takes a single line input at the bottom of the window. However, in Cmdline mode, entering a command exits the mode when the command is executed. Entering a command in Ex mode doesn’t cause the mode to change.

Basic vim command reference

Use the given commands to save and exit from file and use these codes in vim command mode.

:w use for saving.

:wq use for save and exit.

:q use for an exit.

! use for force (example :w!  :q!).

Example:

Use the given commands for copy and paste in vim normal mode.
y for a copy.

yy for copy a line.

p for paste.

d for a cut.

dd for cut a line.

Advanced vim command reference

So often we use find tool to search keywords in the document. In vim editor we use “/<your word to search>” in normal vim mode.
if you are in Insert mode then press ESC to come in normal mode. “/” will take you to visual mode.
/word_to_search

for nevigation press key as given below.

  • ‘n’ for next match.
  • ‘N’ (shift+n) for the previous search.
  • ‘ggn’ to jump to the first match.
  • ‘GN’ to jump to the last match.

in case you are at the bottom of a file, and you want to search backward, then instead of initiating the search with ‘/’, use ‘?’

?word_to_search

— — — — —

Highlight search results

Rather than using ‘n’ and ‘N’ to jump for search results, it is better you have highlighted search result, this can be made possible by setting the ‘hlsearch’ variable, something which you can do by writing the following in normal/command mode.

:set hlsearch

— — — — —

Case-insensitive search.

By default, the search you do in Vim is case-sensitive. This means that if I am searching for ‘linux’, then ‘Linux’ won’t get matched. However, if that’s not what you are looking for, then you can make the search case-insensitive using the following command.

:set ignorecase

So after I set the ‘ignorecase’ variable using the aforementioned command, and searched for ‘linux’, the occurrences of ‘LINUX’ were also highlighted.

— — — — —

Smartcase search.

vim also offers you a feature using which you can ask the editor to be case-sensitive only when the searched word/pattern contains an uppercase character. For this you need to first set the ‘ignorecase’ variable and then set the ‘smartcase’ variable.

:set ignorecase

:set smartcase

For example, if a file contains both ‘LINUX’ and ‘linux,’ and smartcase is on, then only occurrences of the word LINUX will be searched if you search using ‘/LINUX’. However, if the search is ‘/linux’, then all the occurrences will get matched irrespective of whether they are in caps or not.


Enhance the appearance of code writing in vim.

before setting up vimrc file

Before setting up the vimrc file how it looks. So to make it look good we need to apply some commands to it, let’s dig into it.

Display line number command

If you want to see line number in your code then use the command as shown below. I personally suggest to enable this feature, the main reason is it will help while we are debugging the code.

:set number

— — — — —

Colorful text theme

To enable this theme its supper easy “syntax on” the only command. and to disable it use “syntex off” command. in-home directory of your terminal creates a file as named .vimrc then write syntax on then save the file by writing :wq in command line and press enter.

  • create .vimrc file in the home directory by using vim ~/.vimrc
  • write the given code in the file by going in insert mode (press i).
syntax on

:set hlsearch

:set ignorecase

:set smartcase

:set number

vimrc file data

  • press “ESC” to come in normal mode.
  • type :wq which means save and exit.

that’s it.

Now just have a look how it look like after setting .vimrc file, Does it look pleasant?

after vimrc file

I hope you liked the vim tutorial.


Bonus tip 😉

In terminal type vimtutor it will give you vim tutorial.

In vim editor type “/help” to get help vim in detail.

For more reference, you can also visit the main site of vim.

Leave a Comment