Title: Marginally Useful VIM Tricks
1Marginally Useful VIM Tricks
- COS Club Enrichment Series
- Jason OKane
- October 31, 2000
2Premise and Purpose
- The premise
- The text editor is a program we use so frequently
that it is worthwhile to invest effort in getting
the environment just right. - The purpose
- To provide an introduction to what VIM can to do
and to stimulate further exploration.
3A Confession
Without any customizations, I find VI and VIM to
be nearly unbearable.
4Customizing VIM
- Create a file in your home directory called
.vimrc - Put commands in it.
5From jokanes .vimrc
version 4.0 set bs2 set ruler set incsearch set
showmatch set ai set laststatus2 set
shiftwidth2 set tabstop2 set visualbell set
ignorecase cab W w cab Q q cab E e cab Sp sp cab
SP sp cab Wq wq iab i include iab d define
6Cool Settings
- syntax on
- activates syntax highlighting
- incsearch
- shows next match so far
- showmatch
- matches parens, brackets, etc. (vital for LISP)
- ignorecase
- case-insensitive searches
7More Cool Settings
- visualbell
- switches from annoying bell to soothing flash
- bs2
- allows liberal use of backspace
- Use help option-summary for a complete list of
settings
8Abbreviations
- Command Abbreviations
- cab W w
- cab Q q
- Insert Mode Abbreviations
- iab d define
- iab i include
9Mappings
- Shift Key? Humbug!
- map
- map w wltC-Mgt
- map q qltC-Mgt
10.viminfo
- VIM can store commands, buffer contents, search
patterns, etc. between editing sessions. - set viminfo'1,f0,50,/50,n/vim/viminfo
11Working with Multiple Files
- sp ltfilenamegt
- opens another file
- sp
- opens another window with the current file
- CTRL-W ltdirectiongt
- switches to another open file
- Q
- closes the current file
12Exceptionally Nerdy Scripts
- Spelling Checker
- 2-keystroke Make
- File Skeleton System
13Spelling Checker
- /vim/spell.vim lists all English words
- command line spell used to create list of words
to show in red
fu! Spell() w!spell0.tmp !spell lt spell0.tmp
gt spell1.tmp !sed -e"s//syntax keyword
ErrorMsg /" lt spell1.tmp gt spell2.tmp !echo
syntax clear gt spell3.tmp !cat spell2.tmp gtgt
spell3.tmp syntax on so spell3.tmp so
/vim/spell.vim !rm spell0.tmp spell1.tmp
spell2.tmp spell3.tmp endf
142-Keystroke Make
- Executes a make the current directory.
- Shows the results (read error messages) in a
split window
152-Keystroke Make
fu! Make() pe mrn "make-results" "
Create a buffer to hold the make results, if
needed. " Assume that if there's a
make-results buffer, it's the current one "
This is reasonable since the mapping below moves
up 99 windows before " calling this make
function. pe if(maincurbuf-gtName() ne mrn)
VIMDoCommand("new") VIMDoCommand("e
mrn") pe if(maincurbuf-gtName() ne mrn)
VIMDoCommand("e mrn") " Empty out the
buffer pe maincurbuf-gtDelete(1,
maincurbuf-gtCount) " Execute make "
The zero is needed in order to read into an empty
buffer. 0r!make " Allow users to close the
window without saving the results set
nomodified endf map m ltC-Wgt99kcall
Make()ltC-MgtltC-MgtltC-Wgtj
16File Skeleton System
- Reads in a skeleton when a new file is created.
- New file types can be added simply by creating
new files in the skeleton directory.
17File Skeleton System
pe HOME "/home/users9/jokane" pe SKEL
"HOME/vim/skel" fu! ReadSkel() pe fname
maincurbuf-gtName() pe (ext) (fname
/\.(a-z)/) pe VIMMsg("yo SKEL/ext")
pe if(-e "SKEL/ext") VIMDoCommand("r
SKEL/ext") pe maincurbuf-gtDelete(1) end
f au! au BufNewFile call ReadSkel()
18What was that again?
- If you remembered all of that, seek professional
help. - If not, find these slides and code at
http//www.css.tayloru.edu/jokane/vim