2

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.

Community
  • 1
  • 1
mtv_piba
  • 67
  • 1
  • 8
  • I am not sure which path do you want to get. you want to get usr's current dir? (under which dir the user has started vim?) vim's `:pwd and getcwd()` will give you that, as Ingo answered. However, user can in vim execute commands, which leading to the dir gets changed, like `:cd ..` or with `autochdir option`. in this case, you won't get that dir any more. When your plugin function will be triggered? – Kent Aug 06 '14 at 11:36
  • I made commands in my vim plugin to call the path function, the design is that user need execute the commands once they launch "vim" or "vim some_path_to_file". So :cd .. will not have chance to impact it but autochdir maybe. But anyway, it doesn't matter to my logic, I just want to get the current dir where user execute "vim" with or without file name arguments. For more details, I explained why I found pwd and getcwd() not work in my cases. Thanks for your asking. – mtv_piba Aug 06 '14 at 13:09

1 Answers1

2

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.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • 1
    Thanks. But I've found :pwd and getcwd() not work in my case. If I am at /abc/, then run "vim def/my.txt", then :pwd and getcwd() all return /abc/dev/, that's not my expectation. But if I just run "vim" at /abc/, then it will return /abc. Generally speaking, I want to get the dir where user run "vim" or "vim the_path_to_file". – mtv_piba Aug 06 '14 at 12:58
  • Do you have the mentioned `'autochdir'`, or an equivalent `:autocmd`?! Because Vim doesn't normally behave that way. In any way, you could store Vim's CWD during startup by putting this into your `~/.vimrc`: `let g:cwd = getcwd()`. – Ingo Karkat Aug 06 '14 at 13:07
  • And I've tried a few options with fnamemodify(), found nothing useful:( Yes! Good point @Ingo Karkat! I will examine my ~/.vimrc. I almost there :) Thanks a lot:) – mtv_piba Aug 06 '14 at 13:12
  • Oh my god~~~~~~~~~~~~~~~~~You're right ::>_<:: There is "set autochdir" in it (●-●) Thanks a lot Ingo! – mtv_piba Aug 06 '14 at 13:15
  • `:pwd` does not work for me but `:!pwd` does. However, I don't know how to execute the `getcwd()` function. – Jean Paul Apr 25 '19 at 11:27