thinksimple.pl

Useful Vim commands for developers

17.06.2010 16:41

UPDATE: There’s now Vim commands for developers, Part 2 available!

I’m always on the look for new Vim tips and tricks, so here’s a new lot. Most of them are from this presentation but these are the ones I want to start using.

Marks

  • m<letter> – bookmark named letter
  • `<letter> – jumps to that bookmark
  • '<letter> – jumps to line with the bookmark
  • :marks – show current marks

Handy example:

ma #=> set a mark
c`a #=> change text from cursor to mark a
d`a #=> delete text from cursor to mark a
y`a #=> yank text from cursor to mark a
=`a #=> reformat lines from current to the one with mark a
  • `` – moves you between the last two locations

Text objects

They are complicated things which I probably thought I will never need, so here’s a Vim help link: :help text-objects. One useful tip:

  • ci{ – change text inside {} block
    Or, obviously, di{, yi{, ci( – you get the idea.

Undo – simple version

  • u – undo
  • CTRL + R – redo

Complex version: If you undo something and make a change, a new branch is created, so then you can do this:

  • g-, g+ – go to newer/older text state (through branches)
  • :earlier Ns,m,h – go to text state as it was N seconds/minutes/hours ago
  • :later Ns,m,h
  • :undolist – lists changes

Tabs

  • gt, gT – move around tabs

Completion

There are many types of completion and you can read about them under :help completion, but this is probably most useful:

  • CTRL + N – complete with keywords in the current file

You can also use omnicompletion, triggered by CTRL + X CTRL + O, but it doesn’t seem to be working for me.

Open file under cursor

gf, that’s easy. However, you can also do this: CTRL + W, CTRL + F and bang! split window open. This alone is the best tip in this post.

Comments

mario 17 Jun 17:26

I have been using vim for a while now and i still learn new things about this editor! Very useful tips.

Evaryont 17 Jun 17:46

If you like gf, check out gF - it also goes to the line number specified after the file name.

jiyuu 17 Jun 18:04

Sorry, I'm nitpicking but, omnicompletion is triggered by Ctrl + X Ctrl + O and you must have a ruby enabled vim (`vim --version` showing +ruby)

mileszs 17 Jun 18:44

Nice work! I added some of these to the vimtips account on Twitter (http://twitter.com/vimtips) via its website, http://vimtweets.com. I linked to you in the tweets.

squil 17 Jun 19:21

@jiyuu That's right, it's Ctrl + X Ctrl + O, you do need ruby enabled, but you also need some omnicompletion files which I must have wiped or something. Which explains why it doesn't work. Thanks :)

Sam 17 Jun 19:39

If you like text objects, you should also learn about the 't' and 'f' movement commands. For example:

ct" - change the text between the cursor and the next " in this line
cf" - change the text between the cursor and the next " in the line including the "

dtX - delete from the cursor to X
dfX - delete from the cursor finding(including) X

I remember them as t = to and f = find.

Sachin 18 Jun 00:18

Great Tips. You can get more looking the screencasts at http://VimCasts.org

Tom 19 Jun 13:14

I user SuperTab instead of Ctl+N

Comments are closed.