Linux Tutorials

Shortcuts For Navigating Terminal History

shortcuts for navigating command line history

Today I’ll highlight some tips and tricks on how to get more proficient on a terminal program. You must have at least once typed in a long command on your terminal program and then forget it the next day. Navigating terminal history commands come in handy in such a scenario.

However, there is more than one way to navigate them. Like Linux gurus out there, you might prefer one over the other based on how you use your keyboard regularly. Read on below to learn how to navigate Linux terminal history on your favorite terminal program.

Browsing Terminal History

The arrow keys

The most common method is using the arrow keys on your keyboard. Use the Up arrow key to browse your recent commands in sequence, then press the Enter key when you find your preferred command to execute it.

Down arrow key is mostly useful for undoing the Up arrow key command. Suppose you pressed Up key too many times and crossed the command that you were looking for, Down arrow key is used for browsing back the command list. So, up and down is synonymous to moving forward and backward hence is the quickest way to navigate through terminal history.

​Unfortunately, for most laptop users, the arrow keys might prove inconvenient (like my HP notebook does to me) because of its small Up/Down arrow keys.

keyboard arrow keys

In which case, we have another trick up in our sleeve; an alternate way to browse up and down without using the arrow keys. And that is…  

The Ctrl+p and Ctrl+n keys

Ctrl+p is abbreviated like “previous” which does exactly what the Up arrow key does; browsing the previous recent commands in sequence. While Ctrl+n is abbreviated like “next” and performs the same function as the Down arrow key.

​You might fuss that you have to press two keys this time to move up and down, but trust me, this is much more efficient if you don’t want to leave your hands away from the keyboard. Up/Down arrow keys force you to break your finger alignment on the home row keys if you are someone who loves touch typing.

The history command

Okay, so you’ve just gotten familiar with the navigating terminal history commands in sequence; one by one, using either of the two above methods. But what if you have performed so many tasks on your terminal program. Are you willing to keep browsing the commands one by one until you find that one command you’ve been looking for? That would be tedious right?

This brings up the need to use “history” command, which prints out all your recent commands at one go. The commands are prefixed with some number that acts like indexes to those commands. So you’ll notice there are two columns displayed as output to the history command.

command line history

That number will be used to execute the desired command. But how do we do that? Search the command from the terminal output and memorize that number prefixed to it. Then use the below syntax to execute your preferred command on the prompt:

!<index number>

For instance, I want to execute a long command like “sudo flatpak install flathub com.jetbrains.IntelliJ-IDEA-Community” and I see that the mentioned command is indexed at 1035, I would have typed in like the one shown below and pressed the Enter key:

!1035

History command is most useful if we don’t want to browse the previous recent commands in sequential order.

The reverse search

linux command line reverse search

The reverse search is another cool trick for navigating previous recent commands. It’s like a search function we normally use on most word processors and text editors. However, instead of Ctrl+f we use Ctrl+r to perform a reverse search. This might seem quite jarring and out-of-regular-habit-task but don’t worry, with practice, you’ll get the hang of it 🙂 Press Ctrl+r and start typing that command you were intending to execute and like magic the terminal prompt will start printing out commands automatically. If the command is what you are looking for then press the Enter key to execute it.

​If the command is not what you are looking for which most likely will be the case if your command contains common starting words and is way behind the previous recent commands because it was used some weeks or months ago. Just keep typing in that command letter by letter and the reverse search program will get more precise till you get the desired command.

Clear Terminal History

So above were the ways to access terminal commands history, I am also mentioning a way to clear the history.

history -c

Conclusion

With the use of various history command tools on Linux, it’s much convenient and easier to use a shell/terminal prompt nowadays. I’ve listed some of the popular ways to navigate terminal history and by now I’m sure you’d have already found your liking from the above various methods. Hope this article has been helpful in extending your terminal skills 😉 If I’ve missed out some cool methods please do let me know your opinions in the comment section below and I’ll be sure to read them.



LinuxAndUbuntu hosting is sponsored by massiveGRID

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This website uses cookies to improve your experience. We'll assume you're ok with this. Thank you! Ok Read More