oh-my-bash icon indicating copy to clipboard operation
oh-my-bash copied to clipboard

Anyone interested in a new concept for history handling ?

Open Markus-N opened this issue 2 years ago • 3 comments

Hi, I recently stumbled upon oh-my-bash, and I love it :-)

I'm using my history a bit different. At first glance, it may look very unusual or even crazy, as it is a total paradigm shift:
Instead of saving everything, I have a manually curated history, containing only the commands that I know for sure I will need again later and that are correct. I'm also not a bash expert and therefore I often need a few tries until I get it right, especially when sed and/or awk are part of the command.:-)

My curated history is achieved by:

  • Disabling history saving on bash exit.
  • Editing ~/.bash_history manually when I want to add something.
    Yes, this is extra work and you have to get used to it (and not to forget it), but IMO it's worth it.

And over time, I have added some more goodies:

  • Project-specific history that is automatically found and loaded.
    Of course, this is only useful for shells that open directly in the project directory, as part of the desktop session or when you open a new terminal from an existing one.
  • A bash function I named history_edit.
    You can manipulate / rearrange the local history of the current shell, like removing the failed experimental commands, or even share entries between shells if you do history_edit in all of them. This needs the history writing to be disabled, otherwise it will screw up the history file.
  • I also added a blank at the start of every line in my history files.
    You still can recall them via <cursor up> or number (e.g. !23) or find them via Ctrl-R, but when you execute them, the recall will not be appended to your local history - unless you remove the leading blank which is useful when you modify the command and want to remember it locally.
    This is used as a very special feature for repetitive tasks in conjunction with history_edit: I rearrange my local history to have the needed commands at the bottom, then I can just do <cursor up><enter>, check the output, then <cursor up><cursor up><enter> and so on, and the order of the commands never changes :-)
  • I even divided my history files into sections using comments - yes, this works. :-)
    This can be done independently of the other things describe here, I just wanted to add it as inspiration. :-)

Currently, all that stuff is in my custom folder. I could convert this to an oh-my-bash library and add it to the project, but I'll do the work only if more than 1 or 2 people are interested in using this too. :-)

I'm also open for ideas to extend this concept even further.

Markus-N avatar Jul 14 '21 19:07 Markus-N

Sorry, but except for the cross-shell history, looks like all the points you mentioned can be solved just by using "tags" on the commands.

Example docker run -P logstash #testing orgit clone my-awesome-repo #repo`

Then just search for the "tags" in your history.

jvitorinoj avatar Jul 29 '21 01:07 jvitorinoj

Not exactly. :-)

history_edit allows you to edit the whole history, i.e. remove experimental or failed commands completely, rearrange the order of commands for today's specific tasks, etc... without immediately executing anything afterwards. I could not find a way to prevent fc -e from executing whatever lines are in the editor when saving.

But the tags are a good idea anyway, I'll use that from now on. Thanks a lot for this inspiration.

For the automatically loaded project-specific history, I also have no idea how to achieve this without something in .bashrc (or scripts sourced by it). That's why I offered to create a library module for oh-my-bash.

Markus-N avatar Jul 30 '21 09:07 Markus-N

Does it need to be implemented as a module of oh-my-bash? What if someone wants to use your history plugin but doesn't want to use oh-my-bash? I think your history management configuration can be a separate plugin project. Actually, this oh-my-bash is just a rewrite of a certain version of oh-my-zsh for Bash (plus some additions since then). If your history management script is useful, maybe you can also consider implementing it in a way compatible with other shells, in which case it is again useful to make it a separate project.

Currently, all that stuff is in my custom folder. I could convert this to an oh-my-bash library and add it to the project, but I'll do the work only if more than 1 or 2 people are interested in using this too. :-)

I have been once thinking of implementing a function similar to your history_edit that enables editing the command history in TUI, so I'm actually interested in your implementation but not sure if it is really useful for me by just reading your explanation. Maybe you can first expose the working script in a separate project with README containing sufficient examples before tailoring it to make it suited for a module of oh-my-bash.

  • A bash function I named history_edit. You can manipulate / rearrange the local history of the current shell, like removing the failed experimental commands, or even share entries between shells if you do history_edit in all of them. This needs the history writing to be disabled, otherwise it will screw up the history file.

What does "This needs the history writing to be disabled, otherwise it will screw up the history file." mean in detail? Doesn't it solved by just setting shopt -s histappend?

  • Project-specific history that is automatically found and loaded. Of course, this is only useful for shells that open directly in the project directory, as part of the desktop session or when you open a new terminal from an existing one.

For the automatically loaded project-specific history, I also have no idea how to achieve this without something in .bashrc (or scripts sourced by it). That's why I offered to create a library module for oh-my-bash.

Maybe you could automatically load/unload the project-specific history based on the current working directory. One might check the current working directory $PWD in PROMPT_COMMAND to automatically do the work on the change of the directory. The project-specific history would be activated when it finds a special file, e.g., .project_bash_history, in the current directory or one of the nearest parent/ancestor directories.

akinomyoga avatar Jul 30 '21 10:07 akinomyoga