Hot answers tagged

32

Operator-pending mode Between typing an operator (like d, c, or gU) and a motion (like w, i}, or /foo<CR>), Vim is in Operator-pending mode. You can create mappings for this using :omap and :onoremap. In my examples, I'm going to map the Operator-pending d to w. This is a random choice, because I don't know what you actually wanted to use it for. ...


12

[count] is optional. If given, the mapping works for count lines (ie. comments out count lines). <Leader> is usually the backslash key (\), although this can be remapped (also see What is <Leader>? and How can I find out what <Leader> is set to? And is it possible to remap &ltLeader>?) cc is exactly that: two c's. NERDComComment has nothing ...


12

From :help N%: Go to {count} percentage in the file, on the first non-blank in the line |linewise|. To compute the new line number this formula is used: ({count} * number-of-lines + 99) / 100 See also 'startofline' option. I didn't know this either, by the way. But I typed :help %<C-d>, and N% looked like a good match ;-) Also see: How do I ...


12

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 them to call a function like so: fun! SetOpFunc() set opfunc=CountSpaces return 'g@' endfun nno <expr> <F4> SetOpFunc() This technique isn'...


10

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 count and then performs the desired operation. Further reading :help :map-expression :help v:count :help expr1


9

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> will move 6 words A mapping in vim will replace the meaning of a key/sequence of key pressed. So when you do :map <F8> 3w and 2<F8> 23w Vim ...


7

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>m @@ This allows me to use my leader key (,) plus m to run the last macro. Then you can precede this with a number to repeat the macro. Now 3,m is ...


7

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! AtRepeat(_) " If no count is supplied use the one saved in s:atcount. " Otherwise save the new count in s:atcount, so it will be " applied to repeats....


6

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 the last normal mode command and can thus be used to get a count before a mapping. Here the mapping uses v:count1 which is like v:count but default to zero ...


6

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 [count][command] was literally another way to type [command] repeated [count] times. Turns out commands are free to interpret [count] as they like, and J takes it ...


6

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> Reference :help insert


6

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> ":\<C-u>" . repeat(">", v:count1) . "\<CR>" nnoremap <expr> <F6> ":\<C-u>" . repeat("<", v:count1) . "\<CR>" and its visual ...


6

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 comes up. If I do that and run 30<C-x>b, this pops up in the command line: :.,.+29call MyFunction(30) This is because it's basically like you typed 30:...


5

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 characters starting from the cursor to be selected. Typing <esc>5v causes the 5 characters starting from the cursor to be selected. This visual selection ...


5

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 mode command. The former should work for you, as it will be zero when no count has been given. The latter defaults to 1 in same situation. Here is a minimal ...


5

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.


4

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's remap @ to call our custom function: nnoremap <silent> @ :<C-U>call MacroWithCount()<CR> And the function: function! MacroWithCount() ...


4

When using counts and calling functions a range will be inserted automatically by Vim. You can see this by doing simply doing 3:. A common solution is to use <c-u> to remove the range. See :h c_CTRL-U for more help. nmap YY :<c-u>call Clipboard()<CR> However there are a bunch of issues worth going over: You should generally avoid ...


4

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 lines down, indent twice: v3j2>


4

When you do : :2call TestCursorPosWithCount() ... the cursor moves to the beginning of the line 2 and the function is being executed from there. If you use :<C-u>call TestCursorPosWithCount() instead, the original cursor position is available, and you can also still get the count number using v:count.


4

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 even using ex-commands. The best way is typically what keeps you doing your work and not thinking about golfing your key strokes Let's look at some ways to ...


3

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 cscope. Jump to a tag: Ctrl] Jump back: CtrlO Jump to a tag again: CtrlI


3

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 advantage of allowing you to compress the numerical mappings down a little: for i in range(1, 9) execute 'nnoremap <silent> <leader>'.i.' :<C-U&...


3

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 no count is provided. Some commands interpret count 0 as equivalent to 1. For this reason, a shortcut v:count1 is also provided which is the same as v:count ...


3

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 parts, so that you can undo each part of it independently. For that, the generic way is to enter CTRLGu in Insert mode. (See :h i_CTRL-G_u). So simply add CTRLGu ...


3

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. Example: > :map _x :<C-U>echo "the count is " . v:count<CR> < Note: The <C-U> is required to remove the line range ...


3

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 text object, and RJJ to act on the current line or on a set of lines if given a count. Having said that, you can have your mapping behave differently and act on ...


2

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:matchup_motion_override_Npercent. For larger {count}, {count}% goes to the {count} percentage in the file. The default value is 6. This means 5% would cycle five b:...


2

The command is used in normal mode by typing first a count, like 10, followed by the <Leader> key (default is \) and finally cc. Example; type 10\cc in normal mode to comment out 10 lines starting at the cursor position. Omit the count to only comment out the current line. Many people like to remap the <Leader> key to ,. More info here: http://...


2

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:count1 * 10 . '<C-w>+' nnoremap <expr> <C-w>- v:count1 * 10 . '<C-w>-' nnoremap <expr> <C-w>< v:count1 * 10 . '<C-w>&...


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