How to select non-rectangular block of text in vim - 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:
But, with visual mode I can only seem to do this:
or this:
l
Is there a way to select the entire first line and only a portion of the second line?

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.

Related

Vim: behaviour change of search function in visual mode

It used to be part of my Vim vocabulary... change to visual mode, place cursor to a search word, type * and all text between first and next search word would be highlighted. The highlighting can continue as long as you repeat pressing * and there is the next matching search word. Perfect set of commands to copy text, or simply as visual aid.
Coming back to Vim now after some time, this is not working on my configuration. What happens now is that only the letter under the cursor and all other such letters get highlighted.
I wonder if I have some conflict in my mappings or else? I use Vim v7.3. Anyone can help?
Vim does not have a visual mode command for *; this only works in normal mode. However, many people have a customization that supplies the super star visual variant, from Search for visually selected text - or a plugin like my SearchHighlighting plugin; its plugin page has links to many alternative plugins.
I guess you had something like that, too, but forgot about it. I also doubt that is was triggered by Ctrl + *; combinations of Ctrl and non-alphabetical letters mostly aren't available as unique mappings. The usual mapping is just *, overloading the original normal mode command for visual mode.
Vim 7.3 is from 2010 and very outdated. To become reacquainted with Vim (which I commend!), please choose a modern variant. It should be possible to install the latest version 8.1; if you can't find a proper package for your distribution (for Windows, check the binaries from either vim-win32-installer or tuxproject; on Linux, it's also not very difficult to compile it yourself (e.g. from the GitHub sources).

What's a common use case of "selection mode" in vim?

It's the first time i noticed about the selection mode in vim when i accidentally triggered it from visual-line mode by <c-g>.
Vim already has visual mode for text selection what's the use case of selection mode, can anyone give me a hint on this?
(note: i've checked the manual page which describes it as
a resemblance of MS Windows text selection
but i still cannot quite understand why do we need any mouse actions in vim)
Select-mode is commonly used in snippet plugins such as vim-snipmate. Mappings can be created that are unique to select-mode so as not to interfere with regular visual mode behaviour.
Here is an excerpt from Practical Vim by Drew Neil:
If you are happy to embrace the modal nature of Vim, then you should
find little use for Select mode, which holds the hand of users who
want to make Vim behave more like other text editors. I can think of
only one place where I consistently use Select mode: when using a
plugin that emulates TextMate's snippet functionality, Select mode
highlights the active placeholder.
As the documentation suggests, select mode is a bit different from visual mode. Here's the commands you can do in it:
Commands in Select mode:
- Printable characters, <NL> and <CR> cause the selection to be deleted, and
Vim enters Insert mode. The typed character is inserted.
- Non-printable movement commands, with the Shift key pressed, extend the
selection. 'keymodel' must include "startsel".
- Non-printable movement commands, with the Shift key NOT pressed, stop Select
mode. 'keymodel' must include "stopsel".
- ESC stops Select mode.
- CTRL-O switches to Visual mode for the duration of one command. *v_CTRL-O*
- CTRL-G switches to Visual mode.
Otherwise, typed characters are handled as in Visual mode.
When using an operator in Select mode, and the selection is linewise, the
selected lines are operated upon, but like in characterwise selection. For
example, when a whole line is deleted, it can later be pasted halfway a line.
You can see the docs here.
What they mean by "resembles MS Windows selection" is probably that you can extend the selection with Shift+Arrows, and also that any printable characters entered will substitute the selected text and enter insert mode.
Also, see this question/answer at the vi.SE.

How to run command for every line in visual linewise mode in vim

I have the following CSS that I want to comment out with "//" at the beginning of each line (using Sass).
a:focus {
outline: thin dotted;
}
With my cursor on the first line I enter visual linewise mode and select 3 lines Vjj. To comment I type I//ESC. What I expect to happen is all lines have the text "//" prepended but instead only the first line has been modified.
Alternatively if I use visual blockwise mode to select (i.e. Ctrl-vjj) the lines and press I//ESC I receive the expected result of all lines prepended with "//".
My assumption has been that linewise and blockwise modes were merely different ways to select text. If I wanted to select all text of multiples lines the selection commands were interchangeable as long as I was able to select the text to modify. But the behavior above leads me to believe there's a difference I don't yet understand.
Is it possible to use visual linewise mode to accomplish this task or is it just the wrong tool for the job? Also documentation on the differences between the two modes would be greatly appreciated.
If you are in linewise visual mode you can use normal to accomplish what you want.
:'<,'>norm I//
normal runs the command I// on every line in normal mode.
Character-wise, line-wise and block-wise visual modes all allow you to select text across multiple lines.
Character-wise visual mode is mainly useful when you don't care about or don't want to deal with "lines".
Line-wise visual mode is to be used when dealing with whole lines is important.
Block-wise visual mode is a convenient way to repeat a change across multiple similar lines. I like to see it like "a window inside the window" that allows me to act on a subset of my current buffer.
The one you choose is dictated by what you plan to do with that selection but their behavior differs only when doing visual mode commands: because Ex commands are always line-wise, they don't care about the specifics of your visual mode beyond the first line and the last line of the selection.
I would prefer to use visual mode and invoke :'<,'>s#^#//#

Why does this Vim mapping behave differently depending on how Visual mode was entered?

This question is what prompted a previous question of mine here: Vim's '(insert) VISUAL' mode?
In that question I asked about "VISUAL" mode vs "(insert) VISUAL" mode, which is where you enter visual mode directly from insert mode. I have found a mapping that behaves differently depending on the "subtype" of visual mode, and I don't understand why:
vnoremap x <Esc>jjj
vnoremap <A-[> <C-G>ugv
The first mapping illustrates how Vim goes out of its way to make mappings to behave consistently: pressing x from "VISUAL" mode and pressing x from "(insert) VISUAL" mode will both move the cursor down 3 lines. The latter does not exit to insert mode and enter three j's. So <Esc> is not behaving differently depending on the mode, even though outside of a mapping it would behave differently from these two modes.
The second mapping illustrates how some maps behave inconsistently. If you highlight a word and press ALT+[ then the word is replaced with ugv if done from "VISUAL" mode but nothing happens if done from "(insert) VISUAL" mode. Presumably this is because Vim is executing the normal mode commands ugv for 'undo' and 'reselect-visual'.
This is a strange inconsistency. It is a minimal example I ran into when debugging my mappings, and would like to know why it works this way.
I can reproduce your examples and I think your analysis is right.
When I type <C-G>ugv from (insert) visual mode, I get the expected results: <C-G> switches to (insert) select mode, ugv replaces the selection with those characters. A mapping should behave in the same way.
I guess this is a bug; the entire (insert) submode is quite special, not frequently used, and therefore prone to bugs. You could delve into the source code yourself, or raise this issue on the vim_dev mailing list. Due to the obscurity of the issue, it may be hard to garner much interest from the devs, though.

Vim format source code from Visual mode

watching a screencast (can't link it since you need to have a peepcode pro subscription) I've seen a developer indenting his source code (a ruby file) graphically using the visual mode inside vim. He did the following steps: press "v" selecting lines and then pressing something else I didn't get (because there is no representation of what's being pressed on the keyboard), then he got the whole source perfectly indented, without the need to write something on the command line.
Is there a plugin you know to do it that way from visual mode?
You can press = in visual mode to automatically indent your code.
Or you can use > and < in visual mode to change the indentation level
of the selected code.
This plugin does exactly what you want: https://github.com/Chiel92/vim-autoformat.
That would be V(motion)=.
See :help = for details.

Resources