Linux+: Linux Shell 25 – vi Editor (Part 1)

J

Jarret W. Buse

Guest
Linux+: Linux Shell 25 – vi Editor (Part 1)

Linux comes with a command-line editor call 'vi' for “VIsual editor”. On most systems you will find that vi is replaced with 'Visual IMproved editor' or 'vim' The vi editor is an editor in the terminal shell. The editor can be somewhat difficult at first until you get use to the shortcut keys to manipulate the data file being edited.

First, to open a file in vi, you type 'vi filename' at the shell prompt. The filename will, of course, include the path to the file as well as include the name of the desired file.

If the system should crash while editing a file, you can use the -r option to recover it.

The syntax is as follows:

vi options filenames

The options are:

  • -b – binary mode is used
  • -C – uses vi compatability mode instead of vim
  • -g – uses a GUI mode if gvim is available on the system
  • -h – displays help information for vi
  • -i filename – specifies the default configuration file, the default is ~/.vminfo
  • -r – recovers a lost file due to a system crash while file was being edited. File is recovered from .swp file
  • -n – does not create .swp file so recovery is not allowed
  • -M – File modification with no write
  • --noplugin – prevents plugins from being loaded
  • --version – shows version of vi(m) currently installed
The options are used during the loading of the program and the file to edit. Once vi is loaded there are many key and key combinations used to move and manipulate the editor:

  • h – moves the cursor left
  • j – moves the cursor down
  • k – moves the cursor up
  • l – moves the cursor right
  • spacebar – moves cursor right one space
  • - (minus) – moves cursor down one in the first column
  • + (add) – moves the cursor up one in the first column
  • ctrl+d – moves down half a screen
  • n ctrl+d – moves down 'n' lines, but the default is half a screen
  • ctrl+u - moves up half a screen
  • n ctrl+u – moves up 'n' lines, but the default is half a screen
  • ctrl+f - moves forward half a screen
  • n ctrl+f – moves forward 'n' lines, but the default is half a screen
  • ctrl+b - moves back half a screen
  • n ctrl+b – moves back 'n' lines, but the default is half a screen
  • ctrl+y - moves forward one line
  • n ctrl+y – moves forward 'n' lines
  • ctrl+e - moves back one line
  • n ctrl+e – moves back 'n' lines
  • M – moves to middle of page
  • H – moves to top of page
  • L – moves to bottom of page
  • W – moves to next word (searches for whitespace)
  • w – moves to next word (first non-alphanumeric character)
  • 5w – moves ahead 5 words
  • B – moves back a word (searches for whitespace)
  • b – moves back a word (first non-alphanumeric character)
  • 5b – moves back 5 words
  • E – moves to end of a word (searches for whitespace)
  • e – moves to end of a word (first non-alphanumeric character)
  • 5e – moves to the end of the fifth word
  • 0 (zero) – move to beginning of current line
  • :# - move cursor to end of the # line. For example, :30 moves cursor to line 30
  • $ - moves cursor to end of current line
  • ) - moves cursor to end of sentence
  • ( - moves cursor to beginning of sentence
  • } - moves cursor to end of paragraph
  • { - moves cursor to beginning of paragraph
  • G – moves cursor to the end of the file
Now that you can move the cursor around, what about making edits? These are the edit keys:
  • i – causes an insert mode (use ESC key to terminate mode)
  • A – appends at the end of the line – insert mode (use ESC key to terminate mode)
  • o – inserts a new line below current line (use ESC key to terminate mode)
  • ESC – terminates any insert mode
  • u – undo last change
  • U – undo all changes for the line
  • dd – delete line
  • #dd – delete # lines. For example, 3dd will delete 3 lines
  • D – delete from cursor to end of line
  • C – delete from cursor to end of line and insert new text (use ESC to terminate insert mode)
  • dw – deletes word
  • #dw – deletes # of words
  • d) – deletes to end of sentence
  • d$ - delete from cursor to end of line
  • d- - deletes current and previous line
  • x – deletes character at cursor
  • X – deletes character before cursor
  • Y – places current line in buffer (copy)
  • p – places characters in buffer after current line
  • P – places characters in buffer before current line
  • r – replaces characters
  • R – overwrite characters from cursor
  • s – substitute one character, but continue to insert
  • S – substitute entire line and insert at beginning of line
  • J – join current and next line into one
  • ~ - change case of current character
  • ctrl+a – increment number under cursor
  • ctrl+x – decrement number under cursor

The options and commands listed here are enough to get you into vi and using it. Other commands do exist within the above criteria, but the list can be too extensive to get into. The coverage of vi on the Linux+ certification exam does not go too deep into the editor. If you use the editor and become use to the commands you should understand them enough to answer the questions. Another article will follow with more commands in other areas of the editor.
 

Attachments

  • slide.jpg
    slide.jpg
    49.4 KB · Views: 16,411

Members online


Top