Efficient Editing With vim
Use Vim Like A Pro
Two important concepts in vi:
marks and registers.
marks: start with ` and '
ma set mark '''a''' at current cursor location
'a jump to line of mark a (beginning of line)
`a jump to position of mark a
d'a delete from current line to line of mark a
d`a delete from current cursor position to position of mark a
c'a change text from current line to line of mark a
y`a yank text to unnamed buffer from cursor to position of mark a
'' jump back (to line where jumped from)
`0 jump to position in last file edited (when exited Vim)
`` jump back (to position where jumped from)
:marks list all the current marks
registers start with ", use "* register to communicate with system clipboards.
There are nine types of registers:ATTENTION: 26 named registers, upper case is used to append contents, not different registers.
1. The unnamed register ""
2. 10 numbered registers "0 to "9
3. The small delete register "-
4. 26 named registers "a to "z or "A to "Z
5. four read-only registers ":, "., "% and "#
6. the expression register "=
7. The selection and drop registers "*, "+ and "~
8. The black hole register "_
9. Last search pattern register "/
registers manual
fast notes for registers:
When you copy and cut stuff, it gets saved to registers. You can pull stuff from those registers at a later time.
:reg - show named registers and what's in them
"5p - paste what's in register "5You can also record a whole series of edits to a register, and then apply them over and over.
qk - records edits into register k
(q again to stop recording)
@k - execute recorded edits (macro)
@@ - repeat last one
5@@ - repeat 5 times
"kp - print macro k
(e.g., to edit or add to .vimrc)
"kd - replace register k with what cursor is on
Tips And Tricks
The * and # Keys
In normal mode you can use * and # to search for the word under the cursor. * search forwards, # backwards.
The . Key
Also useful is the . key. In normal mode it repeats the last change. Very useful for XML editing and where you have to do similar stuff.
The % Key
Pressing the % key in normal mode while being on a parenthesis or a similar construct jumps to the opposite parenthesis. There are a couple of plugins that extend that functionallity but the default behavior is usually good enough for most of the programming languages.
The <> Keys
When working with Python you often have to indent or outdent a couple of lines. Just mark them using the visual mode and press > to indent them. press 4< to outdent them 4 steps etc.
Advanced Undo Features
Vim has a different system of undoing changes. The normal undo is u in normal mode and ^r is redo. But what happens if you undo a few things and then change something? A normal editor forgets about the new changes. It's not possible to redo that again.
In Vim it's different. Vim starts a new undo branch. Using :undolist you can have a look at the possible undo states. With :earlier 20s / 1m / 2h you can then go back 20 seconds, one minute etc. Traveling forward in time works using :later and a timedelta as argument.
The Search Feature
I really like the firefox search feature. Just hit "/" and you can search in the current file and get the results in real time. That works with vim exactly the same. All you have to to is to either add :set incsearch into your vimrc or type it into the command prompt. Using n you can go to the next result. If you want to have all the search results highlighted use :set hlsearch. Hiding the results works using :nohl
Closing XML Tags
If you have the closetag.vim plugin installed (link above) you can add this to your vimrc in order to get the feature working: autocmd FileType html,xhtml,xml source ~/.vim/scripts/closetag.vim (update the path to your installation and your filetypes of course) Once this is done you can close open tags using ^_.
Regexp Replacement after Search
If you searched for a text using /foo and you now want to replace the found results with something you don't have to write this regexp again. Just do :%s/
Using Bookmarks
Bookmarking in vim is darn easy. If you are on the current line just bookmark it with mX where X is a lowercase letter from a to z. Go to that mark using 'X where X is the same letter again. Using '' you can jump back to the position you were before jumping to the bookmark. You can get the list of bookmarks using :marks.
The Vim File Browser
The Vim File browser is a nice thing. If you open a file using :e and that file is a directory you get a nice file browser for the files in that directory. If you are a python developer you probably want to filter some files out (*.pyc etc). Add this to your vimrc: let g:explHideFiles='^\.,.*\.pyc$' This hides pyc files as well as hidden files.
The Wildmenu
The best vim feature is the wildmenu. Add a set wildmenu to your vimrc and discover the possibilities of filesystem surfing ^^ Enter :e in the command line and press ^D. Vim will show you all possibitilites in a nice little window. By entering the start of a filename and pressing tab it completes for you then. If it was a folder you can now press ^D again to get the contents. Once you finished the command this window will disappear again and you can continue working. Works of course for all commands not only the open command.
Vim Completion Features
Last tip but maybe the most powerful one :). If you have vim7 and omni completion for your language you can use ^X^O to get the completion similar to intelli sense or how it's called. Note that the default color (pink) can be overridden in the theme. In my fruity.vim file i use this to get white letters on a dark red background: hi Pmenu guifg=#ffffff guibg=#cb2f27
Also nice is ^N. It looks up all used words in the open buffers and presents then in a dropdown completion. Useful if you have long variable names and don't remember them. Just type in the start of the variable name and press ^N. Vim will either complete it or show the list of possibilities.
No comments:
Post a Comment