DEV Community

Waqar Ahmed
Waqar Ahmed

Posted on • Updated on • Originally published at waqar.org

9 Essential commands to unlock the power of vim

Bram Moolenaar , the creator of vim, cloned and improved vi and released it in 1991. The name vim came from "vi imitations", later changed to "Vi improved". Gradually it became the most popular text editor among Linux users. In 2019 Stackoverflow developers survey vim was the fourth most popular text editor.

In their last survey, Stackoverflow revealed over 2.1 million people visited the ' How do I exit the Vim editor? ' question on Stack Overflow. So I am writing some top googled vim commands and the general know-how required to use vim.

Vim modes

Out of so many, the most frequently used are "command-line mode", "normal mode" and "insert mode".

Normal mode

You are in normal mode when you open a file. To go back to the "normal mode", press the Esc key.

Command-line mode

To run a command, go to the command-line mode by typing colon :.

Insert mode

Get into "insert mode" by pressing i in "normal mode".

Vim commands

Press Esc key to enter "normal mode" then press colon : to enter "command-line mode". At the bottom left corner of vim, a colon : appears. Now you can type any command and press the Enter key to execute the command.

vim command to open a file

Type vim preceded by file name to open a file in the vim text editor.

vim command to save and exit

Press the Esc to get out of "insert mode". Press colon : to get into the "command-line mode" and type wq to write and quit the file. If you have not made any changes to the file, you can exit by q command.

  • :q to quit
  • :wq to write and quit
  • :q! to quit without writing
  • :x to save and quit (similar to :wq)
  • :exit save and exit (similar to :x)

vim command to edit file

Press the i key in "normal mode" to start editing the file. Move around with arrow keys or use these keys:

  • l move right
  • k move up
  • j move down
  • h move left

vim command to go to the end of the file

Type capital G or shift+g to go to the end of the file in vim.

vim command to go to top of file

Type gg or 1G to go to the beginning of the file.

vim command to delete all lines

Use letter d to delete. Go into "command-line mode" by pressing : and type %d to delete all lines. You can also use range 1,$d. Just like regular expressions $ sign represents the end of the file.

vim command to show line numbers

In "command-line mode" type, set numbers or short command set nu to display line numbers of the file.

vim command to replace string

Type search string followed by the replacement string %s/search/replace/g.

vim command to delete lines with pattern

Inside "command-line mode" type, g/pattern/d

Run the :help command to check usage documentation. Add a query after :help to get specific help. Vim comes with a separate tutor program to teach vim usage. Run vimtutor in the terminal for this 20 minutes practice tutorial. Use the comments section to mention any other essential commands that I have missed.

Top comments (0)