我是一名辅助翻译员,以下是您需要翻译的内容:
我开始使用vim进行大量C编程。虽然我在vimrc文件中添加了一些技巧来进行代码颜色格式化、行号等,但我想知道是否有一种方法可以实现以下功能:
-
int main() {
变成这个样子 -
int main() {
}
有人有什么想法或建议吗?只要它能让我再也不忘记括号,我愿意尝试任何方法!
我开始使用vim进行大量C编程。虽然我在vimrc文件中添加了一些技巧来进行代码颜色格式化、行号等,但我想知道是否有一种方法可以实现以下功能:
-
int main() {
变成这个样子 -
int main() {
}
有人有什么想法或建议吗?只要它能让我再也不忘记括号,我愿意尝试任何方法!
但你也可以选择低科技方案:
inoremap { {<cr>}<C-o>O
九年后,为了纪念我尝试用以下映射覆盖所有情况,请将它们放在您的 .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>