Set the working directory to the position of the active script in SublimeREPL

I am using Sublime Text 3 with SublimeREPL for my commands in both R and Python. Whenever SublimeREPL is loaded, it has the current working directory set to the Sublime Text installation directory. This happens on both Windows and Mac OSX. Often times, I need to run code from R or Python that opens a file that is in the same folder as the file, R

or py

that I am running. Under normal circumstances (i.e., not using Sublime Text) this works fine, since the R or Python interpreter knows to look in the same directory as the script.

However, with SublimeREPL, commands are "passed" as text to SublimeREPL, so SublimeREPL has no idea where the program came from. As a result, I have to explicitly specify the location of the file I'm trying to access in each of my programs, which is a bit cumbersome, especially if my programs are being moved (which is what they do).

Is there a way to get SublimeREPL to set the current working directory on boot to the same directory as the currently active script file? This would be a fine solution, as it would simply mean restarting the REPL if I were to switch to using a file located in a different directory.

enter image description here

+3


source to share


1 answer


I had the same question and found the answer here .

For Python and IPython, in your folder Packages/User

create SublimeREPL/config/Python/Main.sublime-menu

where you specify "cwd": "$file_path"

:



[
    {
        "id": "tools",
        "children":
        [{
            "caption": "SublimeREPL",
            "mnemonic": "r",
            "id": "SublimeREPL",
            "children":
            [
                {
                    "caption": "Python",
                    "id": "Python",

                    "children":[
                        {
                            "command": "repl_open",
                            "caption": "Python - Anaconda",
                            "id": "repl_python",
                            "mnemonic": "p",
                            "args": {
                                "type": "subprocess",
                                "encoding": "utf8",
                                "cmd": ["/path/to/Anaconda/python", "-i", "-u"],
                                "cwd": "$file_path",
                                "syntax": "Packages/Python/Python.tmLanguage",
                                "external_id": "python",
                                "extend_env": {"PYTHONIOENCODING": "utf-8"}
                            }
                        },
                        {
                            "command": "repl_open",
                            "caption": "IPython - Anaconda",
                            "id": "repl_python_ipython",
                            "mnemonic": "p",
                            "args": {
                                "type": "subprocess",
                                "encoding": "utf8",
                                "autocomplete_server": true,
                                "cmd": ["/path/to/Anaconda/python", "-u", "${packages}/SublimeREPL/config/Python/ipy_repl.py"],
                                "cwd": "$file_path",
                                "syntax": "Packages/Python/Python.tmLanguage",
                                "external_id": "python",
                                "extend_env": {
                                    "PYTHONIOENCODING": "utf-8",
                                    "SUBLIMEREPL_EDITOR": "$editor"
                                }
                            }
                        }
                    ]
                }
            ]
        }]
    }
]

      

I think for R it is the same.

+1


source







All Articles