Hot answers tagged

7

You can use :write for this: :'<,'>w !copy_stuff For more details, see :help :write_c: Execute {cmd} with [range] lines as standard input (note the space in front of the '!').


5

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 ...


3

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> %Mine 1,/bar/Mine " the next one is equivalent to .Mine by default Mine However, sometimes you want to be able to do 0Mine Or else use the number provided as a <count>: ...


3

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 ...


2

Include the > flag in 'cpoptions', so that Vim automatically adds a new line before appending to a register: set cpo+=> See :h cpo->. I would like a more general solution so that I can understand how to solve future problems. Try this: let @q = '' | *g/^/exe 'norm! "Qyi"' | call setreg('q', "\n", 'ac') Your command didn't work because ...


2

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 ...


1

You could instead try this: :g/begin/,/end/ s/^$//. I don't think g works recursively, so using s instead seems to do the job.


Only top voted, non community-wiki answers of a minimum length are eligible