Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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

You must log in to answer this question.

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