user3071121
user3071121

Reputation: 643

vim mapping key to the command that require argument

As the title, let say I have to run this command in vim <command> <argument>.

In my .vimrc I set let mapleader = ,

So in normal mode, if I type ,ab then vim will run command <command> ab. if I type ,xyz then vim will run command <command> xyz.

How to do that?

Upvotes: 3

Views: 3543

Answers (2)

Ingo Karkat
Ingo Karkat

Reputation: 172510

When the arguments are flexible (i.e. you cannot just predefine a set of mappings, as in @svlasov's answer), you can define an incomplete mappings that stays in command-line mode and thereby allows you to complete the command with the <argument>. The only downside is that you have to type <Enter> at the end (but how would Vim else know when the argument is done?)

Example

:nnoremap <Leader> :echo<Space>

Note that using a pure <Leader> mapping is bad; you can't easily have any other mappings; better use something like <Leader>e.

PS: You should use :noremap; it makes the mapping immune to remapping and recursion.

Upvotes: 9

svlasov
svlasov

Reputation: 10456

Replace <command> with a real one:

nmap <leader>ab :<command> ab<CR>
nmap <leader>xyz :<command> xyz<CR>

Upvotes: 2

Related Questions