I'm trying to automate some simple modifications to a set of files with a script using Vim ex mode. What I want to do is search for a pattern, delete it from its current location (just the pattern, not the whole line), and paste it at the end of the document.
There are some useful suggestions at the following URL, but I feel like there ought to be a way of doing this without defining a special function. Copy search matches
Recommendations?
Ex commands typically work on entire lines. However we can use the command :s to "capture" all the matches into a register and then paste them at the end of the document.
:let @a=""
:%s//\=matchstr(setreg('A',submatch(0),'l'),'')/g
:$put a
Explanation:
a register via let @a=""%s//....\=, as the substitutionsubmatch(0) represents the matched patterna register linewise the matching pattern via: setreg('A',submatch(0),'l')setreg() returns a zero we use matchstr() to basically convert it to an empty string/g flag to match multiple times per linea register to the end of the document via $pu aFor more help see:
:h :let
:h :s
:h range
:h :s\=
:h submatch(
:h setreg(
:h matchstr(
:h :pu
:h registers