1

Say I have more than one buffer opened in different window.

How can I execute an external command with a specific buffer as a argument?

Say I want to execute :!g++ "buffer no"

How to do so?

1 Answer 1

3

Your external command won't be able to do anything with a buffer number, you'll need to pass it a file name as argument. You can use bufname() to get the buffer name of a specific buffer:

:execute "!g++ " . bufname(2)

See :help bufname().

Note that, due to different things (your path mainly but also your compiler's options), the buffer name may or may not be exactly what you want. You can use fnamemodify() to remove parts of its path or turn a relative path into an absolute one or whatever:

:execute "!g++ " . fnamemodify(bufname(2), ':p:h')

See :help fnamemodify() and the linked :help filename-modifiers.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .