1

There is a built-in mapping Ctrl-g which presents the current filename immediately in the command area. I would like to augment this by tapping Ctrl-g again to copy the filename to my system clipboard register.

At the moment, I have added this to my .vimrc

nnoremap <silent> <c-g><c-g> :let @+=@%<CR>

However, this has the downside of delaying the presentation of the filename when only one Ctrl-g sequence is used. Is it possible to make it so one Ctrl-g immediately shows the filename and the second copies it to a register?

2
  • The problem is one of common prefixes: vim has to wait until you type further keys or a delay has passed to decide what command you entered.
    – D. Ben Knoble
    Aug 15 at 10:40
  • @D.BenKnoble yup and I rely on that feature too and I think it makes a lot of sense for it be the default. In this case, as all the actions build on each other and are harmless, I was hoping there is a way to opt-out and just immediately apply each action in sequence.
    – casr
    Aug 15 at 11:08
1

The current buffer name is already in a register :h quote_%:

:echo @%

Is it possible to make it so one Ctrl-g immediately shows the filename and the second copies it to a register?

No, it doesn't make sense. You could rather make one c-g do both. For example,

nnoremap <silent><C-G> :let @+=@%<CR><C-G>

Though this simplistic attempt also breaks {count}<c-g>.

1
  • I'm copying it to the + register because that is my system clipboard and it is useful for pasting into other applications. However, I will update my question to use the shortened form which I just tried and seems to work fine.
    – casr
    Aug 15 at 8:16
1

This is just for fun:

func! MyCg() abort
    func! s:timer_handle(t) abort closure
        unlet b:myctrlg
    endfunc
    if get(b:, "myctrlg", v:true)
        exe "normal! \<c-g>"
        let b:myctrlg = v:false
        call timer_start(500, {t -> s:timer_handle(t)})
    else
        let b:myctrlg = v:true
        let @+ = @%
    endif
endfunc
nnoremap <silent><C-g> :call MyCg()<CR>
  • <C-g><C-g> echo what a single <C-g> does and copy filename to + register (should be pressed with less than 500ms timeout).
  • <C-g> does usual <C-g> things.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.