【问题标题】:Confirmation flag action in for loopfor循环中的确认标志动作
【发布时间】:2016-02-11 10:29:07
【问题描述】:

例子:

  "create list of only certain linenumbers 
  ....

  "take action on these linenumbers
  for i in range(0,len(mylist)-1)

     exe mylist[i].'s/(mysearch)/\=substitute(submatch(0), ".", "", "ge")/gc'

  endfor

如果我使用yn,确认标志可以正常工作,但当我使用aq 时它不起作用,因为它在for 循环中。
如何在确认对话框中捕获 aq 标志并在整个 for 循环中执行它:
a --> do a substitute for all other lines in for loop
q --> end for loop

(我曾经问过类似的问题,但没有找到解决方案)

【问题讨论】:

    标签: for-loop vim substitution flags confirmation


    【解决方案1】:

    我打算使用:global 来填充您的行号,但结果相同。相反,您可以这样做

    exe '%s#\v('.join(map(copy(lines), '"%".v:val."l"'), '|').')\zsPATTERN#REPL#gc'
    

    解决方案的关键是:h /\%l 将匹配限制在特定行。

    【讨论】:

    • 嗨 Luc,我不太明白你的回答。当我推 p.e. 时,我的行号实际上在列表“mylist”中。我的键盘上 3 次 'y' 进行 3 次替换,并且想要更改所有其他人 ('a') 我该如何调整您的解决方案?
    • 您只需将PATTERN 更改为您要替换的内容,并将REPL 更改为您要插入的内容。我仍在使用:substitute。只是我使用 map 和 join 将列表协调成一个非常具体的领先模式。尝试使用 echo 而不是 exe 来查看命令的作用。
    • 我无法调整此解决方案,因为我在 for 循环中有替换命令,其中包含更多代码。我想找到类似if user has pushed 'a' --> change subsitute code from 'gc' to 'g'if user has pushed 'q' --> break for loop
    • 你不能让你的 for 循环做它必须做的事情然后建立所有行的列表,最后执行替换吗?如果没有,唯一的其他解决方案是让您实施替代“模式”。可以办到。您必须使用echohl StatusLineNC:redraw。例如,在lh-brackets 中查看我的BracketsManipMode 函数
    • 您将无法使用:substitute。感谢search()getline()subtitute()setline() 和一个您必须编程的对话框,您必须模拟它。
    猜你喜欢
    • 2020-09-23
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 2020-11-29
    • 2011-10-18
    • 1970-01-01
    • 1970-01-01
    • 2011-05-13
    相关资源
    最近更新 更多