2017-10-17 113 views
4

目前我行可以搜索文本有效的方式來刪除包含在vim某些文本進行提示

/text 

,然後使用dd刪除線,如果我不想刪除我可以去下一場比賽與n

但是,有沒有更快的方法來做到這一點!

下面的這個命令刪除所有包含文本的行,但問題是它一次刪除所有行,有時文本在某些行中是異常。

:g/text/d 

但我想簡單的東西一樣一樣

:%s/text/some_other_text/gc 

,因爲這給了替換或不選擇。刪除包含text的每行前

:g/text/s/.*\n//c 

這會要求確認:

+0

'%s/text。* \ n // gc'將會做 –

+2

感謝LievenKeersmaekers,您將**文本**替換爲行結束符** \ n **使用正則表達式不加任何操作** // **。這很好,因爲它突出顯示了**文本**在哪裏排列以作出刪除決定,但並不是從開始到結束刪除該行。 – AmritpalNagraMe

回答

4

你可以混合:help global:help substitute

enter image description here

+0

啊發現它:只用輸入文件中的aaaa來試試你的解決方案。它不會工作。 –

+0

謝謝@romainl 這將工作在短行的情況下,但如果行很長的情況下,我必須搜索**文本**在其中的位置。 對我來說,搜索('/ text')然後刪除行('dd')更加方便,因爲我可以通過高亮顯示文本所在的位置來找到它。 – AmritpalNagraMe

+0

@AmritpalNagraMe - 我很想被證明是錯誤的,但是當你有連續的行包含你的搜索字符串時,這不起作用。 –

2

你並不需要爲這個全局命令。在本身的替代命令就足夠了由

  • 添加一個通配符
  • 以及添加結束線。

例如

%s/.*text.*\n//gc 
+0

要回答以前的註釋,模式應該是'。* text。 * \ n' –

+0

@LucHermitte - 你在說什麼評論? –

+0

當我評論他/她的問題時,我會談到OP的評論:https://stackoverflow.com/questions/46781951/efficient-way-to-delete-line-containing-certain-text-in-vim-with-提示/ 46807561?noredirect = 1#comment80525803_46781951 –

3

我試圖找到一種方法來使用global:substitute,並正確處理連續的行上的比賽,並在第一行一致,但很可惜,我沒有啓發。

所以,我回到我的基本知識:我實現了我認爲缺少的東西::confirm global

The result has been pushed in my library plugin

工作原理:

  1. 我準備的是記住以前的用戶選擇,當它的事項(總是,或退出,或最後)有狀態變量。
  2. 我在模式上執行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產生一個啓發較少提示
+0

這真是太神奇了,所有這一切都需要出現一個簡單的,有效的用例*(或換句話說:驚人的它並不能直接使用)* –

+1

嗯,':%s /.*文本。 * \ n // gc'工作得很好,開箱即用。可能有一個有效的解決方案,涉及':g' +':s',但正如我所說,我沒有受到啓發找到它。 –

相關問題