I'm trying to make autocommands that will run only when the buffer is a man page (:Man), but I can't get the right pattern as I'm not too familiar with regex and globs. Here's what I've tried so far:

autocmd! BufEnter *man://* call Myfunc()
autocmd! BufEnter [^man://]* call Myfunc()

but nothing seems to work. Any alternative ideas are okay too.

2 Answers 2


It's probably better to use a FileType buffer. The problem with BufEnter is that the buffer may not yet be fully set up when it's run; I also don't think it's run for new buffers.

I'm also not entirely sure if I follow where you go the man:// pattern from? I don't see that in the source? And using bufname('') gives me /home/martin/ls.~, not man://...?

At any rate, something like this should work as you expect:

autocmd! FileType man call Myfunc()

From what I have tested this works:

function! Test()
    echom "entering man buffer " . expand('%')

autocmd! BufEnter man://* call Test()
  • odd.. I tried it in vim8.1 and nvim0.3 by vim --noplugin -u test.vim but It seems not working.. any idea? Commented Mar 12, 2019 at 11:29
  • It only works when I open the man page manually through new man://test(1) but not using the :Man test command. Commented Mar 12, 2019 at 12:23
  • Actually it there is something weird: when I do my firt :Man ls the autocommand doesn't work but if I use :Man again it works. I would say that maybe :Man sets the filename after the BufEnter event is triggered. Depending on what you want to do maybe you can use another autocommand event.
    – statox
    Commented Mar 12, 2019 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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