This past semester I managed to do most of my coding in Vim (with the noted exception last minute adjustments of ARM assembly code in the lab, where none of the Raspberry Pi's had Vim installed). Now that the semester is over I've had a little time to reflect on what I might change to enhance my productivity or quality of life. My primary focus was on enclosing text.
I started with the basic stuff: parenthesis, brackets, and quotations auto-closing. While there are a plethora plugins that will do this (and likely well), I opted instead to do a little remapping in .vimrc.
There are the simple and obvious ones:
Closes the parenthesis and repositions the cursor in between them.
inoremap ( ()<Left>
Creates a new line between the curly brackets
inoremap {<cr> {<cr>}O
I stole this, so I don't really know why it works. The function is to skip over an closing parenthesis if it happens to be the next character.
inoremap <expr> ) strpart(getline('.'), col('.')-1, 1) == ")" ? "\<Right>" : ")"
Many of my remaps start with ";;" because I don't expect to actually need to type that in any code. I opted to use this to find the closing brackets
inoremap ;;) <esc>/)<Enter>a
The auto-enclose works pretty well (but does take some getting used to), but many times, I discovered, I needed to add quotes or brackets around text post ex facto.
For single words I used "vv" as the signal because I could think of no words that begin with "vv", and it reminded me of selecting words using visual mode.
inoremap vv" <esc>bi"<esc>ea"
To enclose whole lines, I just started with "vvv".
inoremap vvv( <esc>0i(<esc>A)
The real fun begins when we use visual mode. Here we can enclose the selected text regardless of new lines or whitespace.
vnoremap ;;( <esc>
<i(<esc>>a)<esc>
Even better, we can add language specific comment blocks to our code.
autocmd FileType cpp vnoremap ;;c <esc>
<i/*<space><cr><esc>>a<cr>*/<esc>
Here endeth the primer. Now go forth and experiment with your own .vimrc