my vim gems

Here are some personal tips I’ve wrapped up:

1. Use a plugin manager, it’ll make your life much easier. I use VAM which offers good support for git and mercurial plugin repositories, but also offers its own curated plugin list and installation from self-contained directories.

2. For the following tips, simplified clipboard usage is required:

set clipboard=unnamedplus " for simplified clipboard copy/paste

3. Use vim-shell for fullscreen GVIM:

call vam#ActivateAddons(['github:xolox/vim-shell'])

4. Put the following command on your .vimrc so that on fullscreen you don’t get weirdly colored borders in GVIM:

" Fix borders of fullscreen GUI
if has('gui_gtk') && has('gui_running')
    let s:border = synIDattr(synIDtrans(hlID('Normal')), 'bg', 'gui')
    exe 'silent !echo ''style "vimfix" { bg[NORMAL] = "' . escape(s:border, '#') . '" }'''.
                \' > ~/.gtkrc-2.0'
    exe 'silent !echo ''widget "vim-main-window.*GtkForm" style "vimfix"'''.
                \' >> ~/.gtkrc-2.0'

As GVIM starts, that will generate a ~/.gtkrc-2.0 file that will fix background color for GVIM GTK windows taking the current background color of your colorscheme (so, set the colorscheme before this code).

Example output:

style "vimfix" { bg[NORMAL] = "#002b36" }
widget "vim-main-window.*GtkForm" style "vimfix"

If you use .gtkrc-2.0 with other configurations beyond this, you should adapt the script, this is the only use I have for .gtkrc-2.0.

5. Export code with syntax highlighting to HTML. I’ve aways used :TOhtml for that, but I’ve switched to the more powerful format.vim. For install, on my .vimrc I do (using VAM):

call vam#ActivateAddons(['hg:'])
call vam#ActivateAddons(['hg:'])

It supports exporting not only to HTML, but also \(\LaTeX\) and other formats. I also add the following extra configuration for exporting with the font I’m currently using:

" format.vim Setup
let g:format_HTMLAdditionalCSS = '@import url(;' .
            \' body { font-family: "Droid Sans Mono"; font-size:11pt; line-height: 120%; -webkit-font-smoothing: antialiased }'

6. Create Gists from your code with gist.vim:

call vam#ActivateAddons(['github:mattn/gist-vim']))

7. With the help of format.vim and gist.vim, you can use the web site to host code snippets using your colorscheme. To do that, add the following command (Blocks) to your .vimrc:

" Gist it to
command! -range=% Blocks
            \ <line1>,<line2>Format format | f index.html | exe 'Gist -a' | bd!                                       |
            \ let @+ = '' . matchstr(@+, '\zs\d\+\ze') . '/' |
            \ redraw | echomsg 'Done: ' . @+ | setlocal nomodified

This will create an anonymous gist (Gist -a) from your current selection (or if there’s no selection, use all the buffer) exported to HTML, and then use as the viewer of that HTML, after the command finishes, the temporary buffer with HTML output is destroyed and you get the url of your code snippet (on your clipboard) using your exact colorscheme.

For example,

  • generated with selection (:'<,'>Blocks): 6672885
  • generated from buffer (:Blocks): 6672894

8. Make the QuickFix window take all the lower section of the screen when there’re multiple windows open, so that it stops showing up only bellow a particular window:

" QuickFix Window
au FileType qf wincmd J " Open QuickFix horizontally

9. Paste block selection without overwriting contents:

function! FancyPaste(reg)
    let paste = split(getreg(a:reg), '\n')
    let spaces = repeat(' ', virtcol('.')-1)
    call map(paste, 'spaces . v:val')
    call append(line('.'), paste)

" Paste block while inserting new lines to hold it
nnoremap <Leader>p :call FancyPaste('"')<CR>
nnoremap <Leader>P :call FancyPaste('+')<CR>

The previous creates two mappings, <Leader>p and <Leader>P, that will paste text indenting it according to the position of the pasting location, the first map is for yanked text in VIM, the second is for text from other applications in clipboard.

10. Edit the contents of special buffers:

" Edit command output in a buffer
command! -nargs=+ BufOut redir => bufout | silent <args> | redir END | new | call append(0, split(bufout, '\n'))

" :Me[ssages] as shorthand for `:BufOut :messages' (the dominant use case of :BufOut)
command! -nargs=0 Messages :redir => bufout | silent :messages | redir END | new | call append(0, split(bufout, '\n'))

The first command (BufOut) is for getting output of a general special buffer passed as argument into a new buffer. The second (Messages) is a shortcut for getting output from the messages buffer, the one with error logs, etc.

For reference, this is my .vimrc.