Reputation: 4139
I want to see the output message:
1234
when I type 1234<Enter>
in normal mode of vim.
I've tried to write some vim script here
function! PrintNum(n)
echo n
endfunction
nmap n<Enter> call PrintNum(n)
where n
here is just a dummy variable that represents regex \d+
.
How could I use some kind of regex in map command?
Note that I'm also not sure if I should use nmap
.
Upvotes: 2
Views: 326
Reputation: 172778
Here it is:
function! PrintNum(n)
echo a:n
endfunction
nnoremap <silent> <Enter> :<C-u>call PrintNum(v:count)<CR>
v:count
gets the number that can be put before any command.n
(internally referenced as a:n
), but I could have used v:count
also directly inside the function.:
command by default turns a count into a range, as we want a separate :call
, we remove this range via <C-u>
.<CR>
. The <silent>
avoids that you briefly see the mapped keys.:noremap
; it makes the mapping immune to remapping and recursion.Upvotes: 3