CTRL-R: Show timestamp and syntax highlighted command #3936
+39
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Close #1049
Close #3836
This is a proof-of-concept implementation of enhanced CTRL-R binding for bash.
Screenshot
Technicals
HISTTIMEFORMAT performance
For some reason,
$HISTTIMEFORMAT
makes pipinghistory
extremely slow. Strangely, there is no slowdown when redirecting the output to another file.So we can work around this performance problem by writing it to a temp file and then reading it. But...
Syntax-highlighting of each command
We need to syntax-highlight each command, however, it's not viable to execute an external process such as
bat
for each command.It's much faster to pass the whole file to
bat
at once,bat --style=plain --no-pager --language=bash ~/.bash_history
However, syntax-highlighting quickly breaks down due to malformed commands in history.
Complexity of the code
The Perl and Awk scripts for de-duplicating the list and handling multi-line commands are already quite complex and not easy to read and maintain. Adding timestamp and syntax-highlighting support to them would bring too much complexity.
Solution
I used a Ruby script to implement the enhanced CTRL-R binding. rouge gem was used for syntax-highlighting. The code isn't short, but I'd argue that it's still more readable.
Drawbacks
rouge
gem required for syntax-highlighting.Alternatives
TODO