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
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
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>
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 {} {}
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:
(*) 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)
I use delimiteMate.vim. It handles this in a configurable and non annoying way.
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.
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