13

说我有以下文件

<block>
    <foo val="bar"/>
    <foo val="bar"/>
</block>
<block>
    <foo val="bar"/>
    <foo val="bar"/>
</block>

我怎么能把它变成

<block>
    <foo val="bar1"/>
    <foo val="bar"/>
</block>
<block>
    <foo val="bar1"/>
    <foo val="bar"/>
</block>

我尝试做的一件事是录制一个宏,然后:%s/bar/bar1/gc按一次,然后尝试编辑该宏。由于某种原因,我无法编辑宏。:(yn

4

7 回答 7

41

只是为了表明这可以通过替换来完成:

:let a = ['', '1']
:%s/bar\zs/\=reverse(a)[0]/g

概述

在每次替换时数组就地反转后,bar用变量中数组的第一个元素替换 every 的末尾。a

细节的荣耀

  1. let a = ['', '1']定义一个变量a来保存我们的数组
  2. %s/.../.../对文件中的每一行进行替换
  3. %s/bar\zs/.../在 bar 上进行替换,但在使用 bar 之后开始替换\zs
  4. \=:s命令的替换部分内部使用以下表达式的值
  5. reverse(a)reverse 只是简单地反转数组,但在原地进行
  6. reverse(a)[0]reverse 返回现在反转的数组,因此获取第一个元素
  7. /g替换行中的所有出现(可选)

一般情况

:let a = ['a', 'b', 'c']
:%s/bar\zs/\=add(a, remove(a, 0))[-1]/g

一般情况下“旋转”数组,a, 并使用数组的最后一个位置作为替换替换的值。

如需更多帮助,请参阅

:h :s
:h range
:h /\zs
:h :s\=
:h reverse(
:h :s_flags
:h Lists
:h add(
:h remove
于 2012-12-07T14:45:38.240 回答
4

您可以使用

:%s/bar/bar1/gc

它会在每场比赛中询问你是否要替换它。

否则,您必须在其中指定全部内容,然后将第一个 bar 替换为 bar1。

于 2012-12-07T13:26:04.610 回答
4

我会用宏来做:

qv            start recording in register v
/"bar"/e<cr>  search for "bar" and position the cursor at the end of the match
i1<esc>       insert 1 before the cursor and go back to normal mode
n             jump to next match
q             stop recording

之后,做{count}@v.

于 2012-12-07T14:02:24.773 回答
1

试试这个:

:%s/bar\(.*\)\n\(.*\)bar/bar1\1\r\2bar
于 2012-12-07T13:31:27.993 回答
1

这是一个应该可以解决问题的自定义命令。它使用替换表达式来计算完成的替换,并使用传递的附加参数来决定是否应该进行替换。(这允许比每秒更复杂的安排。)您的示例将很简单:

:%SubstituteSelected/\<bar\>/&1/ yn

这是(不幸的是很长)实现:

":[range]SubstituteSelected/{pattern}/{string}/[flags] {answers}
"           Replace matches of {pattern} in the current line /
"           [range] with {string}, determining whether a particular
"           match should be replaced on the sequence of "y" or "n"
"           in {answers}. I.e. with "ynn", the first match is
"           replaced, the second and third are not, the fourth is
"           again replaced, ...
"           Handles & and \0, \1 .. \9 in {string}.
function! CountedReplace()
    let l:index = s:SubstituteSelected.count % len(s:SubstituteSelected.answers)
    let s:SubstituteSelected.count += 1

    if s:SubstituteSelected.answers[l:index] ==# 'y'
        if s:SubstituteSelected.replacement =~# '^\\='
            " Handle sub-replace-special.
            return eval(s:SubstituteSelected.replacement[2:])
        else
            " Handle & and \0, \1 .. \9 (but not \u, \U, \n, etc.)
            let l:replacement = s:SubstituteSelected.replacement
            for l:submatch in range(0, 9)
                let l:replacement = substitute(l:replacement,
                \   '\%(\%(^\|[^\\]\)\%(\\\\\)*\\\)\@<!' .
                \       (l:submatch == 0 ?
                \           '\%(&\|\\'.l:submatch.'\)' :
                \           '\\' . l:submatch
                \       ),
                \   submatch(l:submatch), 'g'
                \)
            endfor
            return l:replacement
        endif
    elseif s:SubstituteSelected.answers[l:index] ==# 'n'
        return submatch(0)
    else
        throw 'ASSERT: Invalid answer: ' . string(s:SubstituteSelected.answers[l:index])
    endif
endfunction
function! s:SubstituteSelected( range, arguments )
    let l:matches = matchlist(a:arguments, '^\(\i\@!\S\)\(.*\)\%(\%(^\|[^\\]\)\%(\\\\\)*\\\)\@<!\1\(.*\)\%(\%(^\|[^\\]\)\%(\\\\\)*\\\)\@<!\1\(\S*\)\s\+\([yn]\+\)$')
    if empty(l:matches)
        echoerr 'Invalid arguments'
        return
    endif
    let s:SubstituteSelected = {'count': 0}
    let [l:separator, l:pattern, s:SubstituteSelected.replacement, l:flags, s:SubstituteSelected.answers] = l:matches[1:5]

    execute printf('%ssubstitute %s%s%s\=CountedReplace()%s%s',
    \   a:range, l:separator, l:pattern, l:separator, l:separator, l:flags
    \)
endfunction
command! -bar -range -nargs=1 SubstituteSelected call <SID>SubstituteSelected('<line1>,<line2>', <q-args>)

编辑

我现在已将此(连同相关命令)作为PatternsOnText 插件发布。

于 2012-12-07T15:38:13.407 回答
1
:let dosubs=1
:%s/bar/\=[dosubs?'bar1':submatch(0),extend(g:,{'dosubs':!dosubs})][0]/g
于 2012-12-07T19:21:38.663 回答
0

我找到了一个更简单的解决方案:

:g/<block>/norm! j02f"i1

:g ............ global command
/<block>/ ..... every line with <block>
j0 ............ goes down one line and to the column 1
2f" ........... jumps to the second "
i1 ............ insert the number one

我的旧复杂解决方案

\v%(block>\_{-})\zsbar
%s,,&1,g



   \v ............ very magic (avoid lots of scapes)
   %  ............ ignore whats flows
   (  ............ starts (ignored) group
   \_ ............ multiline search
   .{-} .......... non-greedy 
   \zs ........... start pattern for substituition
   bar  .......... pattern we want to change 

   % ............. whole file
   s ............. substituition
   ,, ............ use last search (could be //)
   & ............. use searched pattern 
   1 ............. add 1 after it  
于 2017-08-20T19:01:15.207 回答