也就是按下 { 時自動生出 } 並把游標停在中間
同理,()、[]
更清楚的圖示:( | 表示游標位置)
( --> (|)
[ --> [|]
{ --> {|}
但是若按了 () 總不能跑出 ()) 吧 (我之前就是因為會這樣才放棄括號自動配對,不過解決了
照理說要這樣:
() --> ()|
[] --> []|
{} --> {}|
其他特殊功能:
{<enter> -->
{
|
}
' --> '|'
" --> "|"
'' --> ''|
"" --> ""|
同樣在 .vimrc 裡解決
以下是我修改網路上的,符合我的使用習慣即可
來囉OuO (角括號註解是因為C++會用到 << 運算子
inoremap ( ()<Esc>i
inoremap [ []<Esc>i
"inoremap < <><Esc>i
inoremap {} {}<Esc>i
inoremap {<CR> {<CR>}<Esc>ko
inoremap ) <C-R>=ClosePair(')')<CR>
inoremap ] <C-R>=ClosePair(']')<CR>
"inoremap > <C-R>=ClosePair('>')<CR>
inoremap } <C-R>=ClosePair('}')<CR>
inoremap " <C-R>=QuoteDelim('"')<CR>
inoremap ' <C-R>=QuoteDelim("'")<CR>
function ClosePair(char)
if getline('.')[col('.') - 1] == a:char
return "\<Right>"
else
return a:char
endif
endf
function QuoteDelim(char)
let line = getline('.')
let col = col('.')
if line[col - 2] == "\\"
"Inserting a quoted quotation mark into the string
return a:char
elseif line[col - 1] == a:char
"Escaping out of the string
return "\<Right>"
else
"Starting a string
return a:char.a:char."\<Esc>i"
endif
endf
希望接下來能夠達到刪除對應括號啦,繼續努力OuO
參考資料:
nemtsov/match_parens_brackets.vim
VIM中括号的自动补全与删除
Vim 自动补全成对的括号和引号
----補充----
因為括號補全,所以上次106.06.29 vim abbreviation設定自動產生 _main 就會多出一個 }
所以有使用的可以改成:
autocmd BufRead,BufNewFile *.h,*.c
\:iab <buffer> _main #include <stdio.h>
\<CR>
\<CR>int main(int argc, char *argv[]){
\<CR>
\<CR>return 0;
\}<BS><UP><C-R>=Eatchar('\m\s\<bar>\r')<CR>
autocmd BufRead,BufNewFile *.hpp,*.cpp
\:iab <buffer> _main #include <iostream>
\<CR>using namespace std;
\<CR>
\<CR>int main(){
\<CR>
\<CR>return 0;
\}<BS><UP><C-R>=Eatchar('\m\s\<bar>\r')<CR>

沒有留言:
張貼留言