I know about % and #, which reference the filenames of the current and previous buffers in command-line mode. Is there any way to similarly reference other buffers?

For example, let's say that :ls gives me:

1 #h   foo.txt
2  h   bar.txt
3 %a   baz.txt

...and I want to use a command to the effect of:

:!pandoc foo.txt bar.txt baz.txt -o out.pdf

Only with a bit less typing. I would imagine something along the lines of

:!pandoc b1 b2 b3 -o out.pdf

So. Is there anything like this? Is there an addon somewhere, or some handy bit of vimscript I could use?


You can do:

:!pandoc #1 #2 #3 -o out.pdf

This is mentioned briefly in the help at :h :_#n.

If you need the full path to the file (say, because some of the files are in different directories), you can append :p like so:

:!pandoc #1:p #2:p #3:p -o out.pdf
| improve this answer | |
  • Nice! I wasn't sure how to find this in the help documents (:h # brings up info about the normal-mode object). I might sometimes need the complete filepath, which according to the help would be :!pandoc #1:p #2:p #3:p -o out.pdf. – evilsoup Oct 24 '15 at 22:43
  • 3
    @evilsoup, a good trick to know is Ctrl-D when typing a help command. It lists the possible completions. Try typing :h # and then pressing Ctrl-D. Also note that unlike shell autocompletion, this lists any help topic containing the typed substring—it doesn't have to be at the start of the help topic name. – Wildcard Oct 25 '15 at 13:39
  • @Wildcard thanks for the tip, that'll come in handy I think. – evilsoup Oct 25 '15 at 15:22
  • You can also reference current buffer filename with % and alternate file with #. E.g. !kdiff3 % # will run kdiff3 command with the filenames of current buffer and the alternate one. – Ruslan Jun 24 '19 at 10:50

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.