3

Say I've executed the command:

:g/^.\{1,50\}$/m$

to move all lines with 50 or less characters to the bottom of the file.

Then I realise I actually didn't want to do that for the whole file, but for example a paragraph (or for any visually selected lines). I then do u and vip to select the paragraph I want the g command to affect, I press :, and try to press <UP> to get the previous command, problem is that there are only commands I've done when I've been in visual mode.

How should I do this, so that I don't have to type the whole g command again for the visual selection?

3 Answers 3

3

My preferred solution is

  1. Select your paragraph vip
  2. Hit : and then <C-f> OR press q:
  3. Move up to your :g command and do I'<,'>, then Esc, then <CR>.

As mentioned in the comments, it might be handy to map this:

xnoremap <Leader>. q:<UP>I'<,'><Esc>$
1
  • 2
    This works great! I made a mapping which makes this very smooth: xnoremap <Leader>, q:<UP>I'<,'><Esc>$
    – ruohola
    Commented Jul 25, 2019 at 13:07
1

first of all, you can't do it in general for any visual selection, since the ex commands operate on lines, so only for the whole lines which are fully or partially visually selected you can run such a command. E.g. you have a line 'Hello world' with the 'or' selected, but you can't operate on the selection, which is 'or', only on the whole line 'Hello world'

Use the automatically placed marks '< for the first line of the visual selection, and '> for the last one. The marks can be set with no visible selection (for example, if you escaped the select mode), and the command below will still work, but you can always press gv in the normal mode, to restore the last selection.

So the command would be

:'<,'> g/^.\{1,50\}$/m$

With the requirement to use the last command without retyping, one can use the normal ex command and the @: command from the normal mode:

:'<,'> normal @:

But I personally don't find this convenient, it seems to be quicker to invoke the command history window, find you command (if it was so long to type) edit it quickly with the '<,'> marks and run it

4
  • I'm afraid you didn't understand my question. Yes I know I can operate on the visual selection by just selecting some lines and typing :, which will automatically insert :'<,'> to the command line, BUT I would have to type the whole g/^.\{1,50\}$/m$ command again after that, and that is what I'm trying to avoid here.
    – ruohola
    Commented Jul 25, 2019 at 12:44
  • @ruohola, there is a couple of suggestions in such a case
    – d.k
    Commented Jul 25, 2019 at 12:52
  • 2
    You can also expand the command register in-line. Type :, you get the selection markers added automatically, then type Ctrl-r followed by : to insert a copy of the last command line. (See :help quote_: for info on this and other registers and :help c_CTRL-R for info on what that keystroke does in command mode.) Commented Jul 26, 2019 at 23:23
  • I have that upper arrow in the command window brings the last command, if you want to ask about this, ask another question or look for the answer(better)
    – eyal karni
    Commented Aug 2, 2019 at 11:52
1

You can edit the last command to include \%V in the beginning of the pattern g/\%Vpattern/cmd and it will search the last visual selection.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.