13

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!

2

13 Answers 13

9

:%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.

3
  • 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! Jul 15, 2009 at 17:46
  • Add 'c' to the end of that to make the command interactive. e.g. :%s/x/y/gc
    – sml
    Sep 21, 2010 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. Feb 1, 2011 at 17:40
7

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

2
  • 2
    Also keep in mind >> and << which can manually do (un)indenting. Jul 15, 2009 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. Feb 1, 2011 at 17:34
6

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

2
  • Oooh! That's new to me! Jul 17, 2009 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." Feb 1, 2011 at 17:38
5

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

1
  • 1
    And don't forget it's sister command, #, which does the same thing but goes backwards instead of forwards! Jul 15, 2009 at 14:58
5

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 %
3
  • You might want to explain the "sudo tee" trick in here :-) Also I like to do :!sort after highlighting variable definitions and whatnot. Jul 15, 2009 at 14:57
  • You can really turn Vim into a great "scriptable" editor using :! and some shell/Python/Perl/other scripts.
    – Jonas
    Jul 15, 2009 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, 2011 at 3:06
4
.

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

1
  • this is surprisingly powerful, but it takes a bit of vim-ing before you can really get good with it.
    – aaron
    Jun 13, 2011 at 4:26
3

syntax color highlighting

2

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.

3
  • Oooh, I'd totally forgotten about this! Jul 16, 2009 at 1:32
  • Why, oh why, can't the more popular email clients support similar functionality?
    – erichui
    Jul 16, 2009 at 1:50
  • Recently I am using gw instead of gq because it keeps the cursor position Feb 1, 2018 at 22:10
2

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.
1
  • I use all of these except for abbr. I tend to make a lot more macros than abbreviations. Jul 17, 2009 at 21:53
2

ZZ save and exit (instead of :wq!)

1
  • Huh? What about x?
    – DevSolar
    Sep 16, 2011 at 7:17
1

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.

1
  • Ah, ]] and [[ are new to me. That's cool! Oct 21, 2009 at 19:33
0

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

0

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.

Not the answer you're looking for? Browse other questions tagged .