What Vim features do you use?

13

9

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!

Frew Schmidt

Posted 2009-07-15T14:43:40.427

Reputation: 1 062

Question was closed 2011-08-31T04:07:09.000

Note my comment below, and you won't even have to type gv. :) – Nathan Long – 2011-02-01T17:41:26.227

1

For historical purposes, here's a link to the original SO question:

http://stackoverflow.com/questions/95072/what-are-your-favorite-vim-tricks

– Jax – 2009-07-16T01:20:57.553

Answers

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.

karlgrz

Posted 2009-07-15T14:43:40.427

Reputation: 259

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 Schmidt – 2009-07-15T17:46:14.057

Add 'c' to the end of that to make the command interactive. e.g. :%s/x/y/gc – sml – 2010-09-21T06:21:01.543

1Note 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 – 2011-02-01T17:40:11.000

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.

Nathan Long

Posted 2009-07-15T14:43:40.427

Reputation: 20 371

2Also keep in mind >> and << which can manually do (un)indenting. – Frew Schmidt – 2009-07-15T17:46:50.717

@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 – 2011-02-01T17:34:42.303

6

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

Kristo

Posted 2009-07-15T14:43:40.427

Reputation: 614

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 – 2011-02-01T17:38:50.800

Oooh! That's new to me! – Frew Schmidt – 2009-07-17T22:09:31.223

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 %

Lucas Jones

Posted 2009-07-15T14:43:40.427

Reputation: 669

You might want to explain the "sudo tee" trick in here :-) Also I like to do :!sort after highlighting variable definitions and whatnot. – Frew Schmidt – 2009-07-15T14:57:40.483

You can really turn Vim into a great "scriptable" editor using :! and some shell/Python/Perl/other scripts. – Jonas – 2009-07-15T15:05:13.467

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 – 2011-11-16T03:06:23.210

5

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

Jonas

Posted 2009-07-15T14:43:40.427

Reputation: 1 091

1And don't forget it's sister command, #, which does the same thing but goes backwards instead of forwards! – Frew Schmidt – 2009-07-15T14:58:29.513

4

.

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

Phoshi

Posted 2009-07-15T14:43:40.427

Reputation: 22 001

this is surprisingly powerful, but it takes a bit of vim-ing before you can really get good with it. – aaron – 2011-06-13T04:26:12.957

3

syntax color highlighting

mas

Posted 2009-07-15T14:43:40.427

Reputation: 2 431

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.

erichui

Posted 2009-07-15T14:43:40.427

Reputation: 844

Recently I am using gw instead of gq because it keeps the cursor position – SergioAraujo – 2018-02-01T22:10:19.660

Oooh, I'd totally forgotten about this! – Frew Schmidt – 2009-07-16T01:32:13.323

Why, oh why, can't the more popular email clients support similar functionality? – erichui – 2009-07-16T01:50:22.283

2

ZZ save and exit (instead of :wq!)

dogbane

Posted 2009-07-15T14:43:40.427

Reputation: 3 771

Huh? What about x? – DevSolar – 2011-09-16T07:17:51.463

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.

Jack M.

Posted 2009-07-15T14:43:40.427

Reputation: 3 133

I use all of these except for abbr. I tend to make a lot more macros than abbreviations. – Frew Schmidt – 2009-07-17T21:53:10.483

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.

secureBadshah

Posted 2009-07-15T14:43:40.427

Reputation: 1 411

Ah, ]] and [[ are new to me. That's cool! – Frew Schmidt – 2009-10-21T19:33:23.657

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.

wbogacz

Posted 2009-07-15T14:43:40.427

Reputation: 302

0

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

akira

Posted 2009-07-15T14:43:40.427

Reputation: 52 754