Skip to content

Instantly share code, notes, and snippets.

@chew-z
Created February 14, 2017 09:52
Show Gist options
  • Save chew-z/ed32432e8fb106898fba4560d42d112d to your computer and use it in GitHub Desktop.
Save chew-z/ed32432e8fb106898fba4560d42d112d to your computer and use it in GitHub Desktop.
Removes silt from .zsh_history file
#
brew\sinstall.*
brew\sinfo.*
brew\sprune.*
brew\sdoctor.*
brew\slink.*
brew\sunlink.*
brew\suntap\s
.*/Dropbox.*
which.*
ptr\s.*
ping.*
dig.*
rm\s.*
say\s.*
mpv\s.*
python3\scolortrans.py.*
.*/private/var/folders/zz/.*
# security\sadd-.*
#!/usr/bin/env python3
#
# clean up zsh history
# takes .zsh_history file and removes lines according to set of regexes
#
import re
import os
import argparse
import shutil
def getArgs(argv=None):
# Command line arguments.
parser = argparse.ArgumentParser(description =
'Helps removing stilt from shell history',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-p', '--pattern',
help='commandline regex pattern. If present --file is ignored')
parser.add_argument('-f', '--file',
default='~/.config/trim_zsh_history.conf',
help='Location of config file containing patterns to remove')
parser.add_argument('-o', '--overwrite', default=False,
action="store_true",
help='Overwrite hidden .zsh_history file in $HOME directory. Always creates hidden backup file.\
If not set result of operations is found in zsh_history_working_copy')
parser.add_argument('-v', '--verbose', default=False,
action="store_true")
return parser.parse_args(argv)
args = getArgs()
verbose = args.verbose
overwrite = args.overwrite
config_file = os.path.expanduser(args.file)
pattern = args.pattern
hist_line_start = '^:\s\d{10}:\d;'
hist_line_end = '$'
comment_pattern = re.compile('^#.*$')
# go to user $HOME folder
os.chdir(os.path.expanduser("~"))
# backup .zsh_history
shutil.copy('.zsh_history', '.zsh_history.bak')
# 1) flush working copy of zsh_history if exists
try:
working_copy = open('zsh_history_working_copy', 'r')
os.unlink('zsh_history_working_copy') # touch zsh_history_working_copy first if it doesn't exist
working_copy = open('zsh_history_working_copy', 'w')
except FileNotFoundError:
working_copy = open('zsh_history_working_copy', 'w')
# 2) read config file (one regex per line) OR use commandline input
if pattern is None:
with open(config_file, 'r') as cf:
config_lines = cf.readlines()
else:
config_lines = list()
config_lines.append(pattern)
if verbose:
for x in config_lines:
print(x)
# 3) read zsh history and check matching patterns. if none write line to working copy
with open('.zsh_history', 'r', encoding='utf-8') as zsh_history:
cntr = 0
for hist_line in zsh_history.readlines():
Match = False
for config_line in config_lines:
if re.match(comment_pattern, config_line):
pass
else:
if verbose:
print(config_line)
hist_line_pattern = r'' + hist_line_start + config_line + hist_line_end
match = re.match(hist_line_pattern, hist_line)
if match:
if verbose:
print(hist_line)
Match = True
cntr += 1
if not Match:
working_copy.write(hist_line)
print('# ', cntr, ' lines removed from history')
# 4) clean up, overwrite .zsh_history
working_copy.close()
if overwrite:
os.chdir(os.path.expanduser("~"))
shutil.move('.zsh_history', '.zsh_history.bak')
shutil.move('zsh_history_working_copy', '.zsh_history')
print('\nTrimed history has been saved to ~/.zsh_history file.')
else:
print('\nTrimed history has been saved to ~/zsh_history_working_copy.\nNo changes had been made to ~/.zsh_history.\nUse --overwrite option if you want to save changes to ~/.zsh_history')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment