25 essential Vim commands for navigation and editing

Vim commands for navigation and editing

Overview

Vim is an extremely powerful text editor and can be especially useful when editing larger files. It can speed up/ease up the process. We’ll be looking at some essential Vim commands for navigation and editing text.This article focuses on every day Vim usage, meaning – to help out or increase productivity for a individual or to jump start their journey working in Vim. Vim has multiple working modes and in this post we’ll cover – Normal, Command, Visual and Insert mode, which are modes for every day usage.

  • Normal mode is a default mode when you open Vim and from it you access other modes.
  • Insert mode is the text editing mode – adding and removing text by typing.
  • Visual mode is used for text selection and bulk editing.
  • Command mode is used to run Vim global commands(Vim preferences), text manipulation, pattern search and so on.

Insert mode – text editing

To enter the Insert mode, hit the letter “i” on your keyboard. Once in Insert mode, you can add and remove text like in any other text editor.

vim commands

“Ctrl+p” and “Ctrl+n” – auto completion menu

vim commands

Insert mode also supports standard keyboard shortcuts.

Standard navigation keyboard shortcuts:

  • “Home”,“End” keys – cursor jump to the beginning and to end of line,
  • “PgUp” and “PgDn” keys – Jump to beginning and to the end of screen(not file jumps, it’s terminal screen jumps)
  • “Ctrl+arrow keys” (left and right) – Jump to next and previous word
  • “Ctrl+Shift+c” and “Ctrl+Shift+v” – copy and paste while in Insert mode.

To exit the Insert mode, hit the “Esc” key and you’ll be again in Normal mode.

Normal mode commands

In Normal mode we can navigate through text with these commands:

  • “0” – move cursor at the beginning of the line
  • “$” – move cursor at the end of line
  • “gg” – move cursor at the beginning of file
  • “GG” – move cursor at the end of file
  • “e” – set cursor at end of the current word
  • “b” – set cursor at the beginning of the previous word
  • “w” – set cursor at the beginning of the next word
  • “H” – move the cursor to the start of the displaying screen.
  • “M” – move the cursor to the middle line of the screen.
  • “L” – move the cursor to the end line of the screen.
  • “ctrl+f” – Jump forward(down) one full screen.
  • “ctrl+b” – Jump backwards one(up) full screen
  • “ctrl+d” – Jump forward (down) a half screen
  • “ctrl+u” – Jump back (up) one half screen

Text deletion(Normal mode commands as well)

  • “dd” – Deletes a line of text
  • “dw” – Deletes a word
  • “D” – Deletes everything from where your cursor starting position is to the end of the line
  • “d0” – Deletes everything from where your cursor starting position is to the beginning of the line
  • “dgg” – Deletes everything from where your cursor starting position is to the beginning of the file
  • “dG” – Deletes everything from where your cursor starting position is to the end of the file

Undo and redo

  • “u” – run undo
  • “Ctrl+r” – run redo

Select, copy and paste text in Vim

Now let’s review text selection, copy, paste and delete process.

For this need we need to enter visual mode. You access it with “v” and “V” keys.

  • “V” – selects the entire line
  • “v” – selects letter by letter or character by character with the left, right arrow keys

Once the text is selected we can do the following:

  • “d” – delete the selection
  • “y” – copy the selection
  • “p” – paste the selection

Copy the entire line, a word or multiple lines:

  • “yy” – copies the entire line
  • “2yy” – copies the next 2 lines from the cursor starting position
  • “yw” – Copies a word
  • “y$” – Copies from where your cursor starting position to the end of a line

Search in Vim

To start a search, in Normal mode hit the “/” character and then just type what you need to find and hit “Enter”. Then once you get a hit, with the “n” letter you move to the next search hit and with “N” you move to the previous hit.

vim commands

Search and replace in Vim

Search and replace is run in command mode. We tell Vim we’ll run a command with the “:” character.

So, for a search and replace command it will look like this:

  • “:s/oldtext/newtext” and then hit “Enter”

vim commands

But this command will search and replace only one line, so here are command variations to search and replace the entire text file:

  • “:s/Vim/VIM/g”
    Find each match of ‘Vim’ (in the current line only), and replace it with ‘VIM’.
  • “:%s/Vim/VIM/g”
    Find each match of ‘Vim’ (in all lines), and replace it with ‘VIM’.
  • “:%s/Vim/VIM/gc”
    Change each ‘Vim’ to ‘VIM’, but ask for confirmation first.
  • “:%s/\<Vim\>/VIM/gc”
    Change only whole words exactly matching ‘Vim’ to ‘VIM’; ask for confirmation.
  • “:%s/Vim/VIM/gci”
    Change each ‘Vim’ (case insensitive due to the i flag) to ‘VIM’; ask for confirmation.
  • “:%s/Vim/VIM/gcI”
    Change each ‘Vim’ (case sensitive due to the I flag) to ‘VIM’; ask for confirmation.

Save changes and exit Vim

These commands are also run from command mode.

“:q” – quit Vim without saving any changes

“:q!” – quit and discard any made changes(the “!” in command mode actually overrides/ignores any prompts that may pop up which can block from command running)

“:w” – write/ save changes

“:wq” – save and quit Vim

vim commands

Learn using Vim from terminal

You can also learn Vim from the terminal itself. In most Linux distributions which comes with Vim pre-installed, also comes the application – vimtutor, which is a Vim manual in which also covers basic, everyday usage, instructions to perform operations and list Vim commands and what they do.

 

vim commandsSummary

To summarize the article we covered some of the essential Vim commands how to navigate in Vim, how to edit text and how to perform some basic commands – search text in Vim, make a selection of a text, do copy and paste of the text, run search and replace  and how to save changes and quit.

Working in Vim may seem intimidating, but it doesn’t take much time to get used to it and it may speed up and increase productivity.

Thank you for your time…

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *