Created
August 30, 2019 07:14
-
-
Save ylxdzsw/0be0e08ea12191a0e13d612734c24b0f to your computer and use it in GitHub Desktop.
For me, the shell history is mainly used as a completion dict when I press the up arrow. This script removes the lines that does not help serve this purpose
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
# For me, the history is mainly used as a completion dict when I press the up arrow. | |
# This script removes the lines that does not help serve this purpose | |
entries = readlines("$(homedir())/.zsh_history") | |
commands = Set(readlines(`zsh -ic 'print -rl -- ${(k)aliases} ${(k)functions} ${(k)builtins} ${(k)commands}'`)) | |
filter_but_keep_multiline(f, iter) = let skip = false | |
filter(iter) do line | |
if skip | |
skip = endswith(line, '\\') | |
return true | |
end | |
if endswith(line, '\\') | |
skip = true | |
return true | |
end | |
f(line) | |
end | |
end | |
set = Set() | |
entries = filter_but_keep_multiline(entries) do line | |
# 1. remove short (no more than 4 chars) lines | |
if length(line) < 20 | |
return false | |
end | |
# 2. remove non-existing commands | |
content = line[16:end] | |
if split(content)[1] ∉ commands | |
return false | |
end | |
# 3. remove duplications | |
if content in set | |
return false | |
else | |
push!(set, content) | |
return true | |
end | |
end | |
open("$(homedir())/.zsh_history", "w") do fout | |
for line in entries | |
println(fout, line) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment