I rely on my history file for previous commands, especially ones that are long with many parameters. Once I find the command and its line number(nn
) I use !nn
to run it. I prefer to see the full output from the search rather than doing an in-line reverse search.
To do this I search through it with history | grep some_text
However I tend to get a lot of output due to duplicate entries (differing only in their line numbers). As my history grows this gets worse over time.
How could I get a list of just the unique commands that I could then use with the !
recall command operator?
I tried history | uniq | grep some_text but that doesn't work because of the line numbers.
I can do history | cut -b8- | uniq
and that does show just the commands.
However when I add sort
I get $ history | sort
sort: string comparison failed: Illegal byte sequence
sort: Set LC_ALL='C' to work around the problem.
sort: The strings compared were
5359 \253' and 5360 x'.
Also the line numbers are gone which I would actually want for the !line-number
command
Ideally I would like this to happen each time I do my history | grep
- which I do so much I have an alias hg='history | grep '
so I could add any solution to that.
Less ideal, but still of interest would be the ability to run an ad-hoc program which would actually make the history file entries unique (again, dealing with the line numbers issue) so that only the most recent history command of any given command is retained). presumably by deleting the other lines. But would deleting lines in the history file this way mess up the way the file is used by the system?