views:

3722

answers:

33
+39  Q: 

Useful Vim features

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.

+16  A: 

The following google video was good: 7 Habits For Effective Text Editing 2.0

dpp
+8  A: 

One of the ones I use most is =. That does automatic indentation of existing code.

Nathan Fellman
+4  A: 

syntax on

Although this is rather basic, it is still the #1 feature that I use.

borodimer
+1  A: 

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

Seven habits for Effective Text Editing 2.0

Flubba
+3  A: 

Here are two screencasts for beginners that I found useful as well:

http://blog.eleutian.com/2008/01/18/VimScreencast1.aspx

http://blog.eleutian.com/2008/02/06/VimScreencast2.aspx

dpp
+2  A: 

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.

jj33
similarly: `%s/PATTERN/REPLACE/gc` - the c is neat to confirm the first few replaces, and hit a to replace them all.
hgimenez
Quick and useful example of pasting commands into the editor: `r! find /path/to/dir -name '*.html'` to insert a list of all html files in the subdirectories of /path/to/dir.
Jabir Ali Ouassou
+1  A: 

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.

dlamblin
+3  A: 

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!

Nathan Fellman
+16  A: 
Brendan
Do you use the standard Vim? -- I get the following error in the newest standard Vim: "E518: Unknown option: spellang=en_gb."
Masi
Ah, sorry, there should be three 'l's in spelllang, I've changed the andswer as appropriate
Brendan
I started using mm/'m instead of ma/'a. Carves out a few ms.
kotlinski
@kotlinski - that's a good tip, I'll change it
Brendan
+7  A: 

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.

Greg Hewgill
+8  A: 

Put your cursor on a function and press K to get the man page.

Judge Maygarden
It seems very useful. Can you clarify how to use it? When I had my cursor over things some C functions, it did not do anything. Where do you use it?
Masi
I see it searches just the manuals in shell.
Masi
I have the same problem: I used it unsuccessfully in .vimrc and in .zshrc. It seems to search manuals by info. -- To get more results would apparently require to change the reader to man, for instance. -- It would be great to have the manuals which are as zip-files in /usr/share/man in the search results.
Masi
+3  A: 

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.

braklet
+3  A: 
  • 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.

Peter Stuifzand
+2  A: 
Brendon
+2  A: 

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.

Sébastien RoccaSerra
Daniel Gonzalez Gasull
Thanks. Too bad, this page was really useful.
Sébastien RoccaSerra
+4  A: 

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).

Kris Erickson
+3  A: 

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 %
sherbang
+1  A: 

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').

John Meagher
+5  A: 

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.

Dominic Dos Santos
+1  A: 
:retab

Homogenizes the indentation style of the file, based on your current settings. (shiftwidth, expandtab, etc)

amrox
+3  A: 

Find and remove empty lines:

:g/^$/d

JayG
+1  A: 

:r! shellcommand (replace shellcommand with shell command of your choice). This will append the results of the shell command to the current buffer.

metadave
A: 

Learn to use and love the search/replace feature:

:[range]s/reg_ex/text_to_replace

Frosty
+2  A: 

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

Frosty
Please Fix (check :help gg or :help G): gg without a count goes to the first lineG without a count goes to the last line
Tao Zhyn
Note: 'End' and 'Home' can be used to mean 'last' and 'first' line.
David Oneill
+15  A: 

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

shyam
A: 

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.

Lucas Oman
A: 

here are some interesting options, that has changed my vim usage in the last month or so.

  1. vsplit - splits the CMD line interface into 2 or more vertical sections and allows editing multiple files at the same time
  2. hsplit - horrizontal version of the above
  3. 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

shiva
+9  A: 

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.

Frew
Awesome! Great thanks :)
Masi
I just figured out new commands such as ci{, ci(, and ci[. Cool!
Masi
+1  A: 

Multi-file search replace etc.:

args grep -l foo ../*/*.cpp ../*/*.h

argdo %s/foo/bar/gc

andreas buykx
A: 

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.

s1n
A: 

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
intuited
A: 

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.

Marco Mariani
Which is your favorite color scheme for such terminals?
Ed Brannin
depending on the language, xoria256 or inkpot, both changed to have a black background (let g:inkpot_black_background = 1, and I think I patched xoria).
Marco Mariani