41

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.

muru
  • 24,838
  • 8
  • 82
  • 143
iProgram
  • 1,589
  • 2
  • 15
  • 25

3 Answers3

41

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
muru
  • 24,838
  • 8
  • 82
  • 143
  • :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. – SnoringFrog Mar 09 '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 09 '15 at 20:54
  • Oh, I see how it could be read that way. That makes sense then. – SnoringFrog Mar 09 '15 at 20:56
17

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.

Slothie
  • 3
  • 3
Runium
  • 1,237
  • 10
  • 20
-2

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.

pandahop
  • 1
  • 1
  • 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 '21 at 13:20
  • yeah file type and extension are sometimes different. i used < in a script that searches the web for selected text and it worked well. – pandahop Aug 21 '21 at 04: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 '21 at 11:29