Variable name completion
If, like me, you use ViM to code in, and spend more time typing in names of
variables than doing actual work you should know that Ctrl+P and Ctrl+N
will cycle you through a list of words completing the word you have begun to
type.
Indenting Code
Again, if you're like me, you'll find this useful.
When I first started using ViM many years ago I used to reindent old code by
hand. I don't do it that way anymore.
Go to 'normal mode' (not insert or command mode) and use ctrl+V to select the
lines you want reindented. Then press the equals key ('=').
That's it - ViM will reindent the selected code for you.
Removing Recurring Blank Lines
This really isn't a ViM trick, but it's so useful that I'm going to mention it
anyway.
If I have recurring empty lines in a file, I get rid of them by doing a
:!cat -s
Function navigation
When I want to see the code of a function, I use ctags and ViM's tags
support.
(ctags is a tool which creates an index file describing where functions in
source code are defined. More information on ctags is available at the ctags
website.)
In normal mode, I press ctrl+] to see the definition of a function and press
ctrl+t to go back to the code I had been looking at.
If you want to go straight to a specific function, go to the command mode and
type ':tag show_me_the_function' where show_me_the_function is the name of the
function in question.
If you want to go straight to a function from the command line:
$vim -t show_me_the_function
If you just can't remember the full name of a function, but know how it starts
(with 'ui_b' for example, then try this:
:tag /ui_b
You'll be shown the first match. To get to the next match, do ':tnext' (can be
abbreviated to ':tn'). The opposite is ':tprevious' (which can be abbreviated to
':tp').
Recording actions
To record an action into a register, press 'q' then a letter to indicate which
register you have in mind, perform your action, then press 'q' again.
To make use of what you recorded into the register press '@' and then the
letter of the register.
Note: when using recorded actions/registers, it's important to navigate around
the line/file with the w/b commands or you may end up with buggy actions
recorded in your registers!
Searching for words, the quicker way
If you want to search for the word under the cursor, just press '*'.
This will search forwards through the file, to search in the other direction,
press '#'.
Comparing/merging files in style.
(with thanks to Stephen Shirley)
If you have two versions of a file and you want to see what the
differences are, and take some of the changes from one and put them into
the other, vimdiff (a.k.a. vim -d) is just the ticket. 'vimdiff file.old
file.new' will open up vim with the two files side by side, with the
differences highlighted. To 'obtain' the change in the other file, you
type 'do', and to 'put' the change from the current file across, you
type 'dp'. To move from one file to the other, you use the standard
change windows keystrokes: <ctrl-w><left-arrow> and
<ctrl-w><right-arrow>. If the lines in the files are too long to display
(wrapping is turned off by default in diff mode to make things clearer),
you can give the '-o' parameter on the cmd line to get the window split
horizontally instead of the default vertical split. And there you have
it.
Related: Learn more about vi Syntax highlighting in ViM, mappings and the vimrc file Using vi Vi filters, search & replace and more...
About the author, Ken Guest.
USERS COMMENTS
|