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!

2
  • 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 Mar 4 '14 at 3:56
  • benjifisher, sorry! I fixed it for you! =] – Eduardo Lucio Mar 4 '14 at 21:03
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_:
1
  • I marked "Ingo Karkat" and "benjifisher" answers as "answer" because they are complementary! – Eduardo Lucio Jun 19 '14 at 18:29
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
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.