VIM For Beginners: An Introductory Reference

·

NOTE: This article assumes the reader has an introductory knowledge of the Linux ecosystem.

Vim is a powerful text editor for Linux which is run entirely from the terminal. As it does not have a GUI (graphical user interface), it allows manipulation of text-based files without having to devote resources to a desktop environment—a boon for dedicated servers. Vim can be intimidating to new users due to a perceived steep learning curve, however the majority of the difficulty is in adapting to the CLI (command-line interface) format as opposed to more traditional GUI formats.

To get started with Vim, you will need to open a terminal window (for those new to Linux, the Windows counterpart to a terminal is the Command Prompt) on your machine and type “vim” followed by the name of the file you want to edit or create. For example, to edit a file called “file.txt”, you would type vim file.txt. If “file.txt” does not exist, the file will be created in the current directory in the terminal.

Once you are in the Vim editor, you will be in “normal” mode. In normal mode, you can navigate through the text file using the arrow keys. To enter text, you will need to press i to enter “INSERT” mode. While in insert mode, you can type text as you would in any other text editor. To return to normal mode, press the Escape key.

There are many other functions in Vim that can be accessed from normal mode. Below is a categorized list of case-sensitive commands:

Navigation

  • k – Move the cursor up one line (Same as Up Arrow)
  • h – Move the cursor one character to the left (Same as Left Arrow)
  • j – Move the cursor down one line (Same as Down Arrow)
  • l – Move the cursor one character to the right (Same as Right Arrow)
  • 0 (NumLine) – Move the cursor to the beginning of the line
  • $ – Move the cursor to the end of the line
  • w – Move the cursor forward one word
  • b – Move the cursor backward one word
  • G – Move the cursor to the end of the file
  • gg – Move the cursor to the start of the file
  • `. – Move to the location of the last change

Editing/Deleting

  • dw – Delete a word (Cursor must be at the first letter of the word)
  • d0 – Delete all characters between the beginning of the line and the cursor
  • d$ – Delete all characters between the end of the line and the cursor
  • dgg – Delete all characters between the beginning of the file and the cursor
  • dG – Delete all characters between the end of the file and the cursor
  • u – Undo the last change
  • Ctrl R – Redo the last undone change
  • /yourtext – Search for a case-sensitive string, starting forwards from the cursor.
  • n – Move to the next instance of found text in a search. This will wrap
  • N – Move to the previous instance of found text in a search. This will wrap
  • ?yourtext – Search for a case-sensitive string, starting backwards from the cursor.
  • %s/yourtext/replacedtext/g – Search and replace all instances of ‘yourtext’ with ‘replacedtext’.
  • %s/yourtext/replacementtext/gc – The same as above, but it confirms before replacing each instance of text.

Copy/Paste

  • y – Copy highlighted text
  • P – Paste copied text on the cursor.
  • p – Paste copied text to the right of the cursor.
  • v – Highlight text. Use hjkl/arrow keys to highlight one additional character per key press.
  • V – Highlight text. Use hjkl/arrow keys to highlight one additional line per key press.

Save/Quit

  • :w – Save over the existing file. If you wish to save to a different file (or make a new one), add yourfilename.txt. Example: :w newfile.txt
  • :q – Quit Vim. If you have not saved, it will prompt you to override.
  • :q! – Quit Vim and ignore all warning prompts.
  • ZZ – Quit Vim. If possible, this will also save changes made to the currently open text file.

As a rule of thumb, do not use :q! unless you are absolutely confident you do not need any information stored in the active file.

Leave a Reply

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