Vim Plugin Help: Automatically insert a closing parenthesis with a cursor in the middle

I found some topics, but they are 3 years old. Does anyone have a plugin to recommend for vim that when you enter { it will do:

 { ::cursor here:: } 

Another useful thing: if I type if , it will do:

 if(::cursor here::) 

thanks

+4
source share
6 answers

The snipMate plugin will open. It is very customizable. There is a great screen-cast showing the plugin in action.

For example, in C with the snipMate plugin.

 if<Tab> would produce: if (::cursor:1::) { ::cursor:2:: } 

The first <Tab> would leave the cursor at :: cursor: 1 :: and the second <Tab> would lead you to :: cursor: 2 ::.

There should be something similar to bash scripts in snipMate, if it's not very simple to add one.

+2
source

I am using delimiteMate.vim . He handles this in a customizable and non-annoying way.

+3
source

I am using the following code (php-templates):

 function! EatChar() let l:char=getchar(0) return '' endfunction inoreabbrev function_ function <ESC>maa()<CR>{<CR><CR>}<ESC>`aa<CO>:call EatChar()<CR> inoreabbrev if_ if (<ESC>maa)<CR>{<CR><CR>}<ESC>`aa<CO>:call EatChar()<CR> inoreabbrev ifelse_ if (<ESC>maa)<CR>{<CR><CR>}<CR>else<CR>{<CR><CR>}<ESC>`aa<CO>:call EatChar()<CR> inoreabbrev for_ for (<ESC>maa; ; )<CR>{<CR><CR>}<ESC>`aa<CO>:call EatChar()<CR> inoreabbrev foreach_ foreach (<ESC>maa as $key=>$value)<CR>{<CR><CR>}<ESC>`aa<CO>:call EatChar()<CR> inoreabbrev while_ while (<ESC>maa)<CR>{<CR><CR>}<ESC>`aa<CO>:call EatChar()<CR> inoreabbrev <? <?php <ESC>maa ?><ESC>`aa<CO>:call EatChar()<CR> 
+1
source

This article shows a simple solution and provides links to some more reliable ones.

Here is part of the solution from this page:

 inoremap { {}<Left> inoremap {<CR> {<CR>}<Esc>O inoremap {{ { inoremap {} {} 
+1
source

There are many plugins and ftplugins that do this work. In most cases, new users' plugins are no better, as they often reinvent the wheel without taking into account the odd cases.

In the "old" material, I:

  • My code bracketing system , which simplifies the definition of mappings aimed at inserting balanced pairs of characters in the form of brackets.
  • and related functions , which are aimed at simplifying the task of determining evolving (*) code fragments (as a rule, control instructions). I have examples of using C ++ .

(*) Extension of mappings / abbreviations is prevented in the context of comments / lines, several surrounding forms of C-instructions are provided, and several stylistic options are supported (in the case of C-operator-operators, they are easily transferred to other languages)

+1
source

If you do not need the capabilities of various plugins for text fragments (or simply do not want to determine how to configure them for your needs) and have only a few (one or two) simple templates, you can simply write down its creation and copy it to the shortcut.

For instance,

 :nmap <leader>bo{<cr>}<esc>O 
0
source

Source: https://habr.com/ru/post/1299445/


All Articles