vim多行注釋和取消注釋


原文:https://www.cnblogs.com/Ph-one/p/5641872.html  

多行注釋:

1. 進入命令行模式,按ctrl + v進入 visual block模式,然后按j, 或者k選中多行,把需要注釋的行標記起來

2. 按大寫字母I,再插入注釋符,例如//

3. 按esc鍵就會全部注釋了

 

取消多行注釋:

1. 進入命令行模式,按ctrl + v進入 visual block模式,按字母l橫向選中列的個數,例如 // 需要選中2列

2. 按字母j,或者k選中注釋符號

3. 按d鍵就可全部取消注釋

 

對單行注釋:CTRL_C
對多行注釋: 先”V”,進入塊選擇模式。選擇一段代碼。CTRL_C
同樣,還原的命令和上面的一樣。如對一行CTRL_C后,把它注釋了,再CTRL_C后就還原了。
注意代碼應該要整齊,縮進。選擇塊時,要保證塊中的第一行就是最靠左的。不然會出問題。

復制代碼
“功能說明:加入或刪除注釋//
“映射和綁定

nmap :Setcomment
imap :Setcomment
vmap :SetcommentV
command! -nargs=0 Setcomment call s:SET_COMMENT()
command! -nargs=0 SetcommentV call s:SET_COMMENTV()

“非視圖模式下所調用的函數
function! s:SET_COMMENT()
let lindex=line(”.”)
let str=getline(lindex)
“查看當前是否為注釋行
let CommentMsg=s:IsComment(str)
call s:SET_COMMENTV_LINE(lindex,CommentMsg[1],CommentMsg[0])
endfunction

“視圖模式下所調用的函數
function! s:SET_COMMENTV()
let lbeginindex=line(”‘<") "得到視圖中的第一行的行數
let lendindex=line("'>“) “得到視圖中的最后一行的行數
let str=getline(lbeginindex)
“查看當前是否為注釋行
let CommentMsg=s:IsComment(str)
“為各行設置
let i=lbeginindex
while i<=lendindex
call s:SET_COMMENTV_LINE(i,CommentMsg[1],CommentMsg[0])
let i=i+1
endwhile
endfunction

“設置注釋
“index:在第幾行
“pos:在第幾列
“comment_flag: 0:添加注釋符 1:刪除注釋符
function! s:SET_COMMENTV_LINE( index,pos, comment_flag )
let poscur = [0, 0,0, 0]
let poscur[1]=a:index
let poscur[2]=a:pos+1
call setpos(”.”,poscur) “設置光標的位置

if a:comment_flag==0
“插入//
exec “normal! i//”
else
“刪除//
exec “normal! xx”
endif
endfunction

“查看當前是否為注釋行並返回相關信息
“str:一行代碼
function! s:IsComment(str)
let ret= [0, 0] “第一項為是否為注釋行(0,1),第二項為要處理的列,
let i=0
let strlen=len(a:str)
while i “空格和tab允許為”//”的前綴
if !(a:str[i]==’ ‘ || a:str[i] == ‘ ‘ )
let ret[1]=i
if a:str[i]==’/’ && a:str[i+1]==’/’
let ret[0]=1
else
let ret[0]=0
endif
return ret
endif
let i=i+1
endwhile
return [0,0] “空串處理
endfunction


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM