[wellylug] Vim Tips
Grant McLean
grant at mclean.net.nz
Tue Sep 9 13:43:12 NZST 2003
Further to Brenda's email, I though I'd share some of my Vim usage tips.
Please pitch in your favourites...
Searching:
The asterisk ('*') can be used for search forwards in the document for
the word under the cursor. The hash ('#') is the same but searches
backwards.
The percent symbol ('%') searches for (jumps to really) the bracket
matching the one under the cursor or the next bracket in the file if
there isn't one under the cursor
:set incsearch
This turns on incremental search mode, so the search is executed as you type.
:set ignorecase
:set nowrapscan
make searches case-insensitive and don't wrap around to the beginning
of the file if the search doesn't match.
:map <F12> :nohls<Enter>
Programs the F12 key to turn off search term highlighting.
Remember 'n' takes you to the next match and 'N' (shift-n) takes you
to the previous match.
Text Input:
:map! <C-O> {<C-M>}<Esc>O<Tab>
Programs Ctrl-O to 'open a matching pair of curly braces and position
the cursor indented on the line between. Note, the trailing '!' on the
map makes the mapping work in insert mode.
:map <F7> {gq}
Programs F7 to re-flow the current paragraph, wrapping long lines and
joining short ones.
Using Marks:
You can 'bookmark' the current cursor position by hitting 'm' followed
by a letter. I usually just do mm to create bookmark 'm'.
You can return to a mark by typing a single quote followed by the letter
(eg: 'm). You can also use the backtick instead of the single quote.
For reasons I can't quite explain, I prefer the backtick.
You can delete everything from you current cursor position to a mark
(eg: m) like this:
d`m
Or yank (copy to a buffer) like this:
y`m
And then paste the yanked text by moving somewhere else and pressing 'p'
or 'P' to paste after or before the cursor respectively.
The special mark called dot ('.') is automatically set to the last
position in the file where you changed something.
Shell Interaction:
!!command
Replace the current line with the output of the command. I often use
!!which perl
and then insert #! at the beginning of the line.
:range!command
Take all the lines in 'range' (1,10 = lines 1 to 10; % = all lines;
.,$ = all lines from the current line to the end of the file), pipe
them into command and replace them with the output of command. Eg:
:%sort
There are many many tips here:
http://vim.sourceforge.net/tips/index.php
And a thread with a bunch of useful links for Vim+Perl:
http://www.perlmonks.org/index.pl?node_id=257782
Anyone else care to share some goodies from their .vimrc?
Cheers
Grant
More information about the wellylug
mailing list