我发现了一些关于这个问题的帖子,但它们已经三年前的内容了。有没有什么插件可以推荐给Vim用户,使得当我输入{
时,它会执行以下操作:
{
::cursor here::
}
另一个有用的事情是拥有:
if(::cursor here::)
在输入
if
后面,请按照以下步骤进行操作。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>
这篇文章展示了一个简单的解决方案,并提供了一些更为强大的链接。
以下是该页面的部分解决方案:
inoremap { {}<Left>
inoremap {<CR> {<CR>}<Esc>O
inoremap {{ {
inoremap {} {}
如果您不需要各种文本片段插件的全部功能(或者不想弄清如何配置它们以满足您的需求),并且只有几个简单的模板,那么您可以为每个模板创建一个映射。例如:
:nmap <leader>b o{<cr>}<esc>O
{<Enter>}<^O>O
来达到同样的效果。 - MBO