Vim Tips Wiki
Advertisement

Proposed tip Please edit this page to improve it, or add your comments below (do not use the discussion page).

Please use new tips to discuss whether this page should be a permanent tip, or whether it should be merged to an existing tip.
created March 3, 2012 · complexity basic · version 7.0

Autocapitalizing the start of every sentence, a common function in cell phones, is not as trivial as one would think. Approaches such as mapping ". a" to ". A" in insert mode, using abbreviate, do not work, produce unexpected delay, or require extra keypresses. The strategy here is to interrupt insert mode on certain key presses, such as ., ?, !, etc and to catch subsequent keypresses with getchar().

The Vim cuteness here consists of:

  • getchar – wait for a single keypress, return the numeric key code. One side effect of this function is that pressing Esc will always produce a brief delay, since the function sees Esc as a modifier. To skip this delay, you can also press "/" to cancel capitalization.
  • Ctrl-R= - In insert mode, pressing Ctr-R and then = allows one to execute a function, insert the result, and then return to insert mode. This trick is used to return to insert mode. An alternative would be to use "startinsert", but one nice effect of the Ctrl-R method is that abbreviations (eg, "ab im I'm") still work! (Vim regards using a normal command and then starting insert as an interruption and won't trigger abbreviations. However, using Ctrl-R to call a function that executes normal commands is an odd loophole.)
  • "/<Del>" - It is possible using Ctrl-R to "insert" a delete command. One way this is helpful is that one can avoid various end-of-line complications by inserting this buffer character, "_".

Script

The following script can be placed in your vimrc.

"Inputing '/' cancels capitalizing without delay
function! CapNextKey(prev)
  redraw
  let input = nr2char(getchar())
  let input = (input == '/' ? "\e" : input)
  if input=~'[.?!\r[:blank:]]' "punctuations, return, spaces
    exe 'normal! i' . input . "\<Right>"
    return CapNextKey(input)
  elseif input=="\e"
    return "\<del>"
  elseif a:prev=~'[\r[:blank:]]'
    return toupper(input) . "\<del>"
  else 
    return input . "\<del>"
  endif
endfunction

function! InsertEnterCheck()
  let trunc = getline(".")[0:col(".")-2] 
  if col(".")==1
    return CapNextKey("\r")
  elseif trunc=~'[?!.]\s*$\|^\s*$'   "ie, 'text.[t]ext'
    return CapNextKey(trunc[-1:-1])
  else
    return "\<Del>"
  endif
endfunction
imap . ._<Left><C-R>=CapNextKey(".")<CR>
imap ? ?_<Left><C-R>=CapNextKey("?")<CR>
imap ! !_<Left><C-R>=CapNextKey("!")<CR>
imap <CR> <CR>_<Left><C-R>=CapNextKey("\r")<CR>
nmap o o_<Left><C-R>=CapNextKey("\r")<CR>
nmap O O_<Left><C-R>=CapNextKey("\r")<CR>
nmap a a_<Left><C-R>=InsertEnterCheck()<CR>
nmap A A_<Left><C-R>=InsertEnterCheck()<CR>
nmap i i_<Left><C-R>=InsertEnterCheck()<CR>
nmap I I_<Left><C-R>=InsertEnterCheck()<CR> 

Comments

Amazing. I have not read the code, but I guess it would always be active? Wouldn't you want a way to turn it on/off? JohnBeckett 01:00, March 4, 2012 (UTC)

Yeah, you're right, it's always active, and a future version should probably include an easy way to unmap and remove the autocommands, although that might increase the complexity even more. I use my vim on my cell phone a lot, and I'm not really sure how common that is and how useful people may find this, but it might be interesting to some as a way to use getchar() to create a "new mode" or a more interactive vim. -- q335r49
Advertisement