Vim is my editor of choice, and I feel I am above average in my use of it. I do recognize, though, that the feature list of vim is huge. With this in mind, I was wondering what features you vim users out there use on a regular basis.
The following google video was good: 7 Habits For Effective Text Editing 2.0
One of the ones I use most is =
. That does automatic indentation of existing code.
syntax on
Although this is rather basic, it is still the #1 feature that I use.
You will probably get something from watching this presentation by Bram Moolenaar at a Google Tech Talk - it's all about power tips in Vim and quite well presented. Not for beginners, I suspect
Here are two screencasts for beginners that I found useful as well:
replace all occurrences of PATTERN (which is a regular expression) with REPLACE
:g/PATTERN/s//REPLACE/
Simple but I use it constantly.
The other thing I love, which I can't think up a great example of right now, is being able to paste commands into the editor.
I used to use the exuberant ctags a lot, but now I don't write C. I used the syntax-highlighting-to-colored-HTML-output script frequently. I appreciate the brace highlighting and smart indenting quite a bit. The help is my most often used vim command feature possibly followed by gqq for wrapping. Otherwise I generally use base vi features, where I'd put '/' at the top of my list because I'm not actually as familiar with movement keys, and ':%s/x/y/g' as near the top.
my .vimrc turns on syntax highlighting, and I often invoke it with a glob to do bulk find replaces across a set of files.
Another one is extensive use of windows using the :split command or the ^w commands:
^wn
- new window
^ws
- horizontal split
^wv
- vertical split
^wr
- rotate windows
^ww
/^wW
- move to the next/previous window
^w=
- make all windows equal size
^wc
- close the active window
Combine that with various buffers using :e < filename > to open a file and :b< number > or even b:part_of_the_filename<TAB>
to switch between open files, and I can browse code very quickly.
Now combine that with tags and you have even better browsing capabilities. Create a tags file with
:!ctags -R .
and then you can jump to the declaration of an identifier with ^] and back to where you were with ^T.
The combination of windows, buffers and tags is a killer!
A fantastic list is David Rayner's Best of Vim Tips. I guarantee you could find something new in there every week for the next year.
Put your cursor on a function and press K to get the man page.
Visual block Insert: Ctrl-v
to enter Visual (Block) mode, then shift-i
to do insert after right most highlighted point on all highlighted lines. Great for indenting text where it's not the first thing on the line, or for pre-pending a bunch of variables with a common prefix, where there's no quick regex to match all of them.
:%s/~/bar/
: replace your last replacement string with 'bar'.
search/replace within a visual selection (v
to enter visual mode, select target area, then type :s/foo/bar/[igc]
).
Similarly, :'a,'b:s/foo/bar
for replacing between marks a
and b
.
- I use
gq<motion>
to format text while writing pieces of text (and comments). - Vimdiff is a great way to view differences between files.
- Opening two files at once with the
-o
commandline option. - Find and replace using
/
,n
and.
- Macros
- Visual line mode and
%normal 0i#
to insert comments. And%normal 02x
to remove them. - Project plugin
- Vim Outliner
- Ctrl-N for completing functions and Ctrl-X Ctrl-F for completing filenames
- Set you the history for all command line options to some high value (like 1000) to have Vim remember commands you typed in previous sessions.
A lot of other commands are embedded in my brain (or fingers) somewhere, so I'm not sure what those are.
At this point I'm going to make stuff up (because I don't use those commands (especially abbreviations). So I will stop.
You can consult Vim Tips sorted by ratings, the first tip being Best of VIM Tips.
As for me, I find gv (reselect last selection) and CTRL-O & CTRL-I (Go to older / newer cursor position in jump list) very usefull.
Oh, and CTRL-V {motion} SHIFT-I {write some text} <ESC> (writes the text on each line of the selected block).
And set wildmenu
(enhanced completion mode) is one of the first thing I do when I go to someone else's Vim.
Macros and ^p and ^n. Most people watching type in an editor think that I am the worlds fastest typist with my use (and abuse) of ^p and ^n. If you don't know what they do they are pseudo intellisense for Vim (basically completes a word based on other words in the document, works incredibly well for well named variables and function names). If you use TAGS it also uses the tags file (then ^t to just to the function is also very useful). Another favorite is * in the normal mode, to search for the next occurrence of the word underneath your cursor (but that has already been mentioned).
Something I recently learned:
When you open a file as an unprivileged user that you don't have write access to (should have opened the file as root), you can save it using:
:w !tee %
The often overlooked '.' command. It redoes the last command.
I find it very useful when doing a search & replace type activity where you don't want to replace all instances. You can do a search (using '/'), do the replace (with a 'c' or maybe 'r'), find the next instance (using 'n'), and either redo the edit (with '.') or skip to the next one (another 'n').
Most Vim users are probably aware of them, but *
and #
to search (up and down respectively) for the word under the cursor are extremely useful commands. Make sure you set hlsearch
and set incsearch
in your vimrc beforehand to get the full effect.
I also find myself using the movment commands f
and t
very frequently. f<character>
will move the cursor to the next instance of the given character and t<character>
will move the cursor right before the next instance of the given character. For example, given this code:
int temp = getWeath3rData(cityId).mTemperatures[currentDate].mCelsius;
With the cursor on the first line in normal mode, you can instantly jump to the errant '3' in the misspelled word 'Weath3r' by pressing f3
and then replace it with the correct character with re
.
When combined with counts and other commands (for me, usually d
or y
), the f
command becomes even more powerful. For example, this command:
2df:
can be used to delete everything up to and including the 2nd instance of the ':' character on the current line.
:retab
Homogenizes the indentation style of the file, based on your current settings. (shiftwidth, expandtab, etc)
:r! shellcommand (replace shellcommand with shell command of your choice). This will append the results of the shell command to the current buffer.
Learn to use and love the search/replace feature:
:[range]s/reg_ex/text_to_replace
Use Vim-style movement (no arrow keys, PgUp/PgDown, etc.)
'h' 'j' 'k' 'l' for movement (Up, Down, Left, Right)
'G' End (First line of the file)
'gg' Home (Last line of the file)
'0' Beginning of line
'$' End of Line
'H' Top of the Screen
'M' Middle of the screen
'L' Bottom of the screen
my favourite
:g/search-string/norm @h
runs a macro recorded in h for every match of search-string...divine :D
and the fd to quickly move the cursor to the next occurrence of 'd'...very useful while selecting in visual mode
gf
opens the file under the cursor, which is very handy while exploring code. Since I use tabs, I re-mapped it this way:
map gf :tabe <cfile><CR>
which opens the file in a new tab, instead.
here are some interesting options, that has changed my vim usage in the last month or so.
- vsplit - splits the CMD line interface into 2 or more vertical sections and allows editing multiple files at the same time
- hsplit - horrizontal version of the above
code hiding. This is a little elaborate. found here
:set foldmethod=manual :map + v%zf
Add this to your .vimrc, and in cmd mode, use v + up/down arrow, to select and zf to hide. This also enables using + on { } to fold functions. Use space to open the folded text
Oh my goodness, I can't believe no one has listed these:
- ciw
- ci'
- cit
you can replace c with v or d and do what you would expect. Basically ciw means change inner-word, or, if you put your cursor in the middle of a word in normal mode and type ciw it will erase the entire word (not the surrounding spaces, if you want that use a instead of i) and put you in insert mode. The ' does it with things in quotes and t does it for html tags.
Oh yeah, and gv will reselect whatever you selected with v last.
Multi-file search replace etc.:
args
grep -l foo ../*/*.cpp ../*/*.h
argdo %s/foo/bar/gc
Exuberant ctags directly integrated is my most frequently used feature:
Control+] jumps to the tag (definition) for the symbol under the cursor. Control+t pops off the tag stack, effectively going back from whence you came.
In your .vimrc file, add this:
set tags = /path/to/tagfile
set tags += /path/to/other/tags
You can expand this with the taglist.
There are a few ways to search for help that are pretty, uh, helpful.
:help help
- This opens up help for the help section, which provides the following tips in a verbose paragraphical format:
:helpgrep
and its cousin:lhelpgrep
are pretty great, these let you search for a regex over the entire vim help text contents.:help STRING^D
- this will give you a list of help topics that contain the fixed string STRING
- from within the help screen you can do
:tselect REGEX
- this will search for a help topic that matches that RE, and give you a list of matches.
:tags REGEX
- same deal, just jumps to the first result.
- these commands work using the help tags list, which is compiled using the 'ctags' utility, which you can do if you need to with
:helptags
I recently found out that vim has a good support and some nice color schemes for 256 color terminals. It really makes a difference for syntax highlighting.