39

Is there a way to get the name and extension of a file using vimscript?

If so I would like the name and extension separately.

6
  • "a file"? Which file? The one in the current buffer? A file somewhere in the search path?
    – muru
    Mar 8 '15 at 22:12
  • The file that the user open via `vim <file name>
    – iProgram
    Mar 8 '15 at 22:35
  • possible duplicate of How can I see the full path of the current file? Mar 8 '15 at 22:37
  • I don't want the full path. I only want the name and extension of the file. Not the path I also want to use this in vimscript.
    – iProgram
    Mar 8 '15 at 22:40
  • Yes, both answers are actually in the linked question (specifically, CharlesL's answer)... Mar 9 '15 at 0:32
38

From :he filename-modifiers:

    :t      Tail of the file name (last component of the name).  Must
            precede any :r or :e.
    :r      Root of the file name (the last extension removed).  When
            there is only an extension (file name that starts with '.',
            e.g., ".vimrc"), it is not removed.  Can be repeated to remove
            several extensions (last one first).

    :e      Extension of the file name.  Only makes sense when used alone.
            When there is no extension the result is empty.
            When there is only an extension (file name that starts with
            '.'), the result is empty.  Can be repeated to include more
            extensions.  If there are not enough extensions (but at least
            one) as much as possible are included.
Examples, when the file name is "src/version.c", current dir
"/home/mool/vim":
  :p                    /home/mool/vim/src/version.c
  :t                                       version.c
  :t:r                                     version
  :e                                               c

You can use the expand function to expand these and obtain their values:

:let b:baz=expand('%:e')

For example:

$ vim '+ exe ":normal i" . expand("%:t") . "^M" . expand("%:e")' +wqa foo.bar; cat foo.bar
foo.bar
bar
3
  • :t "Must precede any :r or :e," yet :e "only makes sense when used alone". By the example, I'd side with the latter, but interesting that the docs contradict themselves there. Mar 9 '15 at 20:50
  • @SnoringFrog I believe what it means is that you can't do :e:t, but :t:e is allowed, if meaningless.
    – muru
    Mar 9 '15 at 20:54
  • Oh, I see how it could be read that way. That makes sense then. Mar 9 '15 at 20:56
16

You can use expand(), see :h expand()

In a script you could do this to get file-name:

let file_name = expand('%:t:r')

To get extension you could do:

let extension = expand('%:e')

The expand() function can expand wildcards and special symbols. Here I have used % which expands to current file-name.

-1

after some research i found &lt stands for file type in vimscript.

it seems typing :r in vim gives the file name as well. and replacing r with several other letters gives extensions, etc.

3
  • If you mean &filetype/&ft (&lt is not a valid option), then it does not give the extension but rather the filetype. For some files (e.g., vim, C) these are the same. For others (e.g., python), they are not. Lastly, the :r stuff seems reminiscent of the top answer's mention of filename modifiers…
    – D. Ben Knoble
    Aug 18 at 13:20
  • yeah file type and extension are sometimes different. i used &lt in a script that searches the web for selected text and it worked well.
    – pandahop
    Aug 21 at 4:01
  • 1
    um what? &lt is still not a valid vim variable. Perhaps you are confusing that with the encoding of < as &lt;, based on your web comment? Or perhaps you mean that you used %<, which IIRC is the filename without extension.
    – D. Ben Knoble
    Aug 21 at 11:29

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.