Last active
April 20, 2020 19:21
-
-
Save mwhite/7509467 to your computer and use it in GitHub Desktop.
Bash history with bash and git aliases expanded
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Outputs history with bash and git aliases expanded. | |
""" | |
from __future__ import print_function | |
import re | |
from subprocess import check_output | |
BASH_ALIASES = {} | |
for line in check_output('bash -i -c "alias -p"', shell=True).split('\n'): | |
if not line.strip(): | |
continue | |
match = re.match(r"^alias (.+?)='(.+?)'\n*$", line) | |
BASH_ALIASES[match.group(1)] = match.group(2) | |
GIT_ALIASES = {} | |
for line in check_output('git config --get-regexp alias*', shell=True).split('\n'): | |
if not line.strip(): | |
continue | |
match = re.match(r"^alias\.(.+?) (.+)$", line) | |
GIT_ALIASES[match.group(1)] = match.group(2) | |
def expand(cmd): | |
try: | |
number, cmd = cmd.strip().split(' ', 1) | |
cmd = cmd.strip() | |
except ValueError: | |
# empty line | |
return cmd | |
for alias, expansion in BASH_ALIASES.items(): | |
cmd = re.sub(r"^" + re.escape(alias) + '(\s|$)', expansion + ' ', cmd) | |
for alias, expansion in GIT_ALIASES.items(): | |
cmd = re.sub(r"^git " + re.escape(alias) + "(\s|$)", "git %s " % expansion, cmd) | |
return " %s %s" % (number, cmd) | |
if __name__ == "__main__": | |
for line in check_output('bash -i -c "history -r; history"', shell=True).split('\n'): | |
print(expand(line)) | |
if your shell prints anything on startup (e.g. in .bashrc or .profile), this won't work for you as-is – add the following after line 13 (match = re.match(r"^alias (.+?)='(.+?)'\n*$", line)
) to fix:
if match is None: continue
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
aww, I wish this worked for my fish functions as well :(