Search code examples
vimvi

How to select non-rectangular block of text in vim


I recently started using vim for writing markdown and latex files. Visual mode works great when copying lines or blocks of code, but when I'm writing, I often need to copy non-rectangular chunks of text. For example, what I'd like to be able to do is this:

desired_selection

But, with visual mode I can only seem to do this:

enter image description here

or this:

lenter image description here

Is there a way to select the entire first line and only a portion of the second line?


Solution

  • All of Vim's modes are listed under :help vim-modes. Here is what it says about visual mode:

    Visual mode     This is like Normal mode, but the movement commands
                    extend a highlighted area.  When a non-movement
                    command is used, it is executed for the highlighted
                    area.  See |Visual-mode|.
    

    Where you get a handy link to :help visual-mode, which is a whole section delving into every detail of the three visual modes, started with v, V, and <C-v>.

    Note that it says this near the top:

    This is introduced in section |04.4| of the user manual.
    

    This means that visual mode is taught very early in the user manual and, by consequence, that it is a very basic and foundational topic. It also implies that going through the linked section of the user manual is a prerequisite.

    This is the second paragraph of that very basic section:

    You start Visual mode by pressing "v".  You move the cursor over the text you
    want to work on.  While you do this, the text is highlighted.  Finally type
    the operator command.
    

    Note that it says v, not V.

    Visual mode, by way of v, again, is also casually introduced in lesson 5.3 of $ vimtutor.

    All that to say that, everywhere visual mode is mentioned in the built-in documentation, from the very basic vimtutor to the very extensive reference manual, v is systematically mentioned first.