1

What does C-V ? space CR do?

Can someone explain that key by key?

I know C-V is visual block mode, but the ? ? How does that work?

Imagine this, with the cursor on the first line, first char:

    abc
    abc
    abc

If I do the above, the result is that the visual block selects the whitespace before the abc on all lines.

3
  • 1
    ? starts a backward search
    – Naumann
    Dec 6, 2017 at 15:35
  • @Naumann Yes, that is true, but in C-V it does something differently...
    – mike
    Dec 6, 2017 at 15:46
  • if you <c-v> in normal mode, then ? does a backward search. If you <c-v> in insert mode, then ? is simply inserted literally
    – Naumann
    Dec 6, 2017 at 15:57

1 Answer 1

7

You need to see this as an action (<c-v>) followed by a motion (? <CR>) which is the basis of the Vim "grammar". For example ve is an action (v visually selection) followed by a motion (e until the end of the word).

Here

  • <c-v> starts a block selection from the cursor position to the next motion
  • ? starts a backward search
  • is your search pattern
  • <cr> starts the search

Here the next match (i.e. the previous whitespace) is the "next motion" expected by <c-v>

So your command starts a block selection from the cursor to the previous whitespace.

You could get the equivalent behavior with <c-v>B.


Edit To answer your example:

:h visual-block tells us that

With CTRL-V (blockwise Visual mode) the highlighted text will be a rectangle between start position and the cursor.

Your start position is the first whitespace of the first line. After you use the backward search your cursor position is last whitespace of the last line. If you draw a rectangle between these two points, you should see why all of the whitespaces are selected.

Now in your example remove a whitespace on the last line:

    abc
    abc
   abc

And use the same selection. You will notice that only the first two columns are selected because your start position didn't change but the cursor position changed since the previous whitespace isn't in the same place anymore.

About my <c-v>B example it doesn't work in your case because the motion B doesn't look for another line.

You will notice that if you use set nowrapscan the backward search will not be allowed to go to the last line so you will not select the rectangle anymore. (:h 'wrapscan')

6
  • Well, I can use it, when the cursor is on the first line and the result is, that all lines with a space are selected. So it looks like more of a forward search? Also, normally ? doesn't work on more than one line, or not?
    – mike
    Dec 6, 2017 at 16:04
  • Look at :h ? it is like / but backwards. And like / the search isn't limited to the current line. Now when you say that all lines with a space are selected are you sure that it is an actual selection and not just the search highlighting? If you do :set nohlsearch before <c-v>? <CR> do you still have the same behavior?
    – statox
    Dec 6, 2017 at 16:12
  • It could also be related to :set ws if you do not have any spaces before the current cursor position
    – Naumann
    Dec 6, 2017 at 16:34
  • @statox I added an example, C-V B does not do the same! But C-V / does the same...
    – mike
    Dec 6, 2017 at 17:33
  • 1
    @mike I see three questions from you that have answers but are currently open (no answer has been accepted). If a question has been answered to your satisfaction please accept the best answer. If the given answers don't meet your requirement please provide feed back as to why. Here statox has updated this answer some time ago and brought it to your attention. Please try not to leave questions in a state of limbo. This site depends on both questioners and answers remaining engaged until conclusion, if at all possible. Thanks.
    – B Layer
    Dec 23, 2017 at 10:00

Your Answer

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

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