We held a Vim show and tell at today’s dev discussion in Boston. We went around the room showing plugins, configuration, and general Vim tips in the hopes that some things might wind up in the thoughtbot dotfiles or we might make some people’s lives a little better in Vim.
Plugins We Like (at least some of us)
- vim-vinegar - Adds some niceties to the directory listings (netrw, really) nicer.
- vim-tmux-navigator - Seamlessly navigate Vim and tmux splits.
- vim-rfactory - Early stages of development, but it adds support for navigating directly to traits and factories.
- syntastic - Syntax checking for many languages. Already in the dotfiles, but worth checking out if you don’t use those.
@christoomey featured prominently in our discussion of vim plugins. Thanks, Chris!
** Configuration Tips **
Each of us shared some snippets of vim script that we found useful.
I started with help system speedups, that I lifted from @chistoomey, of course. Vim’s help system is great. Get in there. These bindings make it a little bit quicker to navigate around. You can hit enter
on a highlighted word in help to jump right to the help for that. backspace
takes you back where you were. q
closes help.
autocmd filetype help nnoremap <buffer><cr> <c-]>
autocmd filetype help nnoremap <buffer><bs> <c-T>
autocmd filetype help nnoremap <buffer>q :q<CR>
You can change your cursor shape depending on the vim mode you are in. This is free in gVim/MacVim, but you can approximate that behavior in the terminal with this totally logical and obvious bit of vim script (courtesty of @gfontenot):
" Change cursor shape to an underscore when in insert mode
let &t_SI = "\<Esc>]50;CursorShape=2\x7"
" Change back to a block in normal mode
let &t_EI = "\<Esc>]50;CursorShape=0\x7"
@djcp told us of the power of mksession (:help mksession
). It will persist the current state of you editing session (splits, files, cursor positions, etc) to a file which can then be reloaded later. @djcp maps saving to F2
and restoring to F3
with the script below. You could conceivably persist and restore to a file based on current working directory to save multiple sessions. The file itself is interesting to look at to see how vim sets up splits and such.
map <F2> :mksession! ~/.vim_session <cr> " Quick write session with F2
map <F3> :source ~/.vim_session <cr> " And load session with F3
Both @Paul_Smith and @djcp talked about settings for scrolloff
(:help scrolloff
). Paul sets his to ‘5’ so he always has 5 lines of context at the bottom of a window. DJCP sets his to 999, which causes the current line to be centered. @jferris thinks that’s crazy.
@blakewilliams showed us a bunch of leader commands he likes, but he did it very quickly and I didn’t get them all down. Maybe he can share some of his favorites with us here, or you can peruse his dotfiles.
@jferris schooled us all on grepprg
. The thoughtbot dotfiles set this to the silver searcher (ag
) as long as that’s present, but many of us also use the Ag
plugin. It seems that’s largely unnecessary and could be completely replicated by setting :grep
(configured to run via ag
) to automatically open the quickfix window. Configuring :grep
properly beats using a plugin because lots of internal vim things use :grep
.
Joe also tried again to convert us all to relative number line numbering (:help relativenumber
).
General Vimery:
We discussed some general vim tips, mostly driven by @jferris.
- Marks.
mm
sets a mark namedm
. Go about your business in the file and hit'm
to return to the mark namedm
. Marks can actually be returned to with backtick or apostrophe and have slighly different semantics (:help marks
). - Magic Marks.
<backtick><backtick>
returns to last jump position.'>
and'<
return to end and beginning of last visual selection. Lots more… see:help marks
. - The paragraph motion -
dap
to delete a paragraph, etc. Great for moving methods around. See:help text-objects
for a lot more. -
,
(if you haven’t used it as your leader) does your last linewise find (not search) backwards. -
*
searches for the word under cursor.#
does the same, backwards.
Got any vim tips of your own you’d like to share?