Hot answers tagged

28

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

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 ...


11

[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 ...


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


10

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'...


8

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

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

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

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

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 ...


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

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 ...


5

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 ...


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.


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 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 ...


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

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() ...


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 ...


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

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


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>&...


2

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 the blackhole register with "_ isn't strictly necessary, but it prevents the operation from affecting your registers. Note that setting the visual count to 1 ...


2

It looks like in the current version of vim the n command is guaranteed to move the cursor, unless there is exactly one match on the file. However, the search command / will behave as you are used to. Consider a file with these contents: word word Let's imagine that the cursor is at the beginning of the file. Searching for "word" will place the cursor at ...


2

You can use v:vcount1 with a map expression. xnoremap <expr> <c-a> "\<esc>'<V'>".v:count1."\<c-a>" The key is to escape visual mode then apply the count only to the <c-a>. command. Simply use . to repeat the command. If you rather reselect and keep visual block mode then use the following mapping: xnoremap <expr> ...


2

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\|'<cr> " or nnoremap <silent> <leader>1 :<c-u>normal! 10\|<cr>


2

The answer from @RubenWesterberg is a correct sequence of Normal mode commands to do what you want. But I read your request to not move the cursor a little more literally. Also, if you need to make substitutions like this frequently you probably want something a bit less manual. You can use standard substitution in a mapping like this: :nnoremap <expr&...


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