Xonsh: How to create an alias to a command's subprocess macro, e.g. 'cd' should always call 'cd!'

Created on 19 Feb 2021  路  5Comments  路  Source: xonsh/xonsh

I would like to replace the builtin alias cd with its subprocess macro equivalent cd! as the ability to copy&paste arbitrary paths to prompt without having to do any escaping is way too useful!
How do I do that "by default" (I don't see much use for the nonmacroed cd)?

xonfig

$ xonfig
+------------------+-----------------+     
| xonsh            | 0.9.26          |     
| Python           | 3.9.1           |     
| PLY              | 3.11            |     
| have readline    | False           |     
| prompt toolkit   | 3.0.16          |     
| shell type       | prompt_toolkit  |     
| pygments         | 2.7.4           |     
| on posix         | False           |     
| on linux         | False           |     
| on darwin        | False           |     
| on windows       | True            |     
| on cygwin        | False           |     
| on msys2         | False           |     
| is superuser     | False           |     
| default encoding | utf-8           |     
| xonsh encoding   | utf-8           |     
| encoding errors  | surrogateescape |     
| on jupyter       | False           |     
| jupyter kernel   | None            |     
| xontrib          | []              |     
+------------------+-----------------+     

Expected Behavior

(after adding a special alias to a subprocess macro)
cd should work as cd!, e.g.
cd C:\Dev\1\test\t space\[b] should change directory to C:\Dev\1\test\t space\[b]

Current Behavior

cd C:\Dev\1\test\t space\[b] doesn't work as is and either requires a subprocess macro cd! C:\Dev\1\test\t space\[b] or a raw string escaping

For community

猬囷笍 Please click the 馃憤 reaction instead of leaving a +1 or 馃憤 comment

parser question windows

All 5 comments

Hi! I think there is no way to do this because cd [brackets] is the python code for parser because it contains brackets.

I suppose that your real problem is about autosuggestion of path that contains brackets:

cd /
mkdir '/tmp/[123]'
cd /tmp/<Tab>
# select [123]
cd /tmp/[123]
# SyntaxError: <xonsh-code>:1:14: ('code: \n',)
# cd /tmp/[123]/

But this should work like:

cd /tmp/<Tab>
# select [123]
cd '/tmp/[123]'  # quotes added
# cd-ing to the '/tmp/[123]' directory 

This can be addressed to the #3766 issue.

No, my real problem is the one I've stated - copy&pasting, I don't need autocomplete for that
Moreover, autocomplete with cd works fine, it turns a path string in a raw string r'Path' on tab and adds paths with brackets more or less fine

I don't understand, though, why it's not possible - is the only way to invoke macro syntax via adding ! manually in the prompt? Can I not create a function with my custom name that would work as though it's a subprocess macro? Can there not be a xontrib that parses cd at the start and replaces it with cd!? Or something
What's the actual limitation here?

If it's not possible, I'd like to suggest this as a feature, unless that's somehow also not possible due to the way xonsh is made

Ah, got it. You wrote "How to create an alias" and this is impossible as I wrote. But I have one thought. You can create a xontrib that can modify the command using on_transform_command event:

@events.on_transform_command
def _macro_cd(cmd, **kw):
    if cmd.startswith('cd '):
        return 'cd! ' + cmd[3:]
    return cmd

mkdir '/tmp/[123]'
cd /tmp/[123]
# enjoy :)

You wrote "How to create an alias" and this is impossible as I wrote

That's because the idea of replacing cd with cd! via a xontrib hadn't popped into my head yet :)
By the way, I got it from your own great xontrib-sh (you might've noticed I've submitted a PR there to extend its functionality a bit, so saw that def onepath(cmd, **kw): returning adjusted cmd)!
Glad to know it's a workable solution, will try to do that
Thanks!

Just for reference: I've made an xontrib-cd that does this auto-replacement of cd to cd!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sucrecacao picture sucrecacao  路  5Comments

74th picture 74th  路  4Comments

CJ-Wright picture CJ-Wright  路  6Comments

OJFord picture OJFord  路  6Comments

battaglia01 picture battaglia01  路  3Comments