In the very general case, the :g command cannot be called recursively. There is one exception:
Since Vim patch 8.0.0630, one can call a recursive :g command, if it operates only within a single line. So you can do something like (quoting the help):
When the command is used recursively, it only works on one line.
Giving a range is then not allowed. This ...
Using the -range flag when defining a user-command usually allows the user to provide a range to the command:
command -range Mine echo <range> <line1> ',' <line2>
" the next one is equivalent to .Mine by default
However, sometimes you want to be able to do
Or else use the number provided as a <count>:
A range with commands is always about lines. It seems you want to work with line/column positions.
Whether a range was given can be checked by using <range>:
command! -range Test call TestFunc(<range>,<line1>,<line2>)
function! TestFunc(r,l1,l2) abort
if a:r == 0
echo "No range"
elseif a:r == 1
echo "Single ...
Include the > flag in 'cpoptions', so that Vim automatically adds a new line before appending to a register:
See :h cpo->.
I would like a more general solution so that I can understand how to solve future problems.
let @q = '' | *g/^/exe 'norm! "Qyi"' | call setreg('q', "\n", 'ac')
Your command didn't work because ...
Rich's answer using :'<,'>w !copy_stuff is the right answer!
One alternative is to use the system() function.
You can pass it a string as input, which you can get from a register that captures the text in your range.
It's convenient to define a user command for this purpose, since then you can easily pass it a range.
For example, with your ...