Vim Tips Wiki
(Reviewed and modified. Please see if you like it and revert if necessarry...)
(let references link)
Line 70: Line 70:
   
 
== References ==
 
== References ==
* VimTip855
+
* [[VimTip855]]
* VimTip630
+
* [[VimTip630]]
   
 
[[Category:C]]
 
[[Category:C]]

Revision as of 08:41, 25 July 2007

A simple solution

When you type an open brace, this will automatically insert a closing brace on the same line, after the cursor. If you quickly hit <return> after the open brace, (to begin a code block,) the closing brace will be inserted on the line below the cursor. If you quickly press the open brace key again after the open brace, Vim won't insert anything extra, you'll just get a single open brace.

   :inoremap { {}<left>
   :inoremap {<cr> {<cr>}<esc>O
   :inoremap {{ {

Here are similar mappings for other "paired" characters: brackets and parentheses.

   :inoremap [ []<left>
   :inoremap [<cr> [<cr>]<esc>O
   :inoremap [[ [
   :inoremap ( ()<left>
   :inoremap (<cr> (<cr>)<esc>O
   :inoremap (( (

This one is useful for adding C-style comments. If you don't want it to take effect, type your mapleader character first. (Usually that's '\'.) :help mapleader

   :inoremap /* /**/<left><left
   :inoremap /*<space> /*<space><space>*/<left><left><left>
   :inoremap /*<cr> /*<cr>*/<esc>O
   :inoremap <leader>/* /*

Similar mappings might be useful for quotes, but they might get in your way depending on the type of file you're editing. Some languages use duplicate single-quotes a lot, and some pair the backtick with the quote. For these situations, you might want to put similar commands into language-specific files. For example, this quote-completer for GNU M4 might live in ~/.vim/after/ftplugin/m4.vim. :help after-directory

   :inoremap ` `')<left>
   :inoremap `<cr> `<cr>'<esc>O
   :inoremap `` `

More advanced solutions

If you want something more complex and configurable, there are a number of different scripts that accomplish this task.

ftplugins

Luc Hermitte has some very advanced and smart ftplugins for editing C & C++ files. Those scripts give advanced brace-handling features like markers support (placeholders in another terminology), several things can be easily tweaked (whether we want newlines or not before the curly-brackets, ...), the abbreviations are buffer-relative (which is necessary to have "for" expand into diffrent things according to the filetype of the buffer edited), and more. See also script#50 and script#336.

ReplaceCurly script

This script, originally VimTip855, operates only on braces, but is smarter about detecting when it should act. It will not take effect when editing comments, strings and lines containing the the word "new." (This is useful array initialization, e.g. for string[] myArray = new string[] {"a", "b"}.)

 imap { <esc>:call ReplaceCurly()<cr>cl 
  
 function! ReplaceCurly() 
   imap { { 
   " only replace outside of comments or strings (which map to constant) 
   let elesyn = synIDtrans(synID(line("."), col(".") - 1, 0)) 
   if elesyn != hlID('Comment') && elesyn != hlID('Constant') && match(getline("."), "\\<new\\>") < 0 
     exe "normal a{" 
     " need to add a spare character (x) to position the cursor afterwards 
     exe "normal ox" 
     exe "normal o}" 
     exe "normal kw" 
   else 
     " need to add a spare character (x) to position the cursor afterwards 
     exe "normal a{x" 
   endif 
   imap { <esc>:let word= ReplaceCurly()<cr>cl 
 endfunction 
 
 "Surround code with braces 
 nmap <leader>{} O{<esc>ddj>>ddkP 
 vmap <leader>{} <esc>o{<esc>ddgv>gvdp 

In the above script, Richard Willis suggest changing each of the imap lines to avoids clobbering the unnamed register:

 from: imap { <esc>:let word= ReplaceCurly()<cr>cl
 to  : imap { <esc>:let word= ReplaceCurly()<cr>"_cl 

References