简体   繁体   中英

How to map `g CTRL-G` command in visual mode in Vim?

I really like this command as it shows me number of selected lines, words, characters & bytes. http://vimdoc.sourceforge.net/htmldoc/editing.html#v_g_CTRL-G

But somehow I can't get the mapping to work:

I tried:

vnoremap <leader>z g<C-g>
vmap <leader>z g<C-g>

When I tried it, I was like "wow, it really doesn't work". Then I realized that the status message was showed and disappeared just too quickly. So for your mapping "to work" you can show the last status message using variable v:statusmsg

vnoremap <leader>z g<C-g>:<C-U>echo v:statusmsg<CR>

but that leaves you in normal mode. You can reselect the area again by g v (it wouldn't do to add it in mapping, it would redraw the status message with -- VISUAL -- ). If you know about some method how to keep message displayed (ie. without the need to display it again with echo v:statusmsg ), then you don't need this not very useful workaround (as it is it seems better to use g CTRL+g combination instead of mapping to me).


Edit: I found interesting function sleep (or gs , like go sleep ).

vnoremap <leader>z g<C-g>2gs

Now you can see the message. You are not able to do anything for two seconds but it can be interrupted with CTRL-C or CTRL-Break on MS-DOS. (Obviously, you can define different time period.)

Another way that seems to work (at least in gVim):

vnoremap <leader>z :call feedkeys("gvg\<C-G>")<CR>

The idea here is that the characters specified in the feedkeys() call are "injected" after the call is executed (ie after <CR> is run by the <leader>z mapping). Hence the need to reselect the visual area using gv .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM