16

I've searched near and far, and not found a plugin that can simply auto-close a set of parenthesis like Textmate. For example:

Vim     : (*manually close parens* → )
Textmate: (*Auto closes parens*)

If you can describe a plugin for this, I will be very helpful. Thanks!

dan1st
  • 12,568
  • 8
  • 34
  • 67
beakr
  • 5,709
  • 11
  • 42
  • 66

5 Answers5

13

For those of us who want to go the plain vim way:

ino " ""<left>
ino ' ''<left>
ino ( ()<left>
ino [ []<left>
ino { {}<left>
ino {<CR> {<CR>}<ESC>O

This autocomplete in insert mode. Keep in the vimrc to avoid typing it everytime and when we don't want the mapping, we need to escape it using ctrl - v before typing the mapped char of ( { etc.

dlmeetei
  • 9,905
  • 3
  • 31
  • 38
10

I use AutoPairs. You can get it here:

https://github.com/jiangmiao/auto-pairs.git

If you read the docs, it has a lot of options which cover most eventualities.

phildobbin
  • 814
  • 8
  • 9
  • auto-pairs might break some key maps. For instance å does not work on Swedish keyboards (but not Å). – lindhe Dec 05 '14 at 20:36
9

Try delimitMate:

https://github.com/Raimondi/delimitMate

Some plugins listed here as well.. Plus instructions on setting it up yourself:

http://vim.wikia.com/wiki/Automatically_append_closing_characters

Steve Clarke
  • 139
  • 2
1

I'm maintaining a plugin that simplifies insertion of balanced bracket-like characters, and that even supports surrounding of words/lines/selection.

https://github.com/LucHermitte/lh-brackets/#the-bracketing-subsystem

The default bindings for C & C++ are described in lh-cpp page.

Luc Hermitte
  • 31,979
  • 7
  • 69
  • 83
  • this is a good one, although it takes a bit to get through the docs since im using alot of lh stuff and its a bit overwhelming. I still havent quite figured out why it's generating extra artifacts after insertions but only in .vim language files. – osirisgothra Jul 26 '14 at 18:32
  • The extract stuff is certainly the placeholders. You can jump to the next placeholder with `` if you are using gvim, `` (IIRC) with vim. In C & C++, ';' will try to close all the placeholders after the cursor when it is followed by closing parenthesis and closing `"`. Now, if you have placeholders only in .vim files, and not in other files, we'll have to investigate further. We try do that tomorrow by email or through the tracker on google-code. – Luc Hermitte Jul 27 '14 at 13:57
0

Do it purely in vim without plugins and simulating all of the IDEs that exist out there.

" # Close brackets automatically, with return
inoremap {<cr> {<cr>}<C-O><S-O>
inoremap (<cr> (<cr>)<c-o><s-o>
inoremap [<cr> [<cr>]<c-o><s-o>
" # Close brackets without return
inoremap ( ()<left>
inoremap { {}<left>
inoremap [ []<left>
" # Two cases below are covered by inoremap <exp>
" inoremap " ""<left>
" inoremap ' ''<left>
" # If you close a bracket that is already closed, it overwrites
inoremap <expr> ) strpart(getline('.'), col('.')-1, 1) == ")" ? "\<Right>" : ")"
inoremap <expr> } strpart(getline('.'), col('.')-1, 1) == "}" ? "\<Right>" : "}"
inoremap <expr> ] strpart(getline('.'), col('.')-1, 1) == "]" ? "\<Right>" : "]"
inoremap <expr> ' strpart(getline('.'), col('.')-1, 1) == "'" ? "\<Right>" : "''<left>"
inoremap <expr> " strpart(getline('.'), col('.')-1, 1) == "\"" ? "\<Right>" : "\"\"<left>"
" # enclose a word in normal mode with "'({[
nnoremap ' mmbi'<esc>ea'<esc>`m<right>
nnoremap " mmbi"<esc>ea"<esc>`m<right>
nnoremap ( mmbi(<esc>ea)<esc>`m<right>
nnoremap { mmbi{<esc>ea}<esc>`m<right>
nnoremap [ mmbi[<esc>ea]<esc>`m<right>
" # enclose a selection in visual mode with "'({[
vnoremap ' <Esc>`<i'<Esc>`>a<right>'<Esc>
vnoremap " <Esc>`<i"<Esc>`>a<right>"<Esc>
vnoremap ( <Esc>`<i(<Esc>`>a<right>)<Esc>
vnoremap { <Esc>`<i{<Esc>`>a<right>}<Esc>
vnoremap [ <Esc>`<i[<Esc>`>a<right>]<Esc>
Adriano_Pinaffo
  • 1,429
  • 4
  • 23
  • 46