(Modified 2021 Sep 21)
The vi editor
with vim-specific leanings
Outline
Overview
vi has two modes: insert mode and command mode. Insert mode is where you type, and command mode is where you do everything else, like cut, paste, save, etc. You enter insert mode by several ways (e.g., i), and you enter command mode using ESC. I'm not sure why command-mode operations must preclude inserting, but that's the way it is.
vim has some features on top of vi, which makes it more usable. Such features include mouse controls, like highlighting and scrolling using a wheel mouse. By default, mouse control is off. There's a :set command to turn it on, but I turn it on by default by having this line in my ~/.vimrc:
set mouse=a
In fact, this is the only line I have in my ~/.vimrc file. I should change the colors for syntax-highlighting someday, because the default colors are atrocious (yellow on white??).
Also, there's a graphical version called gvim that has some pull-down menu support. You still have to manually alternate between insert and control mode, but the pulldown menus let you get away without remembering all of the control characters.
I collected the list of commands below from the follow three pages:
University of New Brunswick -- link broken now
vi-improved.org -- This one is amusing since it uses profanity.
Navigation -- done from command mode
G -- go to end of file
123G -- go to line 123
gg -- go to beginning of file
^ or 0 -- go to beginning of line
$ -- go to end of line
w -- go to next word
W -- go to next whitespace-delimited word
b -- go to beginning of previous word
B -- go to beginning of previous whitespace-delimited word
e -- go to end of next word (sometimes useful)
E -- go to end of next whitespace-delimited word
Inserting text
i -- insert before cursor
a -- insert after cursor
I -- insert before line
A -- insert after line
O -- create new line above and insert
o -- create new line below and insert )this is the less intuitive position)
CTRL-P -- autocomplete text based on prior entered text --
You toggle through instances with subsequent CTRL-P and CTRL-N.
r -- replace single character
R -- replace arbitrary number of characters (like "Ovrwrt" mode in emacs)
ESC -- escape from insert mode to command mode
Search/replace -- done from command mode
/string -- search forward for "string"
?string -- search backwards for "string"
n -- repeat earlier search (i.e., same direction)
N -- repeat search in opposite direction --
If you searched backwards using ?, subsequent Ns will search forward.
:noh -- stops highlighting search hits (you could also search for a random, unlikely expression)
:%s/before/after/g -- substitute string "before" for "after" globally
:%s/before/after/gc -- as above, but asks for confirmation
:%s/before/after/gci -- as above, but ignores case
Cutting/pasting -- done from command mode
This is a pretty fundamental set of operations, but the reason I'm listing these after searching and replacing is that it can draw on some of the syntax used in searching. The general from using the d command is that it's followed by a location.
dd -- delete whole line
d12d -- delete 12 lines
d$ -- delete through end of line
dw -- delete word
d/string -- delete through next instance of "string"
d -- (using vim's visual mode) delete mouse-highlighted text
y -- copy or "yank" -- used like d except text is not deleted
J -- join current line with next line -- deletes carriage return
P -- paste deleted or yanked text --
Since deleted text is kept in memory, "cut" might be a more descriptive term than "delete" above.
p -- paste after cursor -- This is the less intuitive location.
u -- undo
CTRL-r -- redo
File input/output -- done from command mode
:q -- quits
:q! -- quits, overriding unsaved changes
:w -- write, i.e., save
:w file -- write to file
:wq -- write and quit simultaneously
:e file -- open a file called file, need not exist
:r file -- insert file at cursor position
:bn -- toggle to next buffer (if multiple files are open)
:bp -- toggle to previous buffer
:buffers -- lists buffers
:buffer 2 -- go to buffer #2, as specified in :buffers
Appearance/split windows -- done from command mode
:set number -- shows line numbers
CTRL-w s -- split screen horizontally
CTRL-w v -- split screen vertically
CTRL-w CTRL-w -- navigate to other window
CTRL-w o -- makes current window the only one
CTRL-w c -- closes current window
CTRL-l (lowercase-L) (or :redraw!) -- refreshes screen
Macros -- done from command mode
qX(command_list)q -- binds (command_list) to letter X
@X -- execute macro bound to X
This page is Lynx-enhanced