gvim makes you strong

After moving to an all-Linux workplace, I quickly learned to love the gvim text editor for its extensibility and ubiquity.  These are some of the tweaks I’ve found most useful.

This one is easy, but using bang to call shell commands is a nice time saver so you don’t have to switch windows:

:!ls .

You can use the % operator to refer to the currently open file:

:!ls -l %

If it is a long command string that you use frequently, you define functions in vimscript within ~/.vim/plugin:

fu! Cmd()
    let file = expand('%')
    execute '!ls -l '.file
endfu

fu! CmdWithArgs(arg1, arg2)
    echo a:arg1.' '.a:arg2
endfu

command Cmd call Cmd()
command -nargs=* CmdWithArgs call CmdWithArgs(<f-args>)

You can then call these functions via:

:Cmd
:CmdWithArgs one two

I use this all the time for commits, pushes, and pulls in git.  This is also handy when using <cfile>, which grabs the path currently under the cursor and can be used to quickly view images or other formats using command line tools.  For text files, this functionality is available out of the box; typing gf (goto file) with the cursor over a filepath will open the file in the same window, and <ctrl>-w gf will open it in a tab (gt to toggle between them).

You can inline python in vimscript:

python << EOF

import sys

EOF

You can also move a visual block of text (defined with <ctrl>-v) from one file to another:

:w>> path | '[,']d

I’m bad about keeping code to 80 characters a line, so this suggestion for some subtle highlighting in ~/.vimrc for when I run over is a nice reminder:

highlight OverLength ctermbg=#1F0A0A ctermfg=white guibg=#1F0A0A
match OverLength /\%79v.\+/

Other .vimrc additions that I always use are highlighting all search results:

set hlsearch
hi Search guibg=LightGreen ctermbg=LightGreen

and changing statusbar colors based on normal or insert mode:

set laststatus=2
au InsertEnter * hi StatusLine ctermbg=LightRed guibg=LightRed
au InsertLeave * hi StatusLine ctermbg=Black guibg=Black

I really like Todd Werth’s ir_black color scheme with taglist (for providing a list of function definitions) and pyflakes (a useful passive syntax checker for python).

For further reading, check out Andrew Scala’s Five Minute Vimscript or Steve Losh’s comprehensive Learn Vimscript the Hard Way.

Leave a Reply

Your email address will not be published. Required fields are marked *