thinksimple.pl

Useful Vim commands for developers

June 17, 2010

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: 8

Vim commands for developers, Part 2

September 18, 2010

The longer I work with Vim, the more great stuff I discover. Here’s another chunk.

Did you know there are so many ways to go to insert mode?

  • i, a – insert before/after current character
  • I, A – insert at the beginning/end of the line
  • O, o – insert a line before/after current line
  • gi – go to Insert mode where you last stopped editing

Behold – Folding!

  • zf – fold selected block
  • :{range}fo[ld]
  • zo – open fold
  • :{range}foldo[pen]

Ever changed your .vimrc and had to close Vim to load it? Well no need for that any more!

:source $MYVIMRC

And our plugin of the day is… NERDTree! NERDTree is a project file browser, very useful!

Commands:

:NERDTree [start-dir|bookmark]  # opens NERDTree
:NERDTreeToggle [start-dir|bookmark]
:NERDTreeClose
:NERDTreeFind  # find current file in tree
o, go # open / open and leave cursor in NERDTree
t, T  # open in new tab / open in tab and keep focus on current tab
i, gi # split window
s, gs # vsplit window

Also – you do know about Pathogen, right?

If you missed out on Vim commands for developers, Part 1, here it is.

Comments: 1

vim find and replace

June 4, 2008

find each occurrence of ‘foo’ and replace it with ‘bar’ without asking for confirmation:

:%s/foo/bar/g

find each occurrence of ‘foo’ and replace it with ‘bar’ asking for confirmation first:

:%s/foo/bar/gc

find (match exact word only) and replace each occurrence of ‘foo’ with ‘bar’:

:%s/<foo>/bar/gc

find (case insensitive) and replace each occurrence of ‘foo’ with ‘bar’:

:%s/foo/bar/gci

find (case sensitive) and replace each occurrence of ‘foo’ with ‘bar’:

:%s/foo/bar/gcI

To insert a new line press ctrl+V, Enter – this will show something like ^M and this will be your new line.

vim tabs

June 4, 2008

Opens a new tab:

:tabnew filename 

Opens three tabs at start:

vim -p file1 file2 file3

Go to next/previous tab:

:tabn
:tabp
gt

Go to first/last open tab:

:tabfirst
:tabfir
:tablast

Move current tab to nth/last position:

:tabm n
:tabm

Show all open tabs:

:tabs

From: http://www.linux.com/articles/59533