Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
meramsey opened this issue Feb 19, 2022 · 2 comments
Labels
question Further information is requested

Comments

@meramsey
Copy link

meramsey commented Feb 19, 2022

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: <exec-alias:nvm>: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
Copy link
Owner

anki-code commented Feb 19, 2022

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.

@anki-code anki-code added the question Further information is requested label Feb 19, 2022
@meramsey
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants