查看 Vim 中的替换列表



>假设我需要通过以下来自 Vim 自述文件的摘录将模式commands?替换为其大写版本。以下命令有效,

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

问题:如何以列表形式查看更改以确保替换正确?

以下是我尝试过的几次尝试。

  1. 查看当前缓冲区和原始文件之间的差异"。工作流程是在进行替换之前保存缓冲区,然后进行替换,并使用链接页面上提到的命令:DiffSaved查看差异。它适用于小文档。对于大型更改,很难找到更改。

  2. 使用lsubstitute命令的标志。:help :s说国旗l"像|:list|一样打印文本"。我通过在标志后附加标志l来尝试g这一点。它说"3 行 5 个替换",并显示最后一行替换。它看起来很有希望,但我不知道如何查看每行都有变化。

  3. 捕获 ex 命令输出。由于:s是一个 ex 命令,我应该能够捕获它的输出。此维基页面显示了这些步骤。它使用redir厘米。我捕获的输出与我使用尝试 2 看到的输出相同。它不是显示每行都有变化,而是简单地说

    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...

不完全是你要求的,但是使用c标志来确认每个单独的更改呢?这是:%s/vcommands?/=toupper(submatch(0))/gc

这显示了按"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
+           }
+  

最新更新