My 10 favourite Vim tips, features and plugins
Contents
- 1: Save time with macros
- 2: Block mode editing
- 3: Powerful search and replace
- 4: Repeat actions with the .dot key
- 5: Snippets
- 6: Switch between different open files (Lusty Juggler)
- 7: Command-T plugin
- 8: Taglist plugin
- 9: Auto format source code
- 10: Split windows
Tip 1: Save time with macros
A lot of text-editing tasks have a similar pattern. For example, doing the same kind of changes on multiple lines. While macros are not unique to Vim, the way you record and apply them is so fast, that you are really using them in practice. To record a macro simply press: (recording to register q)
qq
To stop recording:
q
And then execute the macro (from register q):
@q
Or execute it 10 times:
10@q
In the following example a macro gets recorded for the first line and is then applied to the next five lines:
Tip 2: Block mode editing
Many text files are structured column-wise and Vim has a powerful mode to edit them.Ctrl-V | Enter visual block mode |
Ctrl-Q | Enter visual block mode (Windows) |
I | Insert before selected block |
A | Append to selected block |
c | Change text in selected block |
r | Replace text in selected block with character |
Tip 3: Powerful search and replace
Example: Turn e11, e12, ... to e(1, 1), e(1, 2), ...
:%s/search/replace/g
:%s/\(\w\)\(\d\)\(\d\)/\1(\2, \3)/g
:%s/\(\w\)\(\d\)\(\d\)/\1(\2, \3)/g
\w | Word character |
\d | Digit |
\1, \2, ... | Replaced with the pattern between \( and \) |
Tip 4: Repeat actions with the .dot key
Simple actions can be repeated with the dot key:
.
The following example shows four function declarations that are turned
into a body for the actual implementation. Only the first function body
is typed, the rest is repeated by simply pressing the dot key.Tip 5: Snippets
Quickly insert often-typed text patterns with the snipMate plugin for Vim.For example to create the body of a for loop, type in for (in insert mode) and then tab. The snipMate will then insert the for loop body. It's a bit like IntelliSense. Pressing tab will cycle through the important parts of the for loop. You can customize your own snippets in a simple text file.
Tip 6: Switch between different open files (Lusty Juggler)
The LustyJuggler is a high speed buffer juggler plugin and highly recommended.Lusty Juggler allows you to change the current file with the home row keys. This is especially great for touch typists.
Tip 7: Command-T plugin
Fast file opening with only a few keystrokes. Just type in parts of the file name to locate and open the file. Link to Command-T.Tip 8: Taglist plugin
Source code navigation for all kinds of files with the taglist plugin:Tip 9: Auto format source code
Auto format with the = command. A few examples:= | Format selected area |
== | Format current line |
gg=G | Format entire file |
Tip 10: Split windows
Edit files with multiple views.Ctrl-W s | Split window |
Ctrl-W v | Split window vertical |
Ctrl-W c | Close split window |
If you have any questions or suggestions, send me a mail:
Copyright © 2011 Jan Zwiener