Monday, 9 March 2015

Vim tips

The vim text editor has many features, and I have often felt that my use of it is suboptimal. Here are some features I found out about that are useful which I wish I knew about earlier.

The ":scriptnames" command. This shows a list of the scripts that vim read when starting up. Useful for debugging vim configurations.

To show where a particular vim variable got its value, use ":verb set". This is useful when a vim setting fails to have to value you thought you gave it, because it has been overridden somewhere else.

Sometimes when wanting to open a new line above the one I was on, I would type ESC O, and this would break in a confusing way when I tried typing the contents of the new line. I didn't even know how I got into this state when it happened. Eventually, I realised this could be fixed by using "set ttm=20" in my "vimrc" file. (the ttm setting is also known as ttimeoutlen).

The "a" flag of "formatoptions" is very useful for automatically formatting text as you type it. Turn off for filetypes where newlines are important, for example with "autocmd FileType sh setl fo-=a" for shell scripts.

The "\zs" and "\ze" sequences in regexps are very useful for inserting text before or after certain places. For example, ":%s/\zebanana/the /g" inserts the text "the " before any occurrence of the text "banana". Without this feature you would have to type "%s/\(banana\)/the \1/g", which is harder.

"gv" to reselect the last visual selection - for example, to perform a search and replace on it, or a shift.

Remapping the Caps Lock key to Esc is very helpful - before this I was starting to get a sore little finger by constantly pressing "Control-C".

I have the following to turn off "comment leader" insertion, (like "*" for "/* ... */" comments in C), which I found very difficult to turn off and keep off.
    set fo-=c
    set fo-=r
    set fo-=o

    " Disable "comment leader" garbage
    set com=

    I have the following to disable the gaudy bright yellow search matching:

    set hlsearch
    set hl=ls

    I find the following useful for reformatting lines:

    map <CR> i<CR><Esc>

    This is useful for editing numbers leading zeroes in dates:
    set nrformats-=octal

    No comments:

    Post a Comment