Selecting a number of lines in visual mode in vim - vim

I have a vim plugin that works mainly with the visual mode. One of its commands sends the visually selected region to an interpreter.
However, I would like to select the first 3105 lines of a file.
1,3105mycommand does not work, this is not implemented in the plugin.
Is there a vim command xx that, after invoking 1,3105xx would visually select the first 3105 lines?

An alternate method:
If the plugin only looks at visual mode's marks, then this might suffice:
1k<
3015k>
If it actually needs the visual mode to be on, then you would need to also add
normal gv
:help :k, :help gv
Note that nvim will start in character-wise visual mode, so change the last line to normal gvV for nvim.

The solution is simple: in visual mode one can use the 3105G command in command mode, typed blindly and that extends the visually selected region.

Related

How do I keep text selected when changing from Visual to Insert mode?

I am running Vim extension in VS Code and want to be able to select text using Visual mode and keep that text selected when I'm in Insert mode. Currently I am able to select text in Visual mode but the selection becomes unselected when I change to Insert mode. Is this possible?
In Vim, you can only be in one mode; so it's either insert mode or visual mode. That said, Vim remembers the start, end, and type of the previous selection, and it can be easily reselected via gv (cp. :help reselect-Visual).
As Vim has special atoms to specify mark positions in a regular expression (:help /\%'m), and the '< and '> marks correspond to the current selection, we can use :match to make the last (characterwise) selection permanently visible:
:match Visual /\%'<\_.*\%'>\_./

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#^#//#

How to paste text into Vim Visual Mode?

i am working in vim and I would like to paste yanked text into Visual Mode. Is it possible?
The p command works in visual mode, too (cp. :help v_p). However, the default register contents will be overwritten. (My ReplaceWithRegister plugin has an alternative command that keeps it.)
Alternatively, you can s or c to replace the visual selection, and then use one of the insert mode commands like <C-R>{register} to insert the register contents. For pasting the default register, you need to use "_s to avoid overwriting its contents.

gvim auto copy selected text to system clipboard (to share it with apps )

I wonder, how i can enable auto copy of selected text into '+'
register in Ubuntu (to share clipboard between apps)?
On win XP, i have
set guioptions+=a
and its works perfectly, but not in Ubuntu 11.10.
Also, i tried
set clipboard=unnamedplus,unnamed,autoselect,exclude:cons\|linux.
but without success.
Please do not offer hand-click solutions like
vmap <C-Insert> "+y and mouse copy/paste.
test case (with "behave mswin" option):
open gvim
shift-v, move cursor and Esc (select lines in visual mode)
go to firefox and click ctrl-v or ctrl-Insert to paste text
Solution
In this thread, problem was solved.
You need to apply patch from Christian Brabandt.
Also, if you have problem with paste with shift-insert after recompilation in ubuntu, you can add this in your vimrc:
if has("gui_running")
map <silent> <S-Insert> "+p
cmap <S-Insert> <C-R>+
imap <silent> <S-Insert> <Esc>"+pa
endif
Does "+y work? It's not a suggestion: if this command doesn't work, you may have some underlying problems that prevent a simple solution. So it needs to be checked first, even if it sounds stupid.
set clipboard+=unnamedplus is enough if your version of Vim supports it. Mine is 7.3.35 and it doesn't work (Vim doesn't complain, though).
I don't know exactly which patch introduced unnamedplus but you can do :help 'clipboard' (with the single quotes) to have a list of available options. If unnamedplus is listed, the snippet above should fix your problem. If it's not there you won't be able to use it (obviously): time to re-assess your "do not offer hand-click solutions like vmap "+y and mouse copy/paste" requirement or compile a more recent version of Vim.
Try following:
set guioptions+=P
Explanation:
TLDR: a puts text into "* register. P puts text into "+ register
From :help guioptions
'a' Autoselect: If present, then whenever VISUAL mode is started,
or the Visual area extended, Vim tries to become the owner of the
windowing system's global selection. This means that the Visually
highlighted text is available for pasting into other applications as
well as into Vim itself. When the Visual mode ends, possibly due to
an operation on the text, or when an application wants to paste the
selection, the highlighted text is automatically yanked into the "*
selection register. Thus the selection is still available for
pasting into other applications after the VISUAL mode has ended.
If not present, then Vim won't become the owner of the windowing system's global selection unless explicitly told to by a
yank or delete operation for the "* register. The same applies to
the modeless selection.
'P' Like autoselect but using the "+ register instead of the "*
register.

Resources