How can I change current working directory in Sublime Text 3?

Minh Nguyen picture Minh Nguyen · Nov 14, 2016 · Viewed 8.7k times · Source

Whenever I open a script (Python or R), I want Sublime automatically changes the current working directory to the path of that file. Is it possible?

I added the setting "working_dir": "$file_path", to Sublime preferences but it doesn't help.

Answer

Minh Nguyen picture Minh Nguyen · Nov 14, 2016

Ok, I solved the problem. Here is what I did:

  1. Install Sublime Text 3 package PackageResourceViewer
  2. Open Command Palette, search for PackageResourceViewer: Open Resource
  3. Browse to Python resource then open Python.sublime-build
  4. Remove the default line "shell_cmd": "python -u \"$file\"",
  5. Add the following 2 lines:

    "cmd": ["python", "-u", "$file"],

    "working_dir": "$file_path",

  6. Alternately, you can replace the above 2 lines by:

    "shell_cmd": "cd $file_path; python -u \"$file\""

Do the same thing for R or other build resources if you want Sublime Text 3 automatically change the current working directory in accordance with the active scripts' path.