Skip to content

Instantly share code, notes, and snippets.

@komsit37
Last active November 11, 2021 10:49
Show Gist options
  • Select an option

  • Save komsit37/755c08068042168eb179ff9f290dae75 to your computer and use it in GitHub Desktop.

Select an option

Save komsit37/755c08068042168eb179ff9f290dae75 to your computer and use it in GitHub Desktop.

Using with SublimeREPL

  1. package controll install SublimeREPL
  2. add python 3 build system Tools/Build System/New (optional)
{
  "cmd": ["python3", "-i", "-u", "$file"],
  "file_regex": "^[ ]File \"(...?)\", line ([0-9]*)",
  "selector": "source.python"
}

This will save the file to Users/⁨pkomsit⁩/Library⁩/⁨Application Support⁩/⁨Sublime Text 3⁩/⁨Packages⁩/⁨User⁩/Python3.sublime-build Try with Command+B with this code

import sys
print (sys.version)
  1. add Python3 to SublimeREPL menu /Users/pkomsit/Library/Application Support/Sublime Text 3/Packages/SublimeREPL/config/Python/Main.sublime-menu
{
  "caption": "Python",
  "id": "Python",
  "children":[
    {"command": "repl_open",
      "caption": "Python3",
      "id": "repl_python3",
      "mnemonic": "p",
      "args": {
        "type": "subprocess",
        "encoding": "utf8",
        "cmd": ["python3", "-i", "-u"],
        "cwd": "$file_path",
        "syntax": "Packages/Python/Python.tmLanguage",
        "external_id": "python",
        "extend_env": {"PYTHONIOENCODING": "utf-8"}
      }
    }
  ]
}
  1. custom settings

Preferences/Package Settings/SublimeREPL/Settings - User

{

  // Where to look for python virtualenvs
  "python_virtualenv_paths": [
    "~/.virtualenvs",  // virtualenvwrapper
    "~/.venv"  // venv.bash https://github.com/wuub/venv
  ],

  // If set to true, SublimeREPL will try to append evaluated code to repl
  // output before evaluation (e.g. Ctrl+, f)
  "show_transferred_text": true,

  // If set to true repl view (tab) that receives text for evaluation will
  // be brought to front after text transfer. Note: This will not fire if repl view
  // is in the same tab group as the view from which the code is sent.
  "focus_view_on_transfer": false
}

Preferences/Key Bindings Default (OSX).sublime-keymap -- User

// custom SublimeREPL
  { "keys": ["super+shift+enter"], "command": "repl_transfer_current", "args": {"scope": "selection"}, "context": [{"key": "selector", "operator": "equal", "operand": "source.python"}]},
  { "keys": ["super+enter"], "command": "repl_transfer_current", "args": {"scope": "lines"}, "context": [{"key": "selector", "operator": "equal", "operand": "source.python"}]}
  1. more example
import urllib.request

fp = urllib.request.urlopen("http://www.python.org")
mybytes = fp.read()

mystr = mybytes.decode("utf8")
fp.close()

print(mystr)
@komsit37

komsit37 commented Nov 8, 2019

Copy link
Copy Markdown
Author

Combining q and R

We can use sublime-q with sublimeREPL to write q (for data preparation) and R (for plotting) in the same source (.q) file

Here is an example usage

/q
t: ([] time: .z.t + til 10; price: 10?100.0)
/r
library(rkdb)
h = open_connection('localhost',5555) #this open a connection
execute(h, "t")

R console

> library(rkdb)
> h = open_connection('localhost',5555) #this open a connection
> execute(h, "t")
       time    price
1  27339980 39.27524
2  27339981 51.70911
3  27339982 51.59796
4  27339983 40.66642
5  27339984 17.80839
...

Startup (every session)

  1. you need to start R from menu Tools/SublimeREPL/R (q) (see setup below)
  2. connect q from Tools/Sublime-q/Connect
  3. hit super+enter to send q code to external q session
  4. hit super+r to send r code to sublimeREPL R session

Setup (one time)

  1. Add custom SublimeREPL R config to your User settings (i.e. /Users/pkomsit/Library/Application Support/Sublime Text 3/Packages/User/Main.sublime-menu). This will add custom R session name R (q) in menu Tools/SublimeREPL/R (q)`.
[
     {
        "id": "tools",
        "children":
        [{
            "caption": "SublimeREPL",
            "mnemonic": "R",
            "id": "SublimeREPL",
            "children":
            [
                {"command": "repl_open",
                 "caption": "R (q)",
                 "id": "repl_r_q",
                 "mnemonic": "R",
                 "args": {
                    "type": "subprocess",
                    "external_id": "r",
                    "additional_scopes": ["tex.latex.knitr", "q"],
                    "encoding": {
                        "windows": "$win_cmd_encoding",
                        "linux": "utf8",
                        "osx": "utf8"
                        },
                    "soft_quit": "\nquit(save=\"no\")\n",
                    "cmd": {"linux": ["R", "--interactive", "--no-readline"],
                            "osx": ["R", "--interactive", "--no-readline"],
                            "windows": ["Rterm.exe", "--ess", "--encoding=$win_cmd_encoding"]},
                    "cwd": "$file_path",
                    "extend_env": {"osx": {"PATH": "{PATH}:/usr/local/bin"},
                                   "linux": {"PATH": "{PATH}:/usr/local/bin"},
                                   "windows": {}},
                    "cmd_postfix": "\n",
                    "suppress_echo": {"osx": true,
                                      "linux": true,
                                      "windows": false},
                    "syntax": "Packages/R/R Console.tmLanguage"
                    }
                }
            ]
        }]
    }
]
  1. Add custom shortcut to send R code to SublimeREPL from q source file
    Open menu Sublime Text/Preferences/Key Bindings and add below line
  { "keys": ["super+r"], "command": "repl_transfer_current", "args": {"scope": "lines"}, "context": [{"key": "selector", "operator": "equal", "operand": "source.q"}]}

Extra Explanation

The menu config is just a copy from original one with one line change by adding q to additional_scopes to tell SublimeText to also send command from q source file to R session. (The original is in /Users/pkomsit/Library/Application Support/Sublime Text 3/Packages/SublimeREPL/config/R/Main.sublime-menu).

We need to do this because SublimeREPL selects REPL session based on scope of the source file (file extension). But we want to send R code from q source file, so we need to tell R REPL session to accepts text from q source file by adding additional_scopes config.

We can also do it the other way by calling sublime-q from R source, but I prefer q syntax highlighting. And this will not work if you already have another custom REPL session for q.

Note: file name must be Main.sublime-menu because we are adding a new item to the main menu.

@icecat2005

Copy link
Copy Markdown

Regarding SublimeREPL with Python. I did all the steps, but then how do I send my selected code to a REPL console?

@komsit37

Copy link
Copy Markdown
Author

See step 4. hit super+r to send r code to sublimeREPL R session

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment