vi Editor Basics

vi, emacs, nano, pico, joe… no doubt you have a personal favorite, and it may not even be listed there. One sure fire way to get into a heated discussion with another GNU/Linux user is to boldly claim your personal favorite editor is the best.

In this post, I’m going to be covering the basics of the vi editor. Lets jump right in why don’t we?

To start the vi editor, simply issue the command followed by the filename you want to edit. If the file doesn’t exist, it will be created.

vi ~/testfile

Assuming you don’t have a file named testfile in your home directory, a new file will be opened for editing and you will see a screen similar to this:

Just opened file

Just opened file

There are two “modes” in vi. The first is command mode, the second is input mode. When you first open vi, you will be in command mode. As the names suggest, command mode will accept commands like cursor movement, screen movement, find, change, replace, write, quit, save, etc. Input mode allows typed text to be added to the file. In order to switch out of input mode, hit the [ESC] key.

On your newly opened file, you can start entering text by using one of the four commands listed below to enter input mode:

  • a     # appends after the cursor
  • i      # inserts before the cursor
  • o     # opens a line for editing below the cursor
  • O     # opens a line for editing above the cursor

Hit [i], then type in the following:

Hello world!

Your file will now look like this:

Hello world!

Hello world!

To get out of input mode, hit [ESC]. You will now be back in command mode. From here, you have four command options to save, quit, quit without saving, etc.:

  • :w     # this writes the changes to the originally supplied filename (~/testfile)
  • :q     # this quits the vi editor (see note below)
  • :wq     # this writes the changes to the originally supplied filename (~/testfile) then quits the vi editor (note that you can also use the shortcut command, ZZ, which accomplishes the same thing as :wq)
  • :q!     # this quits the vi editor without saving changes (see note below)

Note: if you attempt to quit (:q) out of the vi editor after making modifications to the file that haven’t been saved, you will get a prompt that looks like this:

Quit error

Quit error

If you want to quit without writing the changes you made since your last save, use the quit override command (:q!), otherwise, save then quit (:wq).

Now that you know the [very] basics of inputing text and file management, I’ll show you how to navigate a file. For practice, lets create a [longer] file and practice on that. I’m just going to print the process tree to the file we were working on, overwriting anything that may have been present.

pstree > ~/testfile

Now when you open testfile with:

vi ~/testfile

You will see a screen with much more information, with the number of lines and characters printed at the bottom next to the filename.

pstree file

pstree file

To move your cursor a single space in one direction, you use the following four commands (remember you have to be in command mode for them to work; hit [ESC] if you are in input mode for some reason):

  • j     # moves the cursor down a single line
  • k     # moves the cursor up a single line
  • l     # moves the cursor forward a single character
  • h     # moves the cursor backward a single character

Well that’s all well and good, but the file is 75 lines long! What if you want to move faster than a single character at a time? No problem there, you can then use the following commands to speed things up:

  • w     # moves the cursor forward a single word
  • b     # moves the cursor backward a single word
  • [CTRL] d     # scrolls down half a screen
  • [CTRL] u     # scrolls up half a screen
  • [CTRL] f     # scrolls forward one screen
  • [CTRL] b     # scrolls back one screen
  • H     # moves the cursor home (left-most position on the highest) line on the screen
  • M     # moves the cursor to the middle line on the screen
  • L     # moves the cursor to the lower line on the screen

The last thing I want to cover in the how-to basics are undo and deletion commands. The undo command is very straight forward:

  • u     # undoes the last change

The deletion commands are as follows:

  • dd     # delete the current line (use 5dd to delete 5 lines, 20dd to delete twenty lines and so forth)
  • dw     # delete the remainder of the current word (use d5w to delete 5 words, d20w to delete twenty words and so forth)
  • db     # delete the previous word (use d5b to delete 5 words, d20b to delete twenty words and so forth)
  • d)     # delete until the end of the current sentence
  • D     # delete to the end of the line
  • x     # delete the current character

You may be feeling a little overwhelmed right now, but you shouldn’t!

Start with the basics and practice as you go along. If you learn one or two new things each time you open the editor, you will be moving along quite well

  • Start with opening and closing files, which will force you to learn the quit, :q, :q! and write, :w, ZZ, commands.
  • Insertion commands will come next, probably just i and a at first.
  • Then a few of the movement commands like j,k,h, and l.
  • Maybe pickup two of the delete commands next, dd and D, and you will be set with very basic editing.

Over time, teach yourself the remainder of the basic commands, then move on to the advanced commands where you will be able to do things like word substitutions, yank/put (copy/paste), regular expressions and search/replace.

Tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published.

Protected by WP Anti Spam