What's a common use case of "selection mode" in vim? - 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.

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 /\%'<\_.*\%'>\_./

Vim does not like brackets

I have a problem that have been bothering me for days now.
Whatever I do, I simply cannot place any single {,},[ or ].
Every time I try, it takes me up to the next or previous paragraph. I've looked it up and I can't find a good way to unmap it and finally have my azerty keyboard to behave correctly so I can peacefully code
In the normal mode, press 'i' to enter the insert mode.
And you can start to edit your file.
After everything is done, press the 'ESC' key and then ':wq' to save your modification.
Vim / vi has several different modes. This dates back to the times when you would edit using a teletyper, i.e. no "visual" representation of the data you are editing, no mouse etc.
With only the "usual" keys at your disposal, you need to navigate in the data, you need to insert data, and you need to execute commands on the data. Not so coincidentially, these are the three modes of Vim / vi, and in each mode, the keys do something differently.
By default, the editor starts in the "normal mode", which you will use to navigate, and enter the other modes from.
In this mode, ) moves you to the end of the sentence, ( to the previous sentence. } and { do the same for paragraphs, combinations of [ and ] work with chapters.
If you enter "insert mode" (most simply by pressing i in "normal mode"), you can enter all the above letters normally. You exit "insert mode" by pressing <Esc>. You will recognize "insert mode" by -- INSERT -- being displayed in the bottommost line of the screen.
If you are in "insert mode", and pressing any of the above keys does move your cursor instead of entering the corresponding symbol, there is some (broken) configuration at work. Check your ~/.vimrc, and if necessary, rename it and try again with a "clean" configuration.
It is next to impossible to do Vim / vi justice in the scope of a SO answer. It is very much an expert-friendly editor, not a novice-friendly one. You need to actually learn how to use this editor, but it is absolutely worth it in the long run.
In insert mode, you should be able to insert brackets without trouble.
In normal mode, those correspond to motions.
To go to insert mode from normal mode, press i.
See: :h object-motions
Alright, Problem Solved ! I had to uninstall and reinstall the Ubuntu bash on windows 10 multiple times and it worked ! Thanks for the help everone !

Select word in VIM with ctrl+shift+arrow

Now, my config is: http://vim.wikia.com/wiki/Example_vimrc
with additional two lines in it:
source $VIMRUNTIME/mswin.vim
behave mswin
my sample text is:
foo.bar
how can i select foo, then .bar using Ctrl+Shift+Right
and bar, then foo. using Ctrl+Shift+Left ?
Ctrl+Lelf and Ctrl+Right stop on dot char, like traditional editor, but when i use Ctrl+Shift+Arrows - it stops only on whitespaces.
vim version: 7.4
os: ubuntu 14.04
The Select-mode (which will be enabled by :behave mswin) only "simulates" some behaviors of MS-Windows like editors. Just these movements cursor keys, <End>, <Home>, <PageUp> and <PageDown> with the shift key are expanding/collapsing current selection.
From :help Select-mode:
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.
From :help keymodel:
... Using a shifted special key starts selection ...
... Special keys in this context are the cursor keys, <End>, <Home>,
<PageUp> and <PageDown>. ...
Try to read about it: :help behave, :help Select-mode and :help keymodel.
But, I strongly recommend you to learn about the "real" vim movements and commands and disable :behave mswin. It is actually way more powerful.
In your case you could simply press veE to select first foo then foo.bar. Or if your intention is to select the whole foo.bar at once even simpler, press vW.

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

edit gvim mouse buttons

I have a two part question regarding mouse clicks on gvim.
1.) I would like to change my gvim double left mouse click into something similar to the windows environment (ie Notepad, Microsoft Word), wherein it: highlights the current word, and replaces it with whatever I type. This is akin to pressing 'cw' for changeword in vim.
2.) I would like my single mouse click to place a cursor upon the selected text and enter insert mode. Again like how a mouse normally behaves in Windows.
1) nmap <2-LeftMouse> ciw
You could use viw here, which will visually select the current word, but then you will still have to press c to replace it.
2) nmap <LeftMouse> <Leftmouse>i
Or replace the i with an a if you would prefer to append, rather than insert.
You can use behave mswin or so $VIMRUNTIME/mswin.vim so set up a lot of stuff so that it works more like a regular windows program.
The specific setting you are looking for are:
set select=mouse
this causes the mouse to start select mode instead of visual mode. Select mode causes the selection to be deleted and insert mode to be entered when a printable key is pressed.
As in Prince Goulash's answer
nmap <LeftMouse> <LeftMouse>i
will turn on a sort of click-to-type mode.

Resources