I would like to save the output of g/pattern1/,/pattern2/ to a file (for each match, a different file).
e.g.
def
.......
end
def
.......
end
you would end up with a file for each “def…end”.
Tried using tempname() like so:
g/pattern1/,/pattern2/exe 'w ' . tempname() but this fails with no range allowed for exe
also tried
g/pattern1/,/pattern2/w `tempname()`
to get tempname() evaluated but this failed with a “too many filenames” error.
What am I missing? Can this be done by using global and other commands, or would you need vimscript to do it?
Use
executewhenever you want to insert variable into command-line if it is a mapping. If it is not, try usingHere
fn<Tab>withwildmode=longest,list:fullwill expand tofname,fnamee<Tab>will expand tofnameescape(,te<Tab>will expand totempname(), so this is a short way to input<C-r>=fnameescape(tempname())<CR>. You can omitfnameescapeif you are sure thattempnamewill not return filename with special characters.And note that backticks will not execute vimscript function, they execute shell command, so
`tempname()`tries to calltempname()in a shell and substitute filename with the result of this call. According to the help, you should have written`=tempname()`.