anki-code / xontrib-sh

Paste and run commands from bash, zsh, fish, tcsh in xonsh shell.
MIT License
54 stars 5 forks source link

Is there a way to make an alias that will be interpreted the same as manually entering this? #6

Closed meramsey closed 2 years ago

meramsey commented 2 years ago

For example the only way I can use nvm in xonsh is with

Doing this in terminal works fine

! [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" && nvm ls

If i setup an alias to make it easier to do that it fails

aliases['nvm'] = '! [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" && nvm'
# or like
aliases['nvm'] = '!bash [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" && nvm'

I get an error like this

``` @ nvm Traceback (Most recent call last): 523 /home/mike/.pyenv/versions/3.9.6/lib/python3.9/site-packages/xonsh/parsers/base.py parse --> tree = self.parser.parse(input=s, lexer=self.lexer, debug=debug_level) 335 /home/mike/.pyenv/versions/3.9.6/lib/python3.9/site-packages/xonsh/ply/ply/yacc.py parse --> return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc) 1203 /home/mike/.pyenv/versions/3.9.6/lib/python3.9/site-packages/xonsh/ply/ply/yacc.py parseopt_notrack --> tok = call_errorfunc(self.errorfunc, errtoken, self) 194 /home/mike/.pyenv/versions/3.9.6/lib/python3.9/site-packages/xonsh/ply/ply/yacc.py call_errorfunc --> r = errorfunc(token) 3459 /home/mike/.pyenv/versions/3.9.6/lib/python3.9/site-packages/xonsh/parsers/base.py p_error --> self._parse_error(msg, self.currloc(lineno=p.lineno, column=p.lexpos)) 650 /home/mike/.pyenv/versions/3.9.6/lib/python3.9/site-packages/xonsh/parsers/base.py _parse_error --> raise_parse_error(msg, loc, self.xonsh_code, self.lines) 238 /home/mike/.pyenv/versions/3.9.6/lib/python3.9/site-packages/xonsh/parsers/base.py raise_parse_error --> raise err SyntaxError: :1:0: ('code: !',) !bash [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" && nvm ^ ~ [✖ ERROR] at 18:40:48 @ ```

If i try to put all but the ! in the alias and then use ! nvm which is an alias for [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" && nvm it also doesn't like that I think as its being expanded in bash and skipping the xontrib loading.

Seems like I should be able to create an alias that calls it through the sh thing like doing it manually would, but I probably missed that option.

Any tips would be super appreciated. This plugin is what really ensured I didn't give up on xonsh. The convenience of being able to prefix is amazing. I'm just curious if there is a way to make an alias or callable that mimics me entering the ! at the beginning before its executed.

anki-code commented 2 years ago

hi @meramsey! I'm not the user of nvm but I think the ExecAlias with $args will work:

aliases['nvm'] = """bash -c @(f'[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" && nvm {" ".join($args)}')"""

nvm
# Node Version Manager (v0.39.1)
# ...

nvm --version
# 0.39.1

You can wrap any bash commands this way.

meramsey commented 2 years ago

wow you sir are genious and this works perfectly. This is what I love about xonsh is that possibilities are endless.

Thanks for taking the time to share with me. learned something new here.