Advanced Vim written by Peter Jay Salzman Last updated: 22 July 2001 ============================ These are advanced vim topics. Read what you're interested in and skip everything else. Ex Mode ------- Vi actually has 3 modes: text entry, command and ex mode; we haven't discussed ex mode, but you've seen examples of it. You can get into ex mode by pressing the colon key ":" from command mode. In the next section on configuration, when you type :set wrapmargin=8, what you're really doing is typing a colon to enter ex mode and giving the command "set wrapmargin=8". Customization ------------- vim is highly customizable. There are tons of options you can set using the :set command. To see them all, go ahead and type :set all You'll see something like: --- Options --- noautoindent ignorecase rulerformat= noautowrite noincsearch scroll=12 backspace=0 noinsertmode scrolljump=1 nobackup isprint=@,161-255 scrolloff=0 backupext=~ joinspaces nosecure You'll actually see a LOT more. Only a few will be useful to you and I'll tell you which ones I use most. First notice that some of these have no equal signs. These are boolean options; they're either set or unset. Unset options are prefixed with "no", like "noautoindent". Set options don't have the "no": noautoindent (unset) confirm (set) autoindent (set) noconfirm (unset) You can use the :set command within ex mode to set and unset options: :set autoindent /* Sets autoindent */ :set noautoindent /* Unsets autoindent */ :set confirm /* Sets confirm */ :set noconfirm /* Unsets confirm */ Some options have equal signs followd by the = sign: shell=/bin/bash wrapmargin=7 You set them similarly to boolean options, but they require an argument: :set wrapmargin=6 Here are some of the more useful boolean options: autoindent Lines following an indented line will have same indentation. backup Write a backup file any time you start editing a file. number Puts line numbers on the LHS of the screen. ruler Display the CCP at the bottom of the screen. showmode Shows what mode we're in. warn Warn you when you try to quit a modified file without saving. Here are some useful nonboolean options: tabstop=3 Sets the number of spaces to a tab wrapmargin=6 Wraps a word to the next line if you get within 6 characters from the edge of the screen. When you start up vim, it tries to read a file named .vimrc in your home directory. If this file exists, vim will read the contents and execute the options as if you had set/unset them with the :set command. Here is my own .vimrc file to get you started. The quote " is a comment. "set autoindent " good idea but is more annoying than helpful. set beautify " i forgot what this does. set nomesg " burger, fries and a coke. don't bug me. i'm eating. set modeline " what mode are we in? "set number " puts line numbers on the LHS of the screen set showmode " shows what mode we're in set warn " warns when we quit without saving modified file set nobackup " NO PESKY ~ FILES PLEASE! set noerrorbells " doesn't seem to work? set wrapmargin=6 " wm: wrap line when we get 6 spaces from end of line set tabstop=3 " how many spaces per tab set shiftwidth=3 " sw: for the >> and << command. uses tabs and spaces. set textwidth=76 " tw: this wins over wrapmargin, i think. " Set no wrapmargins and textwidth for source code only au BufNewFile,BufRead *.f90,*.for,*.c,*.html,*.pl,*.tex set tw=0 wm=0 " HTML mappings au BufNewFile,BufRead *.html map! au BufNewFile,BufRead *.html map! au BufNewFile,BufRead *.html map!
au BufNewFile,BufRead *.html map!
" Latex mappings au BufNewFile,BufRead *.html map! \begin{equation*} au BufNewFile,BufRead *.html map! \end{equation*} au BufNewFile,BufRead *.html map! \begin{equation} au BufNewFile,BufRead *.html map! \end{equation} au BufNewFile,BufRead *.html map! \langle au BufNewFile,BufRead *.html map! \rangle au BufNewFile,BufRead *.html map! vv \v{ au BufNewFile,BufRead *.html map! hh \h{ The last lines are very cool. They apply certain rules like map! and set wm=0 only for files with filenames ending in certain suffixes. For example, when I edit any file ending in html., the function keys are mapped to produce html tags like and
. Programming Coveniences ----------------------- The following commands indent your lines and take optional numerical arguments to specify the number of spaces to indent. >> Shift current line right by 1 shift width << Shift current line left by 1 shift width If you want to change the size of shift width, say to 3 spaces, type :set shiftwidth=3 Vim will use a combination of tabs and spaces to give you the correct shift width. For example, if sw=4 and tabstop=5, then a >> will indent by 1 tab and 1 space. If sw=4 and tabstop=2, then >> will indent by 2 tabs. Next, vim can check your code for forgotten parenthesis or braces. There are a few ways of doing this. First, from command mode, type :syntax on Your C source code will be highlighted or colorized, making it easier to read. Any unmatched {}, [] or () will be highlighted. For instance, while ((c=fopen(filename, FILE) != NULL)) fgets(buffer, size, FP); } The last } is unmatched, and will be highlighted. Some people don't like the syntax highlighting. If it bugs you, you can turn it off by :syntax off Both "syntax on" and "syntax off" can be placed in your .vimrc file. The second way of checking for unmatched (), [] and {} is the % command. If you place the cursor over one one of these characters and type "%", then vim will place the character over the matching character. If there are any unmatched (), [] or {}, vim will beep at you. Lastly, in text entry mode, if you type a ), } or ] and vim doesn't see a corresponding (, { or [, it'll beep at you. Lastly, you can make your program without leaving vim. This is a great boon for programmers! Simply type :make and vim will make your program. You can tell vim to ":make all", ":make clean" or do whatever make you want. If there are any errors, they'll be displayed on the screen and the cursor will be placed on the line of sourcecode that your first error occured on. At some point, if you want to see the n'th error that occured, type :cc and vim will display the n'th error message. Text Markers ============ Vim has 52 text markers named a-z and A-Z. To set a marker, place the cursor anywhere in the text you want to mark and type m followed by the name of a text marker. So to mark a location using marker B, type mB. Text markers have a number of uses. First, you can quickly bring the cursor to the location of the marker by typing ` followed by a marker name. You can quickly bring the cursor to the beginning of a line containing a marker by typing ' followed by a marker name. This is useful enough to know how to use text markers. I usually set text markers on function definitions which allows me to quickly find functions within long source code files. But they have other uses as well. However, these other uses are kind of outdated by vim's highlighting and mouse capabilities, so I won't go over them here. Suffice to say that you can use text markers as addresses, so if you have 2 text markers set, say a and b, if you type :'a,'b d then all the text between markers a and b will be deleted. Abbreviations ------------- Abbreviations are set in ex mode and have the form :abbr With this definition, every time you type , vi will erase what you typed and replace it with . There's a few details you should know about abbreviations. Consider the abbreviation :abbr greetings hi First, you do this in ex mode, but you can also make the abbreviation in your .vimrc file. Second, for the abbreviation to be made, greetings must be preceded and followed by a space, tab or non-alphanumeric character. So 1greetings and greetingss will not be abbreviated with hi, but "!greetings" and "greetings." will turn into "!hi" and "hi." Third, abbreviations only work in text entry mode. Fourth, the may contain a space, but the may not. Fifth, if you really want the string "greetings." to appear in your document, use control-v to escape the period. So if you type: greetings^v! then greetings! will not be abbreviated to hi!. Sixth, abbreviations can be cancelled by the unabbr command: :unabbr Seventh, you can see what abbreviations have been made by typing ":abbr". Lastly, with text abbreviations, your text appears as you type it, and no transformation is made until you follow it with a non-alphanumeric character (which includes an emmediate exit from text entry mode). Keystroke Remapping ------------------- Abbreviations won't take place until you type a non-alphanumeric character AFTER the . Also, vi echoes each character of the abbreviation as you type it, just in case you really want in your text. Keystroke mapping is a bit different. There are two types of keystroke remapping: :map! works in text entry mode only :map works in command mode only There are some differences between remappings and abbreviations. Remappings always occur. So if we have a mapping map! the o then "another" turns into "anoor". This wouldn't happen with abbreviations! Next, remappings do not immediately print their characters. So with the previous remapping, if you type a "t" then nothing will print on the screen until you type the letter "h". If you type h, nothing will print on the screen until you type "e". Lastly, mappings can time out. So if you type "th" and wait for awhile, you'll see "th" appear on the screen and even if you type "e", it won't remap into "o". That is, when you start typing , it won't print to the screen until: 1. it turns into 2. you type a character that makes your string different from 3. you wait till the remapping times out You can see your text entry and command mode mappings by typing ":map!" and ":map" respectively. Lastly, you can delete your mappings with the :unmap and :unmap! commands. This is getting rather long, so I'll briefly cover two more subjects. Mouse Support ------------- Yes, you read that correctly. Vim has mouse support. Give the ex command :mouse=a And you'll be able to place the cursor anywhere on the screen using the mouse. Furthermore, you can highlight text and press "x" to delete it, or y to yank it into the unnamed buffer. Highlighting ------------ This is my favorite vim feature. There are two contexts of highlighting, line oriented highlighting (shift v) and block highlighting (control v). Play around with highlighting text to get a feel for how each works. With block highlighting, you can use any cursor movement keys like w, b and arrow keys to control what gets highlighted. Once a selection is highlighted, you can do a LOT of really neat things. Since this file is getting long, I'll only go over a very few of them. You can use "x" to delete a highlighted selection. You can indent the selection as a group using the << and >> commands. You can also use "y" to yank it into the unnamed buffer. The yank is fairly sophisticated and happens in the same context as how you highlighted the text. As always, you can use P and p to place the contents of the unnamed buffer back into the document. With a little practise, you could do things like placing entire paragraphs next to each other. For example, using block highlighting, you can turn aaaaaa aaaaaa bbbbbb bbbbbb into aaaaaa bbbbbb aaaaaa bbbbbb with only a few keystrokes. You could not do this easily with vanilla vi. This kind of operation was where something like notepad excelled over vi. But I just did that in under 6 keystrokes, and it took me less time than someone with a mouse. If you're familiar with ex commands, you can perform them on the selected text. For example, if you highlight some text and press : then vim will automatically fill in the address field with the starting and ending point of the selection. From there, if you do something like: :s/foo/bar/g it's understood to mean "perform this on the highlighted text only". Conclusion ---------- There is so much more than what I've shown. Vi is phenomenally powerful. People have written games like tower of hanoi using nothing but vi macros. But vim extends this by lightyears. The real power comes when you get really good at vi and integrate what you know with the conveniences of vim. One thing that the author of vim did that was right on the money was to create conveniences that integrate flawlessly with vi. I wish I could show you more, but at this point my little article would become a book if it got any larger. When all is said and done, you should pick the features which you find most useful and master them. Learn the rest later. And have fun.