현재 파일의 이름과 확장자를 어떻게 얻습니까? vimscript를 사용하여 파일의

vimscript를 사용하여 파일의 이름과 확장자를 얻는 방법이 있습니까?

그렇다면 이름과 확장명을 별도로 원합니다.



답변

보낸 사람 :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

expand함수를 사용하여 이를 확장하고 해당 값을 얻을 수 있습니다.

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

예를 들면 다음과 같습니다.

$ vim '+ exe ":normal i" . expand("%:t") . "^M" . expand("%:e")' +wqa foo.bar; cat foo.bar
foo.bar
bar


답변

expand(), 사용할 수 있습니다:h expand()

스크립트에서 파일 이름을 얻으려면 다음을 수행하십시오.

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

확장을 얻으려면 다음을 수행하십시오.

let extension = expand('%:e')

expand()기능은 와일드 카드 및 특수 기호를 확장 할 수 있습니다 . 여기 %에서 현재 파일 이름으로 확장 되는 것을 사용했습니다 .


답변