Stop Arrowing Up So Much In Terminal; Search Instead! (Linux/Unix)
Problem
Do you ever NOT feel like re-writing a command that you wrote some N commands ago? I do!
Arrowing up and focusing until you reach what you want can be annoying for an efficient programmer. Although it usually only takes a couple of seconds, it still adds up. Plus, you don’t want to waste your brainpower on such a task (unless you like doing it).
One of the best pieces of advice I heard when I first started programming is:
“Always improve/automate tasks that you know you’re going to continuously repeat in the future”
So let’s do that. Let’s improve this situation.
Solution
Ctrl+r
This does a ‘reverse-i-search’ or ‘bck-i-search’, which finds the most recent closest match.
Ctrl+r
again to go to next match (further back in history).
It searches through an environment variable called $HISTFILE
. The number of commands that
this file remembers depends on your $HISTSIZE
.
I believe the default $HISTSIZE
in Unix/Linux is around 500. You can check yours by running:
$ echo $HISTSIZE
Alternative approach
$ history 0
0 echo 'example command 0'
1 echo 'example command 1'
2 echo 'example command 2'
3 echo 'example command 3'
...
This displays your history from 0 to $HISTSIZE
.
You can either scroll or Cmd+f
to find your command.
To execute the command, run:
$ !<command-number>
… where command-number
is the number displayed leftmost to your selected command.
Happy coding!
Like what you're seeing? Subscribe below to recieve notifications of new posts ⬇️
Promise not to send more than 1 email per month (not counting subscription confirmation email) 👨💻
Updated: 28 March, 2021
Created: 31 December, 2020