我試圖找到一種方法來使用global
和:substitute
,並正確處理連續的行上的比賽,並在第一行一致,但很可惜,我沒有啓發。
所以,我回到我的基本知識:我實現了我認爲缺少的東西::confirm global
。
The result has been pushed in my library plugin。
工作原理:
- 我準備的是記住以前的用戶選擇,當它的事項(總是,或退出,或最後)有狀態變量。
- 我在模式上執行
global
,每次比賽我都會檢查用戶想要做什麼。
- 我既可以使用鴕鳥政策問,再次狀態
- 或者我要求使用
StatusLineNC
高亮組與echo "\rmessage"
+ :redraw
。這是我們以前甚至在Vim 6 IIRC之前完成的一個非常古老的技巧。
相關的代碼如下:
" Function: lh#ui#ask(message) {{{3
function! lh#ui#ask(message) abort
redraw! " clear the msg line
echohl StatusLineNC
echo "\r".a:message
echohl None
let key = nr2char(getchar())
return key
endfunction
" Function: lh#ui#confirm_command(command) {{{3
" states:
" - ask
" - ignore
" - always
function! s:check() dict abort
if self.state == 'ignore'
return
elseif self.state == 'always'
let shall_execute_command = 1
elseif self.state == 'ask'
try
let cleanup = lh#on#exit()
\.restore('&cursorline')
\.restore_highlight('CursorLine')
set cursorline
hi CursorLine cterm=NONE ctermbg=black ctermfg=white guibg=black guifg=white
let choice = lh#ui#ask(self.message)
if choice == 'q'
let self.state = 'ignore'
let shall_execute_command = 0
" TODO: find how not to blink
redraw! " clear the msg line
elseif choice == 'a'
let self.state = 'always'
let shall_execute_command = 1
" TODO: find how not to blink
redraw! " clear the msg line
elseif choice == 'y'
" leave state as 'ask'
let shall_execute_command = 1
elseif choice == 'n'
" leave state as 'ask'
let shall_execute_command = 0
elseif choice == 'l'
let shall_execute_command = 1
let self.state = 'ignore'
endif
finally
call cleanup.finalize()
endtry
endif
if shall_execute_command
execute self.command
endif
endfunction
function! s:getSID() abort
return eval(matchstr(expand('<sfile>'), '<SNR>\zs\d\+\ze_getSID$'))
endfunction
let s:k_script_name = s:getSID()
function! lh#ui#make_confirm_command(command, message) abort
let res = lh#object#make_top_type(
\ { 'state': 'ask'
\ , 'command': a:command
\ , 'message': a:message . ' (y/n/a/q/l/^E/^Y)'
\ })
call lh#object#inject_methods(res, s:k_script_name, 'check')
return res
endfunction
" Function: lh#ui#global_confirm_command(pattern, command, message [, sep='/']) {{{3
" Exemple: to remove lines that match a pattern:
" > call lh#ui#global_confirm_command(pattern, 'd', 'delete line?')
function! lh#ui#global_confirm_command(pattern, command, message, ...) abort
let cmd = lh#ui#make_confirm_command(a:command, a:message)
let sep = get(a:, 1, '/')
exe 'g'.sep.a:pattern.sep.'call cmd.check()'
endfunction
" Function: lh#ui#_confirm_global(param) {{{3
function! lh#ui#_confirm_global(param) abort
let sep = a:param[0]
let parts = split(a:param, sep)
if len(parts) < 2
throw "Not enough arguments to `ConfirmGlobal`!"
endif
let cmd = join(parts[1:])
call lh#ui#global_confirm_command(parts[0], cmd, cmd . ' on line?', sep)
endfunction
command! -nargs=1 ConfirmGlobal call lh#ui#_confirm_global('<args>')
從這裏,你可以輸入文字:
:call lh#ui#global_confirm_command(pattern, 'd', 'delete line?')
- 或
:ConfirmGlobal/pattern/d
產生一個啓發較少提示
'%s/text。* \ n // gc'將會做 –
感謝LievenKeersmaekers,您將**文本**替換爲行結束符** \ n **使用正則表達式不加任何操作** // **。這很好,因爲它突出顯示了**文本**在哪裏排列以作出刪除決定,但並不是從開始到結束刪除該行。 – AmritpalNagraMe