好的編輯器送你上天堂,不好的編輯器送你住套房。
但是,有好的編輯器還是要有好的設定,才能送你上天堂。
同時註解多行
用virtual mode的方式選取多行:'<,'>s/^/\/\//g
把暫存器a的內的東西貼出來"ap
一般來說 :g 的運作方式是根據一行一行來做的,當你用下面指令的時候,:g/pattern/y a
這代表的意思其實只是把每次比對到的那行都 yank 複製到"a這裡面去。所以這就會造成我們最後只會拿到最後一行的結果,而不是每個比對到的都會進入到"a去。所以就必須告訴 vim 我們指定的內容是要做附加而不是覆蓋::g/pattern/y A
刪除選取的行數,並且存到暫存器a"aD
或:'<,'>d a
取得目前檔案的目錄:echo fnamemodify("%", ":p:h")
%表示current file name
取得目前檔案的名稱:echo expand("%")
若執行的指令很可能會出現錯誤訊息,但仍不想要顯示錯誤訊息的話,可以使用silent,例::silent! exe "normal /path\<CR>"
或:silent! /xxxfdafd
在目前位置加入時間::map <F2> a<C-R>=strftime("%c")<CR><ESC>
流程
1. 把目前的時間存到暫存器a
//[-start-strftime("%y%m%d")-CCD9527-remove]//
SET @a=time()
2. 註解掉目前選取的所有行數
:'<,'>s/^/\/\//g
3. 把選取的部分附加到暫存器a
"AD
4. 把目前的時間存到暫存器a
//[-end-strftime("%y%m%d")-CCD9527-remove]//
SET @A=time()
5. 把暫存器a貼到目前的位置
"ap
line
append
cursor
getline
mode
confirm
repeat
一行一行讀出來(getline)
加工(substitute)
加到list(add)
刪除原本的資料
_vimrc_utils
" Decide comment symbol
function CommentSymbol ()
"
" Set the default comment
"
let markid_start='//'
let markid_end='//'
"
" Set the comment by the different file type
"
if exists("&filetype") != 0
if (&filetype == "asm") || (&filetype == "pov")
let markid_start=';'
let markid_end=';'
elseif (&filetype == "make") || (&filetype == "inform") || (&filetype == "conf")
let markid_start='#'
let markid_end='#'
elseif &filetype == "dosbatch"
let markid_start='rem '
let markid_end='//'
endif
endif
return [escape(markid_start, ' /'), markid_start, markid_end]
endfunction
"Mark from N to M
function MarkN2MbyComment (N, M, Comment)
let ln = a:N
while ln <= a:M
call MarkByLine (ln, a:Comment)
let ln = ln + 1
endwhile
let @/=''
endfunction
" Remove Mark from N to M
function RemoveMarkN2MbyComment (N, M, Comment)
let ln = a:N
while ln <= a:M
call RemoveMarkByLine (ln, a:Comment)
let ln = ln + 1
endwhile
let @/=''
endfunction
" Set UserID from user input
function SetUserID ()
if exists("g:UserID") == 0
let g:UserID = inputdialog("Set Comment Symbol : (EX:IB06920001)")
if g:UserID == ""
let g:UserID = "TempUser"
endif
endif
endfunction
" Clear UserID
function ClearUserID ()
if exists("g:UserID") != 0
unlet g:UserID
endif
endfunction
" Add ceiling and floor comment
function CeilFloorComment (lnL, lnF, markid_start, markid_end, type)
exe append(a:lnL, a:markid_start . "[-end-" . strftime("%y%m%d"). "-" . g:UserID . "-" . a:type . "]" . a:markid_end)
exe append(a:lnF - 1, a:markid_start . "[-start-" . strftime("%y%m%d"). "-" . g:UserID . "-" . a:type . "]" . a:markid_end)
exe "normal " . (a:lnL + 2) . "G"
endfunction
" AddCommentBlock (F9)
function AddCommentBlock (lnF, lnL)
if line(".") == a:lnL
call SetUserID ()
let [comment, markid_start, markid_end] = CommentSymbol ()
call CeilFloorComment (a:lnL, a:lnF, markid_start, markid_end, "add")
endif
endfunction
" ModifyCommentBlock (F10)
function ModifyCommentBlock (lnF, lnL)
if line(".") == a:lnL
call SetUserID ()
let [comment, markid_start, markid_end] = CommentSymbol ()
call CeilFloorComment (a:lnL, a:lnF, markid_start, markid_end, "modify")
endif
endfunction
" RemoveCommentBlock (F11)
function RemoveCommentBlock (lnF, lnL)
if line(".") == a:lnL
call SetUserID ()
let [comment, markid_start, markid_end] = CommentSymbol ()
call MarkN2MbyComment (a:lnF, a:lnL, comment)
call CeilFloorComment (a:lnL, a:lnF, markid_start, markid_end, "remove")
endif
endfunction
function VisualAddCommentBlock ()
let lnF = line("'<")
let lnL = line("'>")
call AddCommentBlock (lnF, lnL)
endfunction
function NormalAddCommentBlock ()
let lnF = line(".")
let lnL = line(".")
call AddCommentBlock (lnF, lnL)
endfunction
function VisualModifyCommentBlock ()
let lnF = line("'<")
let lnL = line("'>")
call ModifyCommentBlock (lnF, lnL)
endfunction
function NormalModifyCommentBlock ()
let lnF = line(".")
let lnL = line(".")
call ModifyCommentBlock (lnF, lnL)
endfunction
function VisualRemoveCommentBlock ()
let lnF = line("'<")
let lnL = line("'>")
call RemoveCommentBlock (lnF, lnL)
endfunction
function NormalRemoveCommentBlock ()
let lnF = line(".")
let lnL = line(".")
call RemoveCommentBlock (lnF, lnL)
endfunction
function VisualAddSelectionComment ()
let lnF = line("'<")
let lnL = line("'>")
call AddSelectionComment (lnF, lnL)
endfunction
function NormalAddSelectionComment ()
let lnF = line(".")
let lnL = line(".")
call AddSelectionComment (lnF, lnL)
endfunction
function AddSelectionComment (lnF, lnL)
if line(".") == a:lnL
let [comment, markid_start, markid_end] = CommentSymbol ()
call MarkN2MbyComment (a:lnF, a:lnL, comment)
endif
endfunction
function RemoveSelectionComment (lnF, lnL)
if line(".") == a:lnL
let [comment, markid_start, markid_end] = CommentSymbol ()
call RemoveMarkN2MbyComment (a:lnF, a:lnL, comment)
endif
endfunction
function NormalRemoveSelectionComment ()
let lnF = line(".")
let lnL = line(".")
call RemoveSelectionComment (lnF, lnL)
endfunction
function VisualRemoveSelectionComment ()
let lnF = line ("'<")
let lnL = line ("'>")
call RemoveSelectionComment (lnF, lnL)
endfunction
function MarkByLine (CurrentLine, comment)
let TempContent = getline(a:CurrentLine)
let TempContent = substitute (TempContent, '^', a:comment,'g')
exe a:CurrentLine . "d"
exe append (a:CurrentLine - 1, TempContent)
exe "normal " . a:CurrentLine . "G"
endfunction
function RemoveMarkByLine (CurrentLine, comment)
let TempContent = getline(a:CurrentLine)
let TempContent = substitute (TempContent, '^\(\s*\)' . a:comment, '\1', 'g')
exe a:CurrentLine . "d"
exe append (a:CurrentLine - 1, TempContent)
exe "normal " . a:CurrentLine . "G"
endfunction
"AddSelectionComment
nmap <F3> :call NormalAddSelectionComment ()<CR>
vmap <F3> :call VisualAddSelectionComment ()<CR>
"RemoveSelectionComment
nmap <F4> :call NormalRemoveSelectionComment ()<CR>
vmap <F4> :call VisualRemoveSelectionComment ()<CR>
" 移除每一行最後多餘的空白
nmap <F6> :silent! %s/ \+$//gc<CR>
" 把tab轉換成空白
nmap <F7> :silent! %s/\t/ /gc<CR>
nmap <F8> :call ClearUserID ()<CR>
vmap <F8> :call ClearUserID ()<CR>
vmap <F9> :call VisualAddCommentBlock ()<CR>
nmap <F9> :call NormalAddCommentBlock ()<CR>
vmap <F10> :call VisualModifyCommentBlock ()<CR>
nmap <F10> :call NormalModifyCommentBlock ()<CR>
vmap <F11> :call VisualRemoveCommentBlock ()<CR>
nmap <F11> :call NormalRemoveCommentBlock ()<CR>
參考資料:
vim 的選擇模式 (visual mode)
有多少種方式可以在vim中插入行號
我的 VIM 整理
vim 提供的函數
vim 選擇模式下的熱鍵( Key mapping )
沒有留言:
張貼留言