Search code examples
vimvi

Replace text from line above in Vim


How can I replace the first ten characters of a line where those ten characters match a particular pattern with the first ten characters from the line above?

Edit: It wasn't clear if I was asking to replace the first ten characters where the match could appear anywhere within the line, so maybe make a note in your answer if it deals with this case (call this case B and the intended one case A?)


Solution

  • Perhaps:

    :%s/^\(.\{10}\)\(.*\n\)abcdefghij\(.*\)/\1\2\1\3/
    

    Where 'abcdefghij' is the 10 character string on the 2nd line.