Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: Using v:count1 as argument of a mapping

I have a mapping like below to move the cursor to the first column of input/update (the red-circled) area in a table. And I can type w or ww to get to the 2nd/3rd column if needed.

noremap <s-F8> 0f\|ew

It goes to the anchor | first, to the end of the green number, and moves to next word (can be a dot, a number or an expression) in the table as below.

A table with 3 columns for data entry

I wonder if I can use the count given in front of a normal-mode command to get to the 2nd or 3rd column directly, with a command 2<s-F8> or 3<s-F8>.

The following code doesn't work, though the @= helps separate the count from the 0 command.

noremap <s-F8> @='0f\|ew'<cr>

I studied the answer of Karkat in the post Mapping with v:count in vim and made this mapping:

noremap <expr> <s-F8> '0f\|e' . v:count1 . 'W'

But it doesn't move if a count is given. (It does move to column 1 if there is no count.)

What would be the correct way to use v:count1 in such a mapping?

like image 582
Charles Jie Avatar asked Dec 22 '25 08:12

Charles Jie


1 Answers

Use the :execute STRING command to include the v:count1 in the string:

noremap <silent> <s-F8> :<c-u>exe 'norm 0f\|e' . v:count1 . 'W'<cr>

Note: The CTRL-U is used to remove the range that Vim may insert.

like image 158
椎名梨梨花 Avatar answered Dec 24 '25 04:12

椎名梨梨花



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!