Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change working directory to currently opened file

Tags:

How can I change my working directory to the path of the file that I currently have open?

Example

  1. current working directory is $HOME
  2. vim /tmp/test.log
  3. ???
  4. CWD is now /tmp/
like image 408
staackuser2 Avatar asked Oct 02 '10 22:10

staackuser2


People also ask

How do I change the working directory of a file?

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.

How do I change the current directory in Vim?

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.

How do I change the working directory in Windows?

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.

How do I change the current working directory in Python?

To find the current working directory in Python, use os. getcwd() , and to change the current working directory, use os. chdir(path) .


1 Answers

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 
like image 147
jkerian Avatar answered Sep 21 '22 19:09

jkerian