Search code examples
filepathvim

How to get path to the vim being executed in vimscript


For example, in directory /abc the command vim def/my.txt would edit file /abc/def/my.txt. I want to get the path /abc in my vim plugin script, but I don't know which function can do it.

I have read related articles, like
(Please add http:// to following items if you want to access, I don't have enough reputation to post more than 2 links o(╯□╰)o ):

But useless.

Thanks.

==============================
I noticed at http://vim.wikia.com/wiki/Get_the_name_of_the_current_file, it said:
:echo @% def/my.txt
I thought I found the hope.
Because I can get /abc by minus def/my.txt from full path.
But I tested above command in my vim environment, it didn't work. It just print my.txt.


Solution

  • I think you're looking for the :pwd command / getcwd() function.

    When you edit def/my.txt from /abc, the latter is your current directory, and all files are addressed relative to it. (Unless you have something like :set autochdir.)

    For dealing with file paths, the fnamemodify() function is also very helpful; e.g. to turn filespecs into absolute ones (:p argument), or to cut off head, tail, or file extension.