In vimscript, if I want to get the value of current working directory for current buffer (could be changed by :lcd
), which variable or expression would I use to fetch that?
This can be an absolute or relative path, so :cd .. will move up one level. Or you can use :cd %:h which will also change to the directory the current file is in, but without setting autochdir. will change directory to your home directory (or on windows, print the current directory).
Windows maintains the current directory in the environment variable %CD% .
:cd is a Vim command operating in the Vim program space and the directory it changes is associated with the Vim process. One way this is useful is if you try to edit a file without a path (e.g. foo. txt ) Vim will look in this directory for the file.
Select a file or directory name and press Enter to open that file or directory. (For example :e /home/user displays the contents of that directory.) To return to the explorer window, press Ctrl-^ (usually Ctrl-6). You can also "edit" a directory to explore that directory.
let cwd = getcwd()
or
let cwd = fnamemodify('.', ':p')
. Both always return effective value for the current buffer, but determination of whether this is working directory local to buffer (i.e. changed by :lcd
/'autochdir'
) or global one (i.e. changed by :cd
/untouched since vim start) is the more interesting question. I do not know the answer for it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With