How can I change my working directory to the path of the file that I currently have open?
Example
Answer: Use the cd Command The current working directory is the directory or folder where you are currently working. You can use the cd (change directory) command to change the current working directory or move around the file system. This command will work in all Linux distribution.
Vim has an option for this. Here's the documentation: 'autochdir' 'acd' boolean (default off) global When on, Vim will change the current working directory whenever you open a file, switch buffers, delete a buffer or open/close a window. It will change to the directory containing the file which was opened or selected.
To change current working directory under the current drive, use command " cd new-path " (change directory). It is important to take note that you need to set the current drive first (via " x: " command) before setting the current directory under the current drive.
To find the current working directory in Python, use os. getcwd() , and to change the current working directory, use os. chdir(path) .
That's actually a builtin. (here's the help link)
:set autochdir
Stick that in your .vimrc or whatnot (:e $MYVIMRC
). As mentioned here, sometimes plugins will have issues with that and you need to use something more complicated like
autocmd BufEnter * lcd %:p:h
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