Useful Vim commands for developers
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– undoCTRL + 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.
