Xonsh: How can I write .xonshr to keep python syntax?

Created on 23 Sep 2018  路  4Comments  路  Source: xonsh/xonsh


.xoshrc is very useful, but it does not keep python syntax.
I want to write .xonshrc as python file with some editors (VSCode, vim).

$HOGE can be written to __xonsh_env__["HOGE"].
How can commands(ex. vim .) be written in the same way?

from xonsh.environ import Env

# $XONSH_SHOW_TRACEBACK
env = builtins.__xonsh_env__  # type: Env
env["XONSH_SHOW_TRACEBACK"] = True

def my_cheat():
    # I want to write as python syntax
    cd my_cheats
    vim .

xonfig

| xonsh            | 0.7.7           |
| Python           | 3.7.0           |
| PLY              | 3.9             |
| have readline    | True            |
| prompt toolkit   | 2.0.4           |
| shell type       | prompt_toolkit2 |
| pygments         | 2.2.0           |
| on posix         | True            |
| on linux         | False           |
| on darwin        | True            |
| on windows       | False           |
| on cygwin        | False           |
| on msys2         | False           |
| is superuser     | False           |
| default encoding | utf-8           |
| xonsh encoding   | utf-8           |
| encoding errors  | surrogateescape |
+------------------+-----------------+

Most helpful comment

In vscode you can change the language by "Change Language Mode" command (Ctrl+K, M) or you can add the following to your settings.

    "files.associations": {
        ".xonshrc": "python",
        "*.xsh": "python",      
    }

All 4 comments

Hey @74th -- xonsh syntax is a superset of Python syntax and there are some bits that will be different.
If I understand what you're looking for, you want syntax highlighting and editor help when editing your xonshrc? I'm not sure how to do this in vscode, but I'm sure there's a way. In vim you can run :set syntax=python and that will work when editing your xonshrc

In vscode you can change the language by "Change Language Mode" command (Ctrl+K, M) or you can add the following to your settings.

    "files.associations": {
        ".xonshrc": "python",
        "*.xsh": "python",      
    }

We should probably have a bit about syntax highlighting in editors in the docs.

Thanks for responses.
I see xonsh is superset of python.

I found __xonsh_execer__.eval() is able to be used.

def _my_cheat():
    cd my_cheats
    name = $(ls | peco).strip()
    vim @(name)
    cd -
aliases["open_cheat"]=_mycheat

This can be rewritten as follows.

def _my_cheat():
    __xonsh_execer__.eval("cd my_cheats")
    name = __xonsh_execer__.eval("ls | peco").lines[0].strip()
    __xonsh_execer__.eval(f"vim {name}")
    __xonsh_execer__.eval("cd -")
aliases["open_cheat"]=_mycheat
Was this page helpful?
0 / 5 - 0 ratings