I can see that I've got a terminal buffer open by running :ls R which finds all buffers with a running terminal job, but I don't know how to capture just the buffer number.

The closest I can get is echo bufnr("cmd.exe") because the buffer name has the command the terminal is using. This isn't crossplatform though.

bufnr("R") doesn't seem to search the flags the same was :ls R does.

  1. By :h 'buftype'

    echo filter(map(getbufinfo(), 'v:val.bufnr'), 'getbufvar(v:val, "&buftype") is# "terminal"')
  2. By :h getwininfo()

     echo uniq(map(filter(getwininfo(), 'v:val.terminal'), 'v:val.bufnr'))
| improve this answer | |
  • Thanks for these! Makes sense there isn't a builtin way to get a terminal buffer's number directly given how you can have so many – TankorSmash Jun 10 at 0:02

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.