Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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