Fixed history commandline

there are certain commands i use it a lot daily, but when i use command-line to do others works(my be many tries and many commands to install somthing)that make the daily commands go back in the history ,and i need to click on the arrows a lot to reach them.
my question is can i make fixed history with certain list of commands?
i know i can reach to the history by type the first letter of the command in fish and other terminal, but i prefer bash

Not what I know of. You could make a text file with the most used commands, so you can copy/paste.

Not really exactly what you are asking for, but I also have to run a few specific commands every day, as I depend on software that is on a slightly broken install and accessing it from chroot is very useful for me.

So what I did to make the process of setting it up way faster each time I turn on the system is create a text file, write the commands I use in there (each one followed by "&&", so the terminal knows that it's supposed to run the next one when it finishes running the first one), change the extension of the file from ".txt" to ".sh", right-click the file, properties, permissions, and check the box that says "allow to run as program", then open it by double-left-click like you would open any file, and then click on the "open in the terminal" option.

In my case, that is how my .sh file for enabling chroot looks:

So if you always run the exact same commands, this could be an easy solution to automate things: just type the commands in a .sh file with a text editor and adding "&&" between them and open the file every time you need to run the commands.

Though, it also depends in your use case. I hope this option is meets your needs.

If you press Ctrl+R while at the prompt, you can run a reverse search throughout your entire history. You can start typing something like "ansible" and it would populate your last command that starts with, or includes, this word. Press Ctrl+R again, and the next match will appear, and so on.
For example I have the following command that I use frequently, especially when working inside virtual machines and the like:

ansible-playbook ./playbooks/local.yml -K --vault-password-file ~/.ansible/local_vault

When I need to come back to it I just press Ctrl+R and start typing "ansible-p" and it's already there for me.

You can increase the history size (number of commands remembered by setting the HISTSIZE variable inside your .bashrc file. By default it's 500, but you can set it to a higher number; use a negative value to make it limitless. Not that I would recommend that unless you are prepared to clear it every now and then.

But if you really find yourself typing the exact same command, write a function and put it in your PATH or .bashrc file so that you can use as if it were any other command. This is probably much easier.

2 Likes

i will use fish to the frequently commands, and i will use bash for other commands. each one has it's
own history

In my experience things like this can cause a lot of unnecessary friction in my workflow. This just seems weird to me, relying on two tools that largely do the same thing and work on the same tasks. Personally, I prefer to stick with Bash because of the compatibility with other machines. But if I were already using another shell like Fish, then why not keep using those extra features?

Of course, you can – and should – use whatever works best for you. However, I think this is a good opportunity to iron out those small inefficiencies that can cause more issues on the long run.

If you could give a more concrete example of what is it you are trying to work around, maybe we can give better recommendations? For example:

In Bash, you can type !ansible and this would run the last command found in history that starts with "ansible" (or whatever word you use instead).

In any case, if you're going to be using multiple shells I assume you'd be running multiple sessions at once. If so, I would recommend adding these two lines in your .bashrc:

shopt -s histappend
PROMPT_COMMAND="history -a; $PROMPT_COMMAND"

The first option makes it so that your history file doesn't get overwritten everytime you close the terminal. The second option appends the command immediately to the history, instead of waiting to the end of the session. This is quite useful when you have multiple sessions running side by side so that you can read each other's command history.

1 Like