How to get the file path of a buffer

pathvim

I know that the register % contains the full path of the current buffer. But how to get the full path of another buffer by its number?

Is there such a function/command in VIM?

I want to explain how I came to this question…

There were 2 buffers open. The first was an XML file in the left window and the other was an XSD file in the right window. I edited both of them. During the editing I wanted to validate the XML against the schema.

However the command

!xmllint --schema /tmp/schema.xsd %

of course worked well only if the current buffer was the one with the XML. So I was curious about whether it would be possible to replace /tmp/schema.xsd with some command or function call that would determine the full path by the buffer number. Something like:

!xmllint --schema getBufferPath(3) %

Best Answer

You can use the expand() call. For example

:echo expand("#2:p")

Will print the full path of the file in buffer #2 you can list all buffers with :ls

You can use other modifiers and other keywords (For full info page look at :help expand())

Here is an quick excerpt:

           When {expr} starts with '%', '#' or '<', the expansion is done
            like for the cmdline-special variables with their associated
            modifiers.  Here is a short overview:

                   %               current file name
                    #               alternate file name
                    #n              alternate file name n
                    <cfile>         file name under the cursor
                    <afile>         autocmd file name
                    <abuf>          autocmd buffer number (as a String!)
                    <amatch>        autocmd matched name
                    <sfile>         sourced script file name
                    <slnum>         sourced script file line number
                    <cword>         word under the cursor
                    <cWORD>         WORD under the cursor
                    <client>        the {clientid} of the last received
                                    message server2client()
            Modifiers:
                    :p              expand to full path
                    :h              head (last path component removed)
                    :t              tail (last path component only)
                    :r              root (one extension removed)
                    :e              extension only

           Example:
                    :let &tags = expand("%:p:h") . "/tags"
            Note that when expanding a string that starts with '%', '#' or
            '<', any following text is ignored.  This does NOT work:
                    :let doesntwork = expand("%:h.bak")
            Use this:
Related Question