How I put my bash history in xonsh

96 views
Skip to first unread message

Nick Demou

unread,
Jan 5, 2018, 3:03:36 AM1/5/18
to xonsh
Hi all, when I first tried to switch to xonsh I found out that I was missing my bash history A LOT. grep foo ~/.bash_history was kind of OK but now as good as ctrl-R.

So I found a way to feed almost all of my bash history to xonsh (a few thousand lines of bash hist). I'm documenting it here for anybody else that may be interested (not sure if this is the best place but it's better than my hard disk)

Create a plain text history file with one command per line

e.g. history > /tmp/commands_history


Adjust config.json to switch xonsh PROMPT to just ###

.xonshrc will not be executed so you need to add/change this to .config/xonsh/config.json :

'env': {'PROMPT': '###'}

Create an expect script which will feed commands to xonsh

$ cat /tmp/feed_hist_to_xonsh
#!/usr/bin/expect

# read the commands to run

# CUSTOMIZE THE PATH at the line below

set f [open "/tmp/commands_history"]
set commands [split [read $f] "\n"]
close $f

spawn xonsh
# Send commands giving an extra enter at the end (in case xonsh expects more input),
# and if instead of the ### prompt you get .. (xonsh still waits for more input) send ctrl-C
# ctrl-c will cancel this specific command which doesn't work as expected in xonsh
foreach cmd $commands {

expect {
"###" { send "$cmd\r\r" }
".." { send \x03 }
}
}

hack xonsh code to make it ignore all commands

You need to add a return as the first statement of the run_compiled_code function in /usr/local/lib/python3.5/dist-packages/xonsh/amalgam.py
(Note that you must have another shell available to execute commands after this change)

def run_compiled_code(code, glb, loc, mode):

"""
Helper to run code in a given mode and context
"""
#make xonsh ignore all commands
     return
if code is None:
return
if mode in {'exec', 'single'}:

run your expect script

expect /tmp/feed_hist_to_xonsh


This was crashing every some thousand commands and I had to delete the already processed commands and then rerun expect

Hugo Wang

unread,
Jan 5, 2018, 3:31:43 AM1/5/18
to Nick Demou, xonsh
Or, you can use python script to import history items into xonsh, like this:

# not full-tested yet
from xonsh.history.main import construct_history

hist = construct_history(gc=False)

with open('./.bash_history') as f:
  for line in f:
    hist.append({'inp': line.strip(), 'rtn': 0})



--
You received this message because you are subscribed to the Google Groups "xonsh" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xonsh+unsubscribe@googlegroups.com.
To post to this group, send email to xo...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/xonsh/4c4c1799-80ad-4f22-ac63-4efe2f07daad%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nick Demou

unread,
Jan 5, 2018, 4:29:25 AM1/5/18
to xonsh
Thanks Hugo. That's *much* better and I think it deserves a mention in the docs (I think it should be added in http://xon.sh/bash_to_xsh.html after a re-titling to something like "Switching from bash to xonsh"). I've spend many years with bash then 3 or 4 years with fish and I'm now enjoying xonsh quite a lot but in both switches from one shell to the other bringing my history along was very important for me.
To unsubscribe from this group and stop receiving emails from it, send an email to xonsh+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages