Hot answers tagged

13 votes
Accepted

How to allow count *before* my custom operator?

You can call a function transparently within a mapping, thus circumventing mode changes (e.g. by pressing :) or losing counts, by using <expr> mappings, similar to what you suggested. Just use ...
Antony's user avatar
  • 2,560
11 votes
Accepted

Can I repeat a macro with the "dot operator"?

Give this a try. It remaps @ so that g@ (plus a dummy motion l) is used afterwards, thus becoming the last operator and subject to repeating with .. " When . repeats g@, repeat the last macro. fun! ...
Antony's user avatar
  • 2,560
10 votes
Accepted

How can a mapping have behavior dependent on the presence of a count?

You can use expression maps and Vim's predefined v:count variable to do this: nnoremap <expr> j v:count > 1 ? "j" : "gj" nnoremap <expr> k v:count > 1 ? "k" : "gk" This checks the ...
Rich's user avatar
  • 31.7k
10 votes

Can I repeat a macro with the "dot operator"?

To repeat your last macro you can use @@ so 3@@ would essentially run @q 3 times. However the @ keystrokes can be unwieldy, so in my .vimrc I have the lines: "- Lazy macro repeat nmap <leader&...
Shadoath's user avatar
  • 258
9 votes

Repeat n time a mapping starting with a count

You can use the expression register for this: :nnoremap <F8> @='3w'<CR> This will use the expression register to create a repeatable mapping you can prefix with a number: 2<F8> ...
nobe4's user avatar
  • 16k
7 votes

Unexpected behavior using "<count>J" to join multiple lines

After further research and office discussion, I realize this is indeed the expected behavior. According to the vim help: J Join [count] lines, with a minimum of two lines. I've always thought [...
Eric Burgess's user avatar
6 votes

Can I repeat a macro with the "dot operator"?

You can record the use of a macro in another macro, and repeat that. qqdwq qr2@qq Now 3@r is like running @q six times.
jjaderberg's user avatar
  • 3,469
6 votes

Can I make the indent commands take an additional count that doesn't affect the motion?

You can do: :>>>> to shift the current line 4 times. So you could write a custom normal mode mapping that shifts the current line {count} times: nnoremap <expr> <F5> ":\<...
romainl's user avatar
  • 40k
6 votes
Accepted

Repeat n time a mapping starting with a count

Another solution is to use the variable v:count like this: nnoremap <F8> :<C-u>execute "normal! " . v:count1 * 3 . "w"<CR> See :h v:count: The variable contains the count given for ...
statox's user avatar
  • 49.6k
6 votes

Insert mode counts in neovim

Yep. {count}i{text}<esc>. For example: 80i-<esc> will insert 80 equal signs. Great as a markdown horizontal rule. And mappable in your ~/.vimrc nnoremap <Leader>hr 80i-<esc> ...
ddrscott's user avatar
  • 431
6 votes
Accepted

Make mapping that accepts count independent of line count

The answer is actually not as complicated as you might think. A trick that I have found very useful in situations similar to this is to remove the <cr> from the end of your mapping and see what ...
DJMcMayhem's user avatar
  • 17.5k
5 votes
Accepted

Can my mapping/function detect when count/range 1 is given instead of none?

To get the count given to <C-E>, you don't need to use the range to do it. There are two pre-defined variables you can use, v:count and v:count1, which contain the count given to the last normal ...
Sakari Cajanus's user avatar
5 votes
Accepted

What does [count]v and [count]V do? How is it useful?

You can see what it does for yourself by performing an operation on a 1 character selection. vy3v<esc>5v In that command, you select 1 character and yanked it. Typing 3v causes the 3 ...
Tommy A's user avatar
  • 6,750
4 votes

How to get the count given to a macro?

It seems that there is no built-in way to do what I want. So I came up with this small chunk of code following @DjMcMayhem comment. It is not extensively tested but it seems to be working. First let'...
statox's user avatar
  • 49.6k
4 votes

Can I make the indent commands take an additional count that doesn't affect the motion?

