Vi is a text editor created for Unix users. The original code for vi was written by Bill Joy in 1976, as the visual mode for a line editor called ex that Bill had written with Chuck Haley. The ex 1.1 editor was released as part of the first BSD Unix release in 1978. With the release of the 2.0 version of ex, in May 1979, the editor was installed under the name “vi”. Today this is commonly referred to as vi or vim editor.

Vim is an improved version of Bill Joy’s vi. It was written by Bram Moolenaar and was released in 1991. Vi is commonly used by Linux users to edit configuration files and also by developers working over shell scripts or other popular coding languages.

Here are some of the reasons why you should familiarize yourself with vi:

  • vi is the closest to universal you’ll ever get
  • It’s plagued with useful shortcuts
  • It’s free
  • It can be used to write documents, code, shell scripts, you name it
  • It has a very sophisticated search system, including regex pattern matching, among other things

In today’s post, we will show you how to use vi more effectively.

vi Editor Basics:

To open an existing file such as test.sh, you will have to use the command- vi test.sh. If the file does not exist, then the file will be created. By default, when you open a file using vi editor, the editor will be in view-only mode.

Non-editable file

In order to edit this file, you need to press “i” on your keyboard. This will allow you to edit the file.

Insert

To save a file, press Esc and type :wq. This will save the file and exit the editor. You can do the same by typing Esc and “ZZ

Save

In case you want to quit without saving changes, you can use Esc and type :q!

To enter any command, you first need to get to the command mode by pressing Esc. 

These are some of the most basic shortcuts for vi

CURSOR MOVEMENT OPTIONS:

These are shortcuts which help you position your cursor within the file.

  • h – moves the cursor to the left. Similar to backspace
  • j – moves down
  • k – moves up
  • l – moves right. Similar to a space bar
  • Return key/Enter Key – Takes you to the beginning of the next line
  • ^ –  takes cursor to the first non-blank column of the current line
  • w – moves to the beginning of the next word or a punctuation mark
  • W – moves past the next space
  • b – moves to the beginning of the previous word or a punctuation mark
  • B – moves to the beginning of the previous word and ignores punctuation mark
  • e – End of next word or punctuation mark
  • E – end of next word and ignores punctuation mark
  • H – moves the cursor to the top of the screen
  • M – moves the cursor to the middle of the screen
  • L – moves the cursor to the bottom of the screen

SCREEN MOVEMENT OPTIONS:

These shortcuts are useful while editing a large file.

  • G – moves to the last line in the file
  • xG – moves to line x within the file
  • z+ – moves current line to the top of the screen
  • z- – moves current line to the bottom of the screen
  • ^F – move forward one screen. Similar to page down
  • ^B – move backward one line
  • ^D – move forward one half screen
  • ^U – move backward one half screen

Insert Options:

These shortcuts help you to insert or append within a file.

  • r – replaces the character highlighted by the cursor with the next typed character
  • R – keeps replacing the characters until escape is hit. Used when more than one character needs to be replaced
  • i – Insert before the cursor position
  • a – append after the cursor
  • A – append at end of the line
  • O – provides empty line above the cursor to add any content

DELETE OPtions:

These shortcuts will help you quickly delete contents within a file.

  • x – deletes a character at the cursor position
  • dd – deletes complete line under cursor position
  • dw – deletes word under cursor position
  • db – deletes word before the cursor position

CODING OPTIONS:

  • yy – Also referred as Yank, it can be used to copy lines and then paste by using the put (p) command. Precede with a count to copy multiple lines
  • P – Put command, Brings back previous deletion or yank of lines or words or characters. Places this before the cursor position
  • p – Put command. Places this after the cursor position
  • . – A dot is used to repeat the last command
  • u – undoes the last command
  • U – undoes all executed commands on one line
  • xp – Character swap. Used to swap two characters
  • J – Joins current line with next line
  • ^G – displays current line number
  • % – If at one parenthesis, will jump to its end or start matching parenthesis
  • mx – Marks current line with character x
  • ‘x – Finds line marked with character x
  • :$ – Move to the last line of the file
  • :# – The command followed by a number will move to the line number which is specified
  • :w – saved the current file without quitting
  • :r filename – copies contents of the file mentioned in the command and places it in the current file after the cursor position 
  • ? – Command is used with a word to be searched. Finds a word going backward
  • / – Command is used with a word to be searched. Finds a word going forward
  • f – finds a character on the line under the cursor going forward
  • F – finds a character on the line under the cursor going backward

These commands should be more than enough to get you started with vim.

Once you get comfortable with it, you’ll never look back!

Facebooktwittergoogle_plusredditpinterestlinkedinmail