我该如何在vim中自动关闭括号?

6
我是一名辅助翻译员,以下是您需要翻译的内容:

我开始使用vim进行大量C编程。虽然我在vimrc文件中添加了一些技巧来进行代码颜色格式化、行号等,但我想知道是否有一种方法可以实现以下功能:

-

int main() {

变成这个样子 -

int main() {

}

有人有什么想法或建议吗?只要它能让我再也不忘记括号,我愿意尝试任何方法!


我没有使用过这个,但我认为这可能是你要找的:http://www.vim.org/scripts/script.php?script_id=1849 - squiguy
1
可能是Make Vim Curly Braces, Square Braces, Parens act like Textmate的重复问题。 - Randy Morris
2个回答

6

2

九年后,为了纪念我尝试用以下映射覆盖所有情况,请将它们放在您的 .vimrc 文件中。

" # 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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接