Why vi? A pursuasive essay/tutorial on the vi editor. Michael Schmitz (incomplete)
I. Forward - Why vi? - - - - - - - - - - -
Disclaimer: if you expect an informing and empirical comparison between vi and its competitors, you have come to the wrong place. This document is defense of a personal bias, perpetuated by my naivety and inability to quit emacs.
The disclaimer stated my foremost reason: honestly, at the time of writing, I do not know how to quit the emacs editor. With vi it is a simple and intuitive "ZZ", rapidly executable and effective. More honestly, I use vi because I feel that as I develop in proficiency with the vi editor, my efficiency increases. Other text processors, such as the behemoth Microsoft Word and the sparse xedit, lure unsuspecting users with their pleasing interfaces. But, in truth, this software provides a shallow learning curve at the expense of efficiency. It's easy to learn, but you never improve.
True other editors such as emacs provide the same advantages as vi, but I simply don't get emacs. Not wanting to hit control every time I want to execute a function, I stick with vi. That and the reliability that vi will exist on any machine I (sanely) would use.
II. An Introduction to vi - - - - - - - - - - - - - -
The principal difference between vi and simple editors such as notepad and xedit is the existance of two modes. Vi has an input mode similar to that of nodepad and xedit, but it also has a command mode. In command mode the user can engage in any number of optimizing operations. For example, 'dd' deletes the current line and '~' changes the case of the letter under the cursor. Moreover, these commands can be strung together to perform novel operations. To exemplify, 'ggVG~' changes the case of the entire document ('gg' goes to the top of the document, 'V' enters a mode where the entire line is selected, 'G' expands the selection to the bottom of the document, and '~' changes the case.)
These two modes causes one of two results in the end user: a quick slide into utter insanity or awe and respect for the philosophy that is vi. Few people have ambivalent feelings about vi.
- "Why do we have to hide from the police, Daddy?" "Because we use vi, son. They use emacs."
Another benefit of vi is its flexibility. I mean, it has hundreds of syntax highlighting schemas preinstalled. In my introductory computer science class students are pursuaded to use TextPad or Eclipse. I found that vi works far better than either of these. It's efficient, I'm already familiar with it, and any functions of the other programs can be implemented within vi via a map command.
While vi may appear bloated, indicipherable, or even absurd, after a few weeks of practice you will enter vi commands into firefox and stare perpexedly at the screen as the computer misinterprets your commands. After months you will begin revising essays using vi syntax. (Actually, this might prove quite efficient:
- xp x 2x cwover x ie
- The brwon dogg jumpeded under thee tre. Note: you may wish to work backwards to preserve text alignment)
III. The vimrc - - - - - - - -
~/.vimrc houses a user's personal vim settings. Listed below is a heavily commented version of my (very simple) .vimrc:
vimrc - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
" disable full compatibility with vi, unleashing all vim's features set nocompatible set autoindent
" set the ex history to 50 set history=50
" show the mousepoint (xx,yy) and % location in file in the lower left set ruler
" show partial commands in the toolbar set showcmd
" disable highlighting of the search phrase set nohlsearch
" search for the phrase as it is typed set incsearch
" expand tabs to spaces set expandtab
" a tab equals four spaces set tabstop=4
" shifting text shifts four spaces set shiftwidth=4
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Vi Motion Commands - - - - - - - - - - While moving around in a file is not super interesting, many of the commands build upon these, so I will get them out of the way now.
/* These commands move the cursor in the given direction. While the cursor
- attempts to remain in the same collumn, it will shift left to the text if it
- is in empty whitespace.
- /
h move cursor left j move cursor down k move cursor up l move cursor right
/* Capitol letters ignore punctuation. Lowercase commands stop on
- punctuation.
- /
w/W move right to the beginning of the next word e/E move right to the next end-of-word b/B move left to the previous beginning-of-word ge/gE move left to the previous end-of-word
( or ) move forward or backword one sentence [ or ] move forward or backword one section (seperated by an empty line)
H move cursor to the top of the page M move cursor to the middle of the page L move cursor to the bottom of the page
gg move to top of file xg moves to line x :x (same as last command) G move to bottom of file
0 move to the beginning of the current line $ move to the end of the current line ^ move to the first non-whitespace of the current line
gm move to the middle of the screen n% move to percentage n of the file n| move to column n
zz or z. center the screen around the cursor line
Compound Commands - - - - - - - - -
Many commands, in fact all of the commands listed in the movement section, can be augmented with additional parameters. For example:
2w moves right two words (to the second beginning-of-word) 5H moves cursor to 5 lines below the top of the screen 10z ¬ moves the cursor to line 10 (¬ denotes enter)
Insertion Commands - - - - - - - - - -
i enters insert mode at the cursor point I enters insert mode at the begining of the current line a enters insert mode at the cursor point A enters insert mode at the end of the current line o enters insert mode on a newline after the current line O enters insert mode on a newline before the current line
c{size} deletes the next specefied distance (word, sentence, etc.) and
- enters insert mode. Example:
- cw change word ce change up to the of the current word c} change text through next sentence
C deletes text on the same line after the cursor and enters insert mode cc/C/S delete line and enter insert mode
r{char} replaces the text under the cursor with the next character added R enters replace mode at the cursor location
s deletes the character under the cursor and enters insert mode
Deletion Commands - - - - - - - - - dx delete the character under the cursor dX delete the character before the cursor dm delete text through the movement m (i.e. dw deletes the next words) dd delete line xdd delete x lines (a number can preceed most commands)
Undo/Redo - - - - - u undoes the previous edit U restores the selected line to its pristine state
- (once the cursor is removed from the line, U will no longer restore
- it. In other words, U undoes all of the edits made while the cursor remained on the current line.)
^R redoes that which was last undone
- (once any text is manually edited, ^R will no longer redo until the
- next undo)
Repeating Commands - - - - - - - - - - . repeat last edit command @: repeat last ex command
Search/Replace - - - - - - - - / search forward for the next occurance of the entered word, or the
- previous search words if no text is entered
? search backwards... n go to the next occurance of the search phrase N go to the previous occurance of the search phrase
* search for the next occurance of the word under the cursor # search for the previous occurance of the word under the cursor
Markers - - - - Markers are useful when navagating large files. One cat set a marker and return to that spot in two keystrokes.
mx marks the current spot with the latter specified (here 'x') `x go to the mark of x 'x go to the line where x was marked
The Ex Editor - - - - - - - :{num} move to line num
Multi-window Fun - - - - - - - - - :split {filename} loads filename into a new horizontal frame
Multiwindow Fun - - - - - - - - :sp[lit] {file} split the screen and open file in the new frame WW or ^W W switch frame
Super-useful vi commands - - - - - - - - - - - - - <C-R><C-W> pulls <cword> from the text onto the ex (:) command line <C-R><C-A> pulls <CWORD> from...
<C-P> complete word with first previous occurance. Repeated execution
- iterates farther into previous matching cases.
<C-N> same as C-P but matches to ocurrances later in the document.
gq Format a section (i.e. paragraph with } or specify lines
- beforehand. See ":help gq" for more information)
set mouse=a !aspell -c %
References - - - - - - This is a list of other excellent vi/vim information sheets and tutorials.
Vim Quickreference Card http://tnerual.eriogerg.free.fr/vimqrc.pdf
Best of Vim Tips http://www.rayninfo.co.uk/vimtips.html
