tags:

views:

103

answers:

6

I found some threads on this but they are 3 years old. Does anyone have a plugin to recommend for vim that when I type { it will do:

{
::cursor here::
}

another helpful thing is if I type if it will do:

if(::cursor here::)

Thanks

+1  A: 

I use following code (php-templates):

function! EatChar()
    let l:char=getchar(0)
    return ''
endfunction

inoreabbrev function_ function <ESC>maa()<CR>{<CR><CR>}<ESC>`aa<C-O>:call EatChar()<CR>
inoreabbrev if_ if (<ESC>maa)<CR>{<CR><CR>}<ESC>`aa<C-O>:call EatChar()<CR>
inoreabbrev ifelse_ if (<ESC>maa)<CR>{<CR><CR>}<CR>else<CR>{<CR><CR>}<ESC>`aa<C-O>:call EatChar()<CR>
inoreabbrev for_ for (<ESC>maa; ; )<CR>{<CR><CR>}<ESC>`aa<C-O>:call EatChar()<CR>
inoreabbrev foreach_ foreach (<ESC>maa as $key=>$value)<CR>{<CR><CR>}<ESC>`aa<C-O>:call EatChar()<CR>
inoreabbrev while_ while (<ESC>maa)<CR>{<CR><CR>}<ESC>`aa<C-O>:call EatChar()<CR>
inoreabbrev <? <?php <ESC>maa ?><ESC>`aa<C-O>:call EatChar()<CR>
Nikolay Frantsev
+1  A: 

This article shows a simple solution and has links to some that are more robust.

Here is part of the solution from that page:

inoremap {      {}<Left>
inoremap {<CR>  {<CR>}<Esc>O
inoremap {{     {
inoremap {}     {}
Dennis Williamson
+1  A: 

There are many plugins and ftplugins that does the job. Most of the time newers plugins are not better as they often reinvent the wheel without taking odd cases into account.

In the "old" stuff, I have:

  • My code bracketing system that simplifies the definition of mappings aimed at inserting balanced pair of brackets-like characters.
  • and related functions that aim at simplifying the task of defining evolved (*) code-snippets (typically the control statements). I have examples of use for C++.

(*) The expansion of mappings/abbreviations is prevented in comments/strings context, several surrounding forms of the C control-statements are provided, and a few stylistic options are also supported (in the case of the C control-statements; they are easy to transpose to other languages)

Luc Hermitte
+3  A: 

I use delimiteMate.vim. It handles this in a configurable and non annoying way.

Randy Morris
+1  A: 

Check out snipMate plugin. It is very customizable. There is a excellent screen-cast showing the plugin in action.

For example in C with snipMate plugin.

if<Tab>

would produce:

if (::cursor:1::) {
    ::cursor:2::
}

First <Tab> would leave the cursor at ::cursor:1:: and second <Tab> would take you to ::cursor:2::.

There should be some thing similar for bash scripts in snipMate, if not it is very simple to add one.

shoban
Very useful.. Seems to have some problems though setting up a { trigger like above (without requiring a for statement or the like) Have you had any luck doing that or do you just go with the provided snippets?
stormist
I usually use the default ones. But I did try adding a snippet for "{", it did work. I added this snippet http://vim.pastey.net/132128 to ~/.vim/snippets/sh.snippets (Shell scripting snippets).
shoban
A: 

If you don't need the power of various text snippets plugins (or just don't want to figure out how to configure it to your needs) and have only a few (one or two) simple templates, you can just record its creation and map it to shortcut.

For example,

:nmap <leader>b o{<cr>}<esc>O
ib