day
day

Reputation: 1157

Vim Auto Closing Parenthesis Configuration in vimrc

The title of the question might be similar to some questions here.

But, what I am asking is specific and different: I have searched some .vimrc files regarding auto closing of parenthesis, as I do not want to install plugin.

So far, this link seems to fit the best: http://vim.wikia.com/wiki/VimTip153

I tried this mapping below, but it is not working in my vim. It seems to be only working in Perl. Could anyone please let me know what needs to be configured to enable the mapping to work in any vim environment such as normal text?

=============Below is the configuration================ This mapping is not working in my vim editor. Do I miss some package?

inoremap { {<CR><BS>}<Esc>ko

to be used in conjunction with my autoindent setup:

set expandtab
set shiftwidth=4
set smarttab
set autoindent
set smartindent

Here is what i want:

{
<cursor here ready for coding>
}

Upvotes: 0

Views: 375

Answers (1)

Darkwater
Darkwater

Reputation: 1386

This is what I use:

inoremap {} <CR>{<CR>}<Up><CR>

After typing {}, it puts them on their own lines with the cursor on a blank line inbetween, like so:

void something(){}_

becomes

void something()
{
    _
}

(_ indicates cursor position)

This assumes you can use the cursor keys in insert mode. If not, you could use the following mapping:

inoremap {} <CR>{<CR>}<ESC>O

I recommend mapping {} over { because this makes it easier to use { in cases where you actually need the symbol itself. Also, if you want to type {} literally, you can just pause for a while between typing the latter character.

Upvotes: 0

Related Questions