Skip to content

Latest commit

 

History

History
153 lines (135 loc) · 6.43 KB

File metadata and controls

153 lines (135 loc) · 6.43 KB

VI

  • u - undo
  • a - move cursor to start of line and enter insert mode
  • A - move cursor to end of line and enter insert mode
  • i - enter insert mode
  • esc - enter command mode
  • :q! - quit without saving
  • :wq! - quit with saving
  • o - insert new line below current line
  • O - insert new line above current line
  • ^ - move to starting of line
  • $ - move to end of line
  • Deleting Text
    • x The current character.
    • 3x The current character and the next two characters.
    • dd The current line.
    • 5dd The current line and the next four lines
    • dW From the current cursor position to the beginning of the next word.
    • d$ From the current cursor location to the end of the current line.
    • d0 From the current cursor location to the beginning of the line.
    • d^ From the current cursor location to the first non-whitespace character in the line.
    • dG From the current line to the end of the file.
    • d20G From the current line to the twentieth line of the file.
  • Yanking/Copy Command
    • yy The current line.
    • 5yy The current line and the next four lines.
    • yW From the current cursor position to the beginning of the next word.
    • y$ From the current cursor location to the end of the current line.
    • y0 From the current cursor location to the beginning of the line.
    • y^ From the current cursor location to the first non-whitespace character in the line.
    • yG From the current line to the end of the file.
    • y20G From the current line to the twentieth line of the file.
  • p - Paste
  • Joining Lines: If we place the cursor on line 3 and type the J command (not to be confused with j, which is for cursor movement), here's what happens:
The quick brown fox jumped over the lazy dog. It was cool.
Line 2
Line 3 Line 4
Line 5

Search-And-Replace

Searching Within A Line

The command fa would move the cursor to the next occurrence of the character a within the current line. After performing a character search within a line, the search may be repeated by typing a semicolon ;.

Searching The Entire File

To move the cursor to the next occurrence of a word or phrase, the / command is used. Type / then type the word or phrase to be searched for, followed by the Enter key. After pressing Enter key the cursor will more to the location of word, then press n command to continue searching the same word further.

Global Search-And-Replace

vi uses an ex command to perform search-and-replace operations. To change the word “Line” to “line” for the entire file, we would enter the following command: File

The quick brown fox jumped over the lazy dog. It was cool.
Line 2
Line 3
Line 4
Line 5

Command

:%s/Line/line/g
  • : The colon character starts an ex command.
  • % Specifies the range of lines for the operation. % is a shortcut meaning from the first line to the last line. Alternately, the range could have been specified 1,5 (since our file is five lines long), or 1,$ which means “from line 1 to the last line in the file.” If the range of lines is omitted, the operation is only performed on the current line.
  • s Specifies the operation. In this case, substitution (search-and-replace).
  • /Line/line/ The search pattern and the replacement text.
  • g This means “global” in the sense that the search-and-replace is performed on every instance of the search string in the line. If omitted, only the first instance of the search string on each line is replaced.

We can also specify a substitution command with user confirmation (for each replacement). This is done by adding a c to the end of the command. For example:

:%s/line/Line/gc

This shows below line for each replacement.

replace with Line (y/n/a/q/l/^E/^Y)?
  • y Perform substitution
  • n Skip this instance of the pattern
  • a Perform the substitution on this and all subsequent instances of the pattern
  • q or Esc Quit substituting
  • l Perform this substitution and then quit. Short for “last.”
  • Ctrl-E, Ctrl-Y Scroll down and scroll up, respectively. Useful for viewing the context of the proposed substitution.

Editing Multiple Files

vi file1 file2 file3...

To switch from one file to the next, use this ex command: :n
To move back to the previous file use: :N

While we can move from one file to another, vi enforces a policy that prevents us from switching files if the current file has unsaved changes. To force vi to switch files and abandon your changes, add an exclamation point (!) to the command.

We can view a list of files being edited with the :buffers command. Doing so will display a list of the files at the bottom of the display:

:buffers
  1 #    "Bookmark2.txt"                line 5
  2 %a   "Bookmark1.txt"                line 1
Press ENTER or type command to continue

To switch to another buffer (file), type :buffer followed by the number of the buffer you wish to edit.

:buffer 1

Opening Additional Files For Editing

It's also possible to add files to our current editing session. The ex command :e (short for “edit”) followed by a filename will open an additional file.

vi Bookmark2.txt

Add another file in editing like below

Bookmark1.txt  Bookmark2.txt
:e Bookmark1.txt

You cannot switch to files loaded with the :e command using either the :n or :N command. To switch files, use the :buffer command followed by the buffer number.

Copying Content From One File Into Another

  • Move to first file as: :buffer 1
  • Move the cursor to the first line, and type yy to yank (copy) the line.
  • Move to second file as: :buffer 2
  • Paste the line using p

Inserting An Entire File Into Another

Move the cursor to the any line, then enter the following ex command:

:r Bookmark1.txt

While in Bookmark2.txt we insert Bookmark1.txt after the line of cursor.

Saving Our Work

  • In command mode, typing ZZ will save the current file and exit vi.
  • ex command :wq will combine the :w and :q commands into one and save and quit.
:w foo1.txt

The :w command may also specify an optional filename. if we were editing foo.txt and wanted to save an alternate version called foo1.txt, we would enter the following:

:w foo1.txt

Note: While the command above saves the file under a new name, it does not change the name of the file you are editing. As you continue to edit, you will still be editing foo.txt, not foo1.txt.