alternate using visual mode: V4> Or just v4> as mentioned in the SO thread How do I indent a single line multiple times in vi or vim? For multiple lines, for example - current line and 3 ...
Sundeep's user avatar
  • 1,086
4 votes
Accepted

`gt` is not consistent with `gT` (and many other commands) when `{count}` is prepended

I can't really explain why it works like this, my best guess is that you more often need to go a tab by its index rather than going 3 tabs forwards. However, if you really don't like how Vim works and ...
statox's user avatar
  • 49.6k
4 votes

How to add a [count] to a binding

Your mapping is tricky because you don't want to yank the leading whitespaces. You cannot simply supply a count to your map as you would do for an operator. You need to define a new command with the -...
perelo's user avatar
  • 461
4 votes
Accepted

Fastest way to find last occurence of a character in line?

I find it is best to not get too wrapped up in optimizing for minimal keystrokes, but instead learning & using motions effectively. Sometimes this means multiple motions, multiple operations, and ...
Peter Rincker's user avatar
3 votes

How to clear digit arguments in a mapping?

One way to do it would be to carry out the movement with a function. The existing count will be converted to a range on the command line, but we can clear this with <C-U>. This has the slight ...
Rich's user avatar
  • 31.7k
3 votes
Accepted

Is it possible to detect when a command uses no count?

0 is not a valid count. 0_x does not mean give a count of 0 to command _x, it means go to the first column of the line, then run command _x. You cannot give 0 as a count. Hence, v:count == 0 means ...
Mass's user avatar
  • 14k
3 votes

Bidirectional tag stack movement with cscope in vim

Ensure setting the cscope using the following in your ~/.vimrc file: set cscopetag set cst set csto=0 set tags=./tags,tags;/ cs add cscope.out Once it is done, re-login into the terminal to run ...
Vicky's user avatar
  • 131
3 votes

How to add a [count] to a binding

You can use the v:count variable to access the count passed to a mapping. If you don't pass any count, the v:count variable will evaluate to zero. If you prefer something that evaluates to 1 instead, ...
filbranden's user avatar
  • 28.7k
3 votes

Undo <count> macro instances

Typically, Vim considers a mapping or a recorded macro as a single change, so if you press u to undo it, it will undo all of it. There exists the possibility to break an undo-sequence into smaller ...
Christian Brabandt's user avatar
3 votes
Accepted

Repeating command for running external script?

Based on :help v:count: *v:count* *count-variable* v:count The count given for the last Normal mode command. Can be used to get the count before a mapping. Read-only. ...
D. Ben Knoble's user avatar
  • 25.8k
3 votes
Accepted

Create mapping that works both with a motion and a count

Usually you would have a separate mapping for an operator that would work on the current line and take a count to work on a set of lines. For example, you could have RJ{motion} to act on a motion or ...
filbranden's user avatar
  • 28.7k
2 votes

Can I make all "resize split" commands take a default count?

There is not a setting that works as you've described, so you're probably on the best track already. Using <expr> and v:count can get you what you want: nnoremap <expr> <C-w>+ v:...
SnoringFrog's user avatar
2 votes

What does [count]% do?

Plugin Match-Up: g:matchup_motion_override_Npercent This plugin offers the possibility to use [count]% for both motions: {count}% forwards {count} times. Requires {count} <= g:...
Hotschke's user avatar
  • 4,740
2 votes

Can I repeat a macro with the "dot operator"?

By default space in vim is "move right 1 character" which is already covered by l, so I've remapped space to re-run a macro from the q buffer: noremap <Space> @q To use, just record a macro ...
brain_bacon's user avatar
2 votes
Accepted

How to clear digit arguments in a mapping?

You almost had it: you need <c-u> to clear the count and then either you use quotes with :exe, or you use none In short: nnoremap <silent> <leader>1 :<c-u>exe 'normal! 10\|'&...
Luc Hermitte's user avatar
  • 17.3k
2 votes

reset count for `v`

One easy way to workaround this would be by typing: v"_y This will yank a single character into the black hole register. Therefore, it's only effect is to reset the visual count to 1. Specifying ...
Rich's user avatar
  • 31.7k

Only top scored, non community-wiki answers of a minimum length are eligible