2017-05-14 140 views
1

假設我需要通過Vim的README文件中的以下提取將commands?模式替換爲其大寫版本。下面的命令作品,在Vim中查看替換列表

:%s/\vcommands?/\=toupper(submatch(0))/g 

問題:如何查看更改的列表,以確保替代是正確的?

這是我嘗試過的一些嘗試。

  1. 查看difference between the current buffer and the original file"。工作流程是在進行替換之前保存緩衝區,然後進行替換,並使用鏈接頁面上提到的命令:DiffSaved來查看區別。它適用於小文檔。對於大型企業來說,找到變化是很困難的。

  2. 使用l標誌substitute命令。 :help :s說國旗l「打印的文字喜歡|:list|」。我試圖通過在g之後追加標記l標誌。它說「三條線上有5個替代品」,並顯示最後一行替代品。它看起來很有前途,但我不知道如何查看每一行更改(s)。

  3. 捕捉ex命令輸出。由於:s是一個ex命令,我應該能夠捕獲它的輸出。這wiki page顯示的步驟。它使用redir cmd。我所捕獲的輸出是相同的,我看到使用嘗試2.而不是示出具有變化率(s)每線的輸出,它只是說

    5 substitutions on 3 lines 
    6 line editing, COMMAND typeahead display, COMMAND to display$ 
    

文本用於取代例如:

Vi IMproved. A clone of the UNIX text editor Vi. Very useful 
for editing programs and other plain ASCII text. Full Vi 
compatibility and includes all Ex commands. Extra features 
above Vi: Multilevel undo, multiple windows, syntax 
highlighting, command line history, folding, improved command 
line editing, command typeahead display, command to display 
yank buffers, possibility to edit binary files, file name 
stack, support for Manx QuickFix and other compiler's error 
messages, shows current file name in window title, on-line 
help, rectangular cut/paste, etc., etc., etc... 
+0

爲什麼不先用':g/\ vcommands?/#p'預覽更改,並在驗證了將要更改的內容後進行替換? –

+0

@LievenKeersmaekers如果我理解正確,':g/\ vcommands?/#p'會打印出包含'\ vcommands?'模式的行。我想查看*替代*而不是找到的模式。 – semibruin

+0

有點複雜,有很大的改進空間,但這可能讓你開始'let @t =''| g/\ vcommands?/ sil y T | let @t = substitute(@t,'\ vcommand?','' \ = toupper(submatch(0))','g')'。執行之後,'t'寄存器包含已經被替換的行。 –

回答

0

不完全是你問什麼,但關於使用c標識,確認每個人的變化是什麼?這是, :%s/\vcommands?/\=toupper(submatch(0))/gc

+1

我不喜歡標誌'c',因爲確認'y'後,vim跳轉到下一個模式,這可能離最近的變化很遠。因此,很難看到這些變化。一次修改或修改錯誤也可能更有效。 – semibruin

0

這說明改變的線壓前將下一變化「Y」和按鍵等待, ,:%s的,舊的,新的,gicp# 無擔保,由GNU-Emacs的查詢靈感-更換。

> git clone https://github.com/vim/vim.git 
> cd src 
> mingw32-make -f Make_ming.mak gvim.exe 
> gvim.exe -c "%s,Sun,SUNDAY,gicp#" testfile.txt 
> git diff ex_cmds.c 

--- a/src/ex_cmds.c 
+++ b/src/ex_cmds.c 
@@ -5095,6 +5095,7 @@ do_sub(exarg_T *eap) 
#endif 
       ); ++lnum) 
    { 
+  int show_change=FALSE; 
     nmatch = vim_regexec_multi(&regmatch, curwin, curbuf, lnum, 
                 (colnr_T)0, NULL, NULL); 
     if (nmatch) 
@@ -5432,8 +5433,10 @@ do_sub(exarg_T *eap) 
         } 
         if (typed == 'n') 
          break; 
-      if (typed == 'y') 
+      if (typed == 'y') { 
+       show_change=TRUE; 
          break; 
+      } 
         if (typed == 'l') 
         { 
          /* last: replace and then stop */ 
@@ -5779,6 +5782,11 @@ skip: 
       line_breakcheck(); 
      } 

+   if (show_change) { 
+    print_line(curwin->w_cursor.lnum, subflags.do_number, subflags.do_list); 
+    do_sleep(10); 
+    (void) plain_vgetc(); // wait for keypress 
+   } 
+