why unix | RBL service | netrs | please | ripcalc | linescroll
hosted services

hosted services

overview

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.

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
`` jumps to the position the cursor was at directly before performing a search
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.
dfc delete from cursor upto and including first occurrence of ic

visual mode

This is the mode that you enter when you select areas.

command action/purpose
ab/aB select () or {} (sometimes using % can make more sense)
: perform operation on block
:’<,’>;s/foo/bar/g perform regular expression substitution 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

registers

Registers are perhaps the most interesting part of vim for me. Of late I have read a part of the manual which explains to me how to avoid overwriting the yank "buffer" when deleting.

vim has a number of registers which are used for various operations automatically.

name purpose
"" The unnamed register
"0 to "9 10 numbered registers
"- The small delete register
"a to "z or "A to "Z 26 named registers
":, "., "% and "# four read-only registers
"= the expression register
"*, "+ and "~ The selection and drop registers
"_ The black hole register
"/ Last search pattern register

To access a register you simply use " followed by the register name before performing your action, so, to yank into the '''a''' register, you would do "ayy and to put from the '''a''' register you do "ap. Simple yet so very helpful if you do not wish to overwrite a buffer/register.

copy register

A common task that I find myself doing is yanking some text and wanting to copy that to another register, so here's how that's done:

:let @a=@"

This copies the default register to the register named '''a'''.

It's quite useful when you wish to duplicate that which is in the default, unnamed register.

searching, using registers

Often I like to use * and # to search forwards and back for that which is under the cursor. Though, if what I want breaks a word boundary then this may not be so simple. There is however, a handy solution.

"ayW # put the contents up to the end of the W boundary into the 'a' register
/[ctrl]-r a # put the contents of the 'a' register into the search string

This handy ctrl-r trick to source the register during a search is transferable to the : prompt too.

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 _no_list 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

maps

maps are a very useful if you wish to carry out one or more actions based on one or more key strokes. For example, I have to regularly reply to emails, which often follow the same format:

Hi Bob,

Thanks

I can pretty much generate all this from the headers using a map activated from the pressing ^C followed by t, so here's how I do that.

map ^Ct         /^To:^Mwwyw}o^M^MThanks^M^[{&lt;up&gt;iHi ^[p$i,&lt;down&gt;

In the above, ^M and ^[ are created by pressing ^V followed by enter and [ respectively.

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="">\, which should save some keyboard gymnastics.

abbreviate #i #include
abbreviate #d #define

abbreviate <a href=" <a href=" href=""></a><left><left><left><left><left><left>
abbreviate <img src="" <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 <code> <code></code><left><left><left><left><left><left><left>
abbreviate <pre> <pre></pre><left><left><left><left><left><left></pre></code>/>"></a>"></a>

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.

  1. screen captures of performing regular vim work
  2. you tube video showing vi to be suitable for editing python code (but it’s code agnostic)
  3. another example of vim in use
  4. 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
  5. demo of using vim and rails.vim to get writing pages quickly
  6. vim editor tutorial (1)
  7. vim editor tutorial (2)

reference cards

  1. Laurent Gregoire’s refernce card
  2. Donald J. Bindner’s reference card
  3. http://www.cafepress.com/vimrefmug
  4. guido’s card
  5. 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.

matrix screen saver

Download, source it, then type :Matrix.

easter eggs

Vim has some surprising Easter eggs

command comments
:help 42
:help holy-grail
:help!
:help map-modes see comment below the table about :nunmap
:help UserGettingBored
:help spoon
:help showmatch read the note
:Ni! read the alert
:help bar French

Just show me some other text editor that can do all the above - and this page just scratches the surface of what vim can do.

some tips

** (gvim:19530): WARNING **: Unable to create Ubuntu Menu Proxy: Timeout was reached

Well, it has annoyed me for some months and decided to look into this. The solution was to add the following alias to .bash_aliases:

alias gvim="UBUNTU_MENUPROXY= /usr/bin/gvim"

That's right, just unset the UBUNTU_MENUPROXY Environment variable.

how to fold

Of late, I've had to do more python than I would normally. Python doesn't have brace blocks like c, java or perl. Normally, with curly brace block languages you can use:

set foldmethod=syntax

This sets the fold enclosure to be marked by { }. Python, however does not use curly braces so the fold method needs to be set differently:

set foldmethod=indent

If you want to open all the folds

zR

or, to collapse

zc

and open

zo

how do i jump?

A wonderful feature of vim is jumps. When editing a editing is is highly useful to return to a position where you were last editing, or even before that. vim builds up a list of your edit locations whilst you are working. These can be viewed with :jumps.

   3   200    4 {"silence-member", no_argument,       NULL, 'S'},
   2   160   50 (void) fputs (_("  -S, --silence-member          silence group/
   1   226    3 sort_mode = true;
>  0   233    5 if (sort_mode && read_only) {
   1     1    0 /*
   2    83   26 static bool silence_member = true;

To go forward, press CTRL-I (tab), to go backwards, CTRL-O.

I can't recommend this enough.

word processing

https://www.maketecheasier.com/turn-vim-word-processor/