Vim - Visual Mode - Improve Word Selection

0

0

Gentlemen/Ladies,

I do not understand why vim is exhibiting the following behavior when I trigger the shortcut below on visual mode (.vimrc):

vmap <script> <C-LEFT> :call Test()<CR>
function! Test()
     "empty
endfunction

enter image description here

Vim goes to the first column of the line above the line where a I trigger the shortcut and shows the folow ": '<,'> call Test ()" in the status bar. But this only happens when I call a function, when I use a command like "$" (See *) the behavior is the expected.

enter image description here

(*)

vmap <C-LEFT> $

My goal is to make Vim consider the "tab" as a word when I use the "Ctrl+Left" combination, because when the first character of the line is a "tab" Vim select the first word in the top line.

enter image description here

This is the desired behavior:

enter image description here

If you have any alternative solution or workaround I would also be grateful.

Thanks!

Eduardo Lucio

Posted 2014-03-04T02:19:33.933

Reputation: 712

It is probably true that most of the people reading this page are male, but some will not be, and some of those may feel excluded because you start your question with "Gentlemen". – benjifisher – 2014-03-04T03:56:11.847

benjifisher, sorry! I fixed it for you! =] – Eduardo Lucio – 2014-03-04T21:03:26.903

Answers

1

The vmap applies when vim is in Visual or Select mode. You did not say what text was selected when you triggered the map.

The surprising behavior may be a result of the

:'<,'>

range that is automatically supplied when you switch from Visual mode to Ex mode using :. Also the way user-defined functions handle ranges by default.

That said, I would expect the cursor to end on the last line of the Visual selection. I cannot reproduce the behavior you describe.

:help :call
:help '<
:help v_:

benjifisher

Posted 2014-03-04T02:19:33.933

Reputation: 628

I marked "Ingo Karkat" and "benjifisher" answers as "answer" because they are complementary! – Eduardo Lucio – 2014-06-19T18:29:43.497

1

This is because the :call is invoked with the automatically added visual range '<,'>. :help :call explains:

When a range is given and the function doesn't handle it itself, the function is executed for each line in the range, with the cursor in the first column of that line. The cursor is left at the last line (possibly moved by the last function call).

So you either need to clear the range

vmap <script> <C-LEFT> :<C-u>call Test()<CR>

or make the function handle it:

function! Test() range

Ingo Karkat

Posted 2014-03-04T02:19:33.933

Reputation: 19 513

0

I "solved" this problem by using the following approach:

" Note: "Normal" movement with "Ctrl+Right"! By Questor
nmap <silent> <C-Right> e
imap <silent> <C-Right> <C-o>e<Right>
vmap <silent> <C-Right> e

" Note: "Normal" movement with "Ctrl+Left"! By Questor
nmap <silent> <C-Left> b
imap <silent> <C-Left> <C-o>b
vmap <silent> <C-Left> b

Note however that I do not use a function. If a function was used I would need to proceed as Ingo Karkat explains in his response (using : <C-u>).

With this simple approach I "fix" the unwanted behavior of Vim explained above.

See an example of use in https://github.com/eduardolucioac/groovim/blob/master/.vimrc

[]'s

Eduardo Lucio

Posted 2014-03-04T02:19:33.933

Reputation: 712