Reputation: 5857
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!
Upvotes: 16
Views: 15523
Reputation: 1699
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>
Upvotes: 0
Reputation: 10371
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.
Upvotes: 13
Reputation: 32926
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.
Upvotes: 1
Reputation: 834
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.
Upvotes: 10
Reputation: 139
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
Upvotes: 9