Vim
From s5h.net
One of the most important tools of a unix administrator is ability to use a text editor. Perhaps the most popular text editor available on unix systems is the vi improved editor, or simply vim.
Here is my cheat sheet. When I find useful commands that reduce the time that I spend editing files (which can be a day long process) I put things here so that I can remember them later. The functions here are what I find highly useful for good editing. Having the ability to reduce the work involved in making many changes at once is advantageous, so here is my over view of the things which I think benefit the most when using vim.
^c indicates a control character for letter c.
Contents |
commands
The commands that I think are perhaps the most important of are the following
| command | action/purpose |
|---|---|
| {,} | moves to the beginning or end of paragraph at cursor |
| (,) | moves to the beginning or end of sentence |
| m | sets a bookmark in a-z and A-Z slots (help m) |
| ‘,' | jumps to bookmark in slot a-z and A-Z (help m). ` retains column position of mark |
| gg | jumps to start of document |
| G or :$ | jumps to end of document |
| :n | jumps to line n |
| z= | suggest spelling alternatives |
| *,# | go to matching word under cursor. this is useful for jumping to a function that you’re calling (* = forward, # = reverse ) |
| gg=G | apply tabulation rules to the current file |
| ga | shows the hex, decimal and ordinal values of the character under cursor |
| ctrl-a/ctrl-x | adds/subtracts to/from decimal under cursor |
| J | joins this line in the next (keeps inserts a space character between join |
| ^f, ^b | go forward or backwards by a page |
| 1,$!grep something | run the grep command on the full buffer (1,$) |
| :earlier/:later n | go earlier or later in the edit time line by n minutes/seconds. This is highly useful if you get lost in undo/redo. Define n as Nx, where N is the number of time units defined where x is either s for seconds, or m for minutes. |
visual mode
This is the mode that you enter when you select areas.
| command | action/purpose |
|---|---|
| ab/aB | select () or {} |
| : | perform operation on block |
| :’<,’>s/foo/bar/g | perform regular expression subsitution on block |
| gu / gU | switches the case of the visual block depending on the case of u |
| g? | rot 13 the block |
| ^v | visual block selection, rather than selecting by line this will select by rectangle config |
my own vim
The environment for vim that I find the most convenient can simply be stored in a text file in your home directory named ~/.vimrc.
| command | action/purpose |
|---|---|
| set spell | enables spell checker, right click, or z= to suggest alternatives |
| set number | show line numbers |
| set autoindent | automatically tab on code blocks |
| set smartindent | sets better indentation based on code |
| set ts=8 | sets the tab stops to 8 character spaces |
| set sw=8 | sets the autowrap to 8 characters |
| syntax enable | automatically highlight code keywords |
| set linebreak | automatically wrap whole words. this can be used rather than set line width |
| set fileformat=dos/unix/mac | sets the line breaks when saving |
| set modeline | this allows vim to process the vim:…: commands stored in files that you edit |
| set nolist | shows non-printable characters |
Recently I've been using autocmds for various file types like this
autocmd FileType perl colors torte
Which sets the color configuration for files that are perl sources. I have a couple of settings for text also
autocmd FileType mail,human set formatoptions+=t textwidth=72 nonumber
for code
| command | action/purpose |
|---|---|
| ]p | paste, but indent at the same time |
| % | go to matching (, {, [ etc |
| gd | go to definition or declaration |
| K | go to man page for the word under cursor |
| :mksession, vim -S Session.vim | save session and open the saved session |
abbreviations
Often its handy to change a sequence as you're typing a word, for example the sequence <a might be handy to turn into <a href=""></a>, which should save some keyboard gymnastics.
abbreviate #i #include abbreviate #d #define abbreviate <a <a href=""></a><left><left><left><left><left><left> abbreviate <i <img src="" /><left><left><left> abbreviate l" “”<left><left><left><left><left><left> abbreviate r" ” abbreviate l' ‘ abbreviate r' ’ abbreviate "" ""<left><left><left><left><left> abbreviate <? <?php?><left><left>
Be careful with the "" abbreviation since it can be annoying in other code, such as when writing $a = "". It might he helpful for yourself if this is changed to work only within html files by making use of the autocmd function.
autocmd
Following on from the above suggestion, this is perhaps the only way to implement the "" abbreviation in a useful way for anyone who has to work with strings in a programming/script language.
autocmd BufNewFile,BufRead *.htm,*.html abbreviate "" ""<left><left><left><left><left>
video links
These pages contain links to various external sites that have vim videos. These videos are for educational purposes and might be live demonstrations or talks describing how vim works.
| address | description of content |
|---|---|
| http://danielwebb.us/software/vim_demo/ | screen captures of performing regular vim work |
| http://www.youtube.com/watch?v=eScUUhCEPzs&NR=1 | you tube video showing vi tp be suitable for editing python code (but it’s code agnostic) |
| http://www.youtube.com/watch?v=IMC9PtXwCtI | another example of vim in use |
| http://www.youtube.com/watch?v=eX9m3g5J-XA | Bram Moolenaar talks about using vim effectively for text editing, or even how to use vim more effectively by people who use it extensively already |
| http://www.youtube.com/watch?v=30P8DSNOZuU | demo of using vim and rails.vim to get writing pages quicky |
| http://www.youtube.com/watch?v=XSXoap2h3Mw | vim editor tutorial (1) |
| http://www.youtube.com/watch?v=dsKGMxoydCc | vim editor tutorial (2) |
reference cards
| Laurent Gregoire’s refernce card |
| Donald J. Bindner’s reference card |
| vim reference mug |
| guido’s card |
| vimtips |
fun
Everyday vim surprises me with something new. Today it's in the form of a screen saver using the popular looking matrix green characters.
Download, source it, then type :Matrix.
