# vi / Vim Editor¶

The vi editor is a basic editor which is available on nearly any Unix system within the core installation. On the first look the editor with it's control keys looks cumbersome. But after learning how to use it you will find that all things are easy done with only a few key strokes.

The first thing to know is that it uses different modes. You start in the command mode and to edit text you should switch to the insert mode. The status bar on the bottom line shows if a special mode is active and will be used for special commands.

Note

Vim is an advanced version of vi adding colorful syntax highlighting, unlimited undo and an optimized GUI. Often a shim is installed to link the vi call directly to Vim. That makes it a full replacement.

## General Keys¶

This mode is active after starting vi and regular keys are linked to special commands so you can't just start typing. The commands consist of one or two simple key strokes without a Enter after it.

Keys Action
Esc Switch to command mode.
I Switch to insert mode before cursor.
Shift+I Switch to insert mode at beginning of current line.
A Switch to insert mode after cursor.
Shift+A Switch to insert mode at the end of current line.
O Switch to insert mode as new line above cursor.
Shift+O Switch to insert mode as new line below cursor.
Shift+R Switch to replace mode in which the existing text is overwritten.
Shift+Z Shift+Z Save file and exit vi (like :wq).
++:++ Give a execution command in the bottom line.

While in command mode a lot of commands help to easy go through the file.

Keys Action
Left or H Move cursor position one character left.
Down or J or + Move cursor position one character down.
Up or K or - Move cursor position one character up.
Right or L Move cursor position one character right.
++pos1++ or 0 or ++^++ Move cursor to the start of the current line.
End or ++$++ Move cursor to the end of the current line. W Move forward one word (alphanumeric characters make up words). Shift+W Move forward one word (white space separates words). E Move forward one word to the end (alphanumeric characters make up words). Shift+E Move forward one word to the end (white space separates words). B Move backward one word (alphanumeric characters make up words). Shift+B Move backward one word (white space separates words). Shift+G Go to the end of the file. <num>G like 4 0 Shift+G Go to a specific line (here line 40). <num><dir> like 4 Right Type a number and then a direction key to move multiple steps (here 4 times right). F <char> Find the character corresponding to the next keystroke typed. Move the cursor to the next occurrence of that character (on the current line only) Shift+F <char> Same as F but movement is backwards. ++;++ Repeat the last F or Shift+F command. Shift+H Move cursor to the top line of the screen, (as opposed to the top of the document which may not be the same place). Shift+M Move cursor to the middle of the screen. Shift+L Move cursor to the last line on the screen. ++%++ Move cursor to the matching parenthesis, bracket or brace. Great for debugging programs. ++(++ Move cursor to the beginning of the previous sentence (where a punctuation mark and two spaces define a sentence). ++)++ Move cursor to the beginning of the next sentence. ++{++ Move cursor to the beginning of the current paragraph. ++}++ Move cursor to the beginning of the next paragraph. ++/++ <term> Finds the next occurence of the term. ++?++ <term> Finds the previous occurence of the term. N Repeats the last search command. Finds the next occurence. Shift+N Repeats the last search command. Finds the previous occurence. + Move cursor to start of next line. - Move cursor to start of previous line. Ctrl+F Scroll forward one screen. Ctrl+B Scroll backward one screen. Ctrl+D Scroll forward one half screen (down). Ctrl+U Scroll backward one half screen (up). Z + Move the current line to the top of the screen and scroll. Z ++.++ Move the current line to the center of the screen and scroll. Z - Move the current line to the bottom of the screen and scroll. Shift+H Move to home—the top line on the screen. Shift+M Move to the middle line on the screen. Shift+L Move to the last line on the screen. ## Editing in Command Mode¶ Keys Action X Remove character under cursor. R <char> Replace character under cursor with other character. C <nav> Change the text from cursor position to the navigation position (remove and switch to insert mode). C W Change word the next word. C 2 B Change back two words. Shift+C same as C ++$++ Change the line from cursor to end of line.
C C or Shift ++S++ Change the whole current line.
S Substitute character under cursor (remove and switch to insert mode).
D <nav> Delete the text from cursor position to the navigation position.
D D Delete current line.
<num> D D Delete num lines starting at current line.
Y <nav> Copy the text from cursor position to the navigation position.
P Paste line(s) you deleted (or copied) back into the file.
U Undo last change.
Shift+U Undo all changes on line.
++!++ <command> Filter text through a program.
++>++ ++>++ Indent line.
++>++ <num> ++>++ Indent number of lines lines (starting from current).
++<++ ++<++ Outdent line.
++<++ <num> ++<++ Outdent number of lines lines (starting from current).
++.++ Repeat the last editing command.
U Undo the last change.
Shift+U Undo all changes in the current line.
Shift+J Join the current with the next line (replace newline with space).
++~++ Toggle case on cursor position.

## Execution Commands¶

Execution commands start with ++:++ in the command mode and will be written in the bottom line:

Command Action
:q Quit vi but ask for saving if changes were made.
:q! Quit without saving.
:wq Write and quit vi.
:e! Wipe out all edits made in the current session.
:sh Execute shell command.
:r<file> Insert file after current line.