Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I spend almost all day programming in Vim and I am sure that a lot of you do too. What features do you use that make your day to day coding that much better? One that I use is gv, which will let you reselect the previously selected text. It is great for reindenting!

share|improve this question
1  
For historical purposes, here's a link to the original SO question: stackoverflow.com/questions/95072/… –  Jax Jul 16 '09 at 1:20
    
Note my comment below, and you won't even have to type gv. :) –  Nathan Long Feb 1 '11 at 17:41
add comment

closed as not constructive by 8088, Sathya Aug 31 '11 at 4:07

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

13 Answers

:%s/x/y/g

This command replaces all instances of x in the current file with y. This is basically just a find and replace, but I use it so often since I've learned it at work that it has become invaluable.

Also, /search string

then n to navigate through all instances of the search string within the file. Great for doing manual tweaks a lot more easily, especially if you can't apply a rule to the entire file.

share|improve this answer
    
I'm actually working on converting a largish section of our codebase at work and I'm doing it entirely with vim regular expressions (so far). It works really well! –  Frew Jul 15 '09 at 17:46
    
Add 'c' to the end of that to make the command interactive. e.g. :%s/x/y/gc –  sml Sep 21 '10 at 6:21
1  
Note that the % is where to do the substitution - % means "the current file." If you visually highlight some lines and type :s, you can do a substitution just in that selection. –  Nathan Long Feb 1 '11 at 17:40
add comment

=% for auto-indenting everything within a block of code.

share|improve this answer
    
Oooh! That's new to me! –  Frew Jul 17 '09 at 22:09
    
That works if your cursor is on the beginning {. If you're inside the block, you can do =i{, for "indent inside the curly braces." –  Nathan Long Feb 1 '11 at 17:38
add comment

== does nice auto-indenting on the current line.

Normally, though, I use = with a motion, like =G to indent to the end of the file, or =i} to indent inside the current set of curly braces.

share|improve this answer
2  
Also keep in mind >> and << which can manually do (un)indenting. –  Frew Jul 15 '09 at 17:46
    
@Frew - yep. Also, if you add vmap <Tab> >gv and vmap <S-Tab> <gv to your .vimrc, you can indent and unindent with Tab and Shift-Tab, respectively, AND it will go right back into visual selection mode so you can indent or unindent further. –  Nathan Long Feb 1 '11 at 17:34
add comment

Use * in command mode to search for the word under the cursor.

share|improve this answer
1  
And don't forget it's sister command, #, which does the same thing but goes backwards instead of forwards! –  Frew Jul 15 '09 at 14:58
add comment

The :! command - invaluable for lsing, and the "sudo tee" trick.

I've been asked to explain the tee trick. Basically, it allows you to save a text file with admin priveleges, even if you didn't start VIM as root:

:w !sudo tee %
share|improve this answer
    
You might want to explain the "sudo tee" trick in here :-) Also I like to do :!sort after highlighting variable definitions and whatnot. –  Frew Jul 15 '09 at 14:57
    
You can really turn Vim into a great "scriptable" editor using :! and some shell/Python/Perl/other scripts. –  Jonas Jul 15 '09 at 15:05
    
I actually have an alternative to !ls, which runs a vnew, outputs r!ls into the new buffer, and shrinks it horizontally. I can't be bothered to remember all of the files in a directory with over three in it ;) –  new123456 Nov 16 '11 at 3:06
add comment
.

Hitting the . key repeats the previous command. Lovely for, say, editing an .ini, or so.

share|improve this answer
    
this is surprisingly powerful, but it takes a bit of vim-ing before you can really get good with it. –  aaron Jun 13 '11 at 4:26
add comment

syntax color highlighting

share|improve this answer
add comment

I am always using gq to re-do line breaks on email, text files, etc. Really useful when replying to email (I use pine) and editing multiple previous replies.

share|improve this answer
    
Oooh, I'd totally forgotten about this! –  Frew Jul 16 '09 at 1:32
    
Why, oh why, can't the more popular email clients support similar functionality? –  erichui Jul 16 '09 at 1:50
add comment

Where to start... Here's the things I can recall I've used in the last 5 minutes or so.

  • NERD Tree - File exploring. Makes VIM more like an IDE.
    • One huge thing for me is the ability to cd to a directory and then drop to a shell prompt through my shell mappings (below).
  • ci for emptying whatever you're stuck between.
  • :! for launching standard apps.
    • I have mappings for launching a shell prompt, mysql client, apache log files, etc.
    • Subversion integration is always nice. Quick diffs, stats, etc.
  • :abbr for making repetitive typing of large blocks go away.
  • Tabs. Need I say more?
  • Splitting out files, always good. :Sex is always nice.
  • :set paste! to keep from having auto-indent run amok.
share|improve this answer
    
I use all of these except for abbr. I tend to make a lot more macros than abbreviations. –  Frew Jul 17 '09 at 21:53
add comment

ZZ save and exit (instead of :wq!)

share|improve this answer
    
Huh? What about x? –  DevSolar Sep 16 '11 at 7:17
add comment

I heavily use following commands while writing or traversing C/Tcl/Perl code:

copy-paste code

CTRL-f and CTRL-b for page forward and backward traversal
% for block-by-block traversal
^ goto start of line
$ goto last end of line

start selecting code using command v (visual mode) and any of the traversal key above
y for copy
p for paste

s to delete a character and goto insert mode to type new text.

Code level traversals (requires coding style discipline)

]] for start of next function
[[ for start of prev function

CTRL-] to jump to definition of a variable/function/macro if tags are available.

Search

* => search word (or text selected using v) in forward direction
# => search word (or text selected using v) in backward direction
n => execute previous search command in forward direction
N => execute previous search command in backward direction

All the above commands can be prepend with a number to signify how many times to execute it. The realization of the power of this additional dimension comes slowly as one uses vim more and more and challenges oneself to improve.

share|improve this answer
    
Ah, ]] and [[ are new to me. That's cool! –  Frew Oct 21 '09 at 19:33
add comment

I just enter Text. Everything else is just ... Sugar.

share|improve this answer
add comment

I'm a former user of TextPad, and had memorized many of that program's accelerator keys. One of the first things I did in Vim was learning the map function to keep consistency (F5 search, F8 replace, etc.). I created my personal colorscheme, and borrowed alot from others' vimrc files. Viva Vim! Configurable to any degree.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.