|

Basic Vim Commands to start work with Vim Editor

Before we start, let’s know about the Vim modes. Vim has two modes:

  • Command Mode: Press ESC for command mode
  • Insert Mode: Press i for insert mode

Command mode

When you start Vim, you are placed in Command mode. In the command mode, you can move across the screen, delete text, copy text and give commands to the editor to get things done.

Insert mode

If you want to write something on a file just like normal text editor, you have to enter the insert mode.

  • i – New text will appear before the cursor
  • a – New text will appear after the cursor
  • I – New text will appear at the beginning of the current line
  • A – Next text will appear at the end of the current line
  • o – A new line is created after the current line
  • O – A new line is created before the current line

Going back to command mode in Vim

You start in command mode, you go in to insert mode. If you want to move back to the command mode, press Esc (escape) key.

Vim Commands how should know

Vim CommandDescription
x or DelDelete the unwanted character
XDelete character is backspace mode
uUndo the last the command
UUndo the whole line
CTRL-RRedo changes
/<search_term>Search and then cycle through matches with n and N
yyCopy a line
ddDelete the line
pputs the previously deleted text after the cursor
(Type dd to delete the line and store it in a Vim register. and p to put the line. works like a cut option)
Aappend text at the end
Esc + :wSave changes
Esc + :wq or
Esc + ZZ
save and exit vim
Esc + :q!Force quit Vim discarding all changes
cedeletes the word and places you in Insert mode
%to find a matching ),], or }
:!to run the shell commands like :!dir, :!ls
vstarts visual mode for selecting the lines and you can perform an operation on that like d delete
:rFilename will insert the content into the current file
Rto replace more than one character. enters Replace mode until <ESC> is pressed.
y – operatorto copy text using v visual mode and p to paste it
ecommand moves to the end of a word.
ctrl-wto jump from one window to another
type a command :e and press ctrl+Dto list all the command name starts with :e and press tab to complete the command

Moving around in Vim Editor

When you are in Vim, you cannot use your mouse to click at in point in the screen like normal text or code editors. This is why you need to know the movement commands to a certain line or word or position in Vim.

I use arrow keys for moving around in Vim. It works well even when you are in insert mode in Vim. That’s the standard way of moving around most of the newer generation is accustomed to.

  • H – Move to the top of the screen. Note that it doesn’t always mean moving to the first line in the file. It’s just for moving to the top of the visible screen.
  • L – Move to the bottom of the screen. Note that it doesn’t always mean moving to the last line in the file. It’s just for moving to the bottom of the visible screen.
  • [[ or gg – Move to the first line of the file.
  • ]] or G – Move to the last line of the file.
  • nG – Move to line number n. Note that you won’t see anything on the screen while typing the line numbers.
  • 2w – move the cursor two words forward.
  • 0 (Zero) – move to the start of the line.

Tip: You can display line numbers in Vim by going into the command mode and typing :set number

Undo Commands in Vim Editor

  • u – undo a change. You can press it several times for multiple undo actions.
  • U – undo the whole line.
  • Ctrl+r – redo a change. You can press it several times for multiple redo actions.

Copy Paste Command in Vim Editor

When you are in command mode, you can either use your Linux terminal shortcuts for copying the text or the following key combinations for copying text:

  • yw – Copy word. Actually, it copies from the current cursor position till the end of the current word plus the white space after it.
  • yy – Copy current line.
  • y$ – Copy from the current cursor position till the end of the line.
  • yG – Copy from the current cursor position till the end of the file.

Suppose you used one of the delete commands discussed below. The deleted text is placed into the buffer. You can paste this text from the buffer using these two paste commands:

  • p – Paste the contents of the buffer before the cursor position

Delete Commands in Vim Editor

  • x – Delete the character at current cursor position just like a delete key
  • X – Delete the character before the current cursor position like a backspace key. Note that the backspace key itself doesn’t work in Vim.
  • dw – Delete word. Actually, it deletes from the current cursor position till the end of the current word plus the white space after it.
  • dd – Delete the current line.
  • d$ – Delete from the current cursor position till the end of the line.
  • dG – Delete from the current cursor position till the end of the file.
  • 2dd – Delete to line .number can be changed for deleting the number of consecutive words
  • dw – move the cursor to the beginning of the word to delete that word
  • 3e – to move the cursor to the end of the third word forward.
  • d2w – which deletes 2 words .. number can be changed for deleting the number of consecutive words like d3w

Note: There are no cut commands in Vim because when you delete something, the deleted text is placed into the buffer. In other words, delete commands are the cut commands.

Searching for text in Vim Editor

Finding a particular text is an important function of a text editor.

  • /<search_term> – search for text in the file. It will do a forward searching for the searched term from your cursor position.
  • ?<search_term> – to perform a backward search
  • n – jump to the next position, if there is more than one match for your search text
  • N – If you want to go back to the previous match
  • Basically, with n and N you can cycle through all the matches.
  • \c – to run a case-insensitive search in Vim. Example: /\cMy_Search

Search and Replace in Vim

Vim provides a substitute command (:s) for searching and replacing text. It relies heavily on regular expressions (regex) for search and replace.

You can use it in the following fashion:

  • :%s/foo/bar/ – replace all the ‘foo’ with ‘bar’ in the entire file. only the first match is replaced.
  • :%s/foo/bar/g – The ‘g’ at the end is responsible for extending the search and replace function on all the matches.
  • :s/foo/bar/g – will do the exact same function as above but only in the current line instead of the entire file.
  • :%s/foo/bar/gi – By default, the search is case sensitive. To make it case insensitive, you can use the flag ‘i’ along with g.
  • :%s/foo/bar/gci – With this confirm flag ‘c’, Vim will ask you to confirm if you want to perform a replace on each match.

In confirmation mode, you’ll be presented with the following option:

  • y – YES, replace this match
  • n – NO, don’t replace this match and move to the next one
  • a – Replace ALL matches
  • q – QUIT without replacing any match
  • l – Replace this match and quit as if it was the LAST match
  • ^E – Scroll the screen up
  • ^Y – Scroll the screen down

Saving and Quitting Commands in Vim

To save or exit Vim, you must enter the command mode first by press Esc key. And then you can use the following options:

  • :w – Save the changes but don’t quit.
  • :w – TEST (where TEST is the filename you chose.) Save the file.
  • :wq – Save and quit
  • :q – Just Quit (if you have unsaved changes, you’ll see this warning: E37: No write since last change (add ! to override))
  • :q! – Force quit (it will discard any unsaved changes)

Similar Posts

Leave a Reply

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

One Comment