7

In bash and zsh I used to write:

alias nb='cd /home/lincoln/Dropbox/nupis/ && jupyter notebook'

But in xonsh this returns an error saying that the command was not found. The tutorial's section on aliases says that I should do something like:

aliases['g'] = 'git status -sb' 

I could make this work in the case of only one command, but when I try the two commands of my bash example, it complains that I am giving too many inputs to cd.

Note: I know I could import the alias from the other shells, but I am interested in learning to do this in xonsh.

das-g
  • 9,718
  • 4
  • 38
  • 80
lincolnfrias
  • 1,983
  • 4
  • 19
  • 29

3 Answers3

9

@lincolnfrias, xonsh does not yet have support for string aliases that have multiple commands. This is a bug / deficiency that will hopefully be addressed soon. Until then, though, you can use a function alias for this behaviour.

def _nb(args, stdin=None):
    cd /home/lincoln/Dropbox/nupis/ && jupyter notebook

aliases['nb'] = _nb

Or if you really wanted to do this in one line:

aliases['nb'] = lambda a, s: ![cd /home/lincoln/Dropbox/nupis/] and ![jupyter notebook]
Anthony Scopatz
  • 3,265
  • 2
  • 15
  • 14
0

Separating commands with ; works fine, as in:

aliases['gp'] = 'git add -A :/; git commit -m asdf;git push;'

vishvAs vAsuki
  • 2,421
  • 2
  • 18
  • 19
0

Aliases which are a single string are evaluated as though they were typed, so you can chain commands using ;, &&, etc.

You can access the arguments passed to the alias as $args in a Python context, i.e. @($args). Note the dollar sign is inside the parenthesis!

For example:

robmee01@C02F2508MD6R:~
@ aliases['nb'] = 'echo cd /home/lincoln/Dropbox/nupis/ && echo jupyter notebook @($args)'

Sample usage:

user@host:~ @ nb --option1 --option2
cd /home/lincoln/Dropbox/nupis/
jupyter notebook --option1 --option2

The guide I've found most useful: anki-code/xonsh-cheatsheet: Cheat sheet for xonsh shell with copy-pastable examples. The best doc for the new users.

RobM
  • 8,373
  • 3
  • 45
  • 37