Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Improved command history in the Terminal UNIX
When working in the terminal, I use the history a lot to repeat common commands, or to slightly edit previous commands. However, the default key bindings are not very efficient. You can scroll up and down the history using the arrow keys, but what if you want the last 'renice' command? You then have to search the history.

What I've done is made it so that the arrow keys will search the history for commands that match what you have typed so far. Read the rest of the article if you'd like to see what changes I've made...

Let's assume I've run the following commands:
% mv FileA FileB
% cd /usr/local/etc
% ls -l
% cd ~
% ls -l
Now, let's say I want to change back to the etc directory. The fastest way to do this is type cd then hold the control key and hit "[", then hold the shift key and hit "P" to make it auto-complete to the last 'cd' command. However, this is a pain. Type the following into the terminal:
bindkey -k down history-search-forward
bindkey -k up history-search-backward
Now, I can just type cd (up-arrow key) to get the last 'cd' command. If you wish to just flip through the history, just use the arrow-keys before you type anything in. Repeatedly hitting the arrow keys will let you peruse through all matches in the history.

I put those two commands in the "environment.mine" file in the ~/Library/init/tcsh directory so that every terminal starts with these bindings.

[Editor's note: From the comments to a previously published tip, make sure you end your Terminal sessions with "exit" in order to keep a properly updated command history.]
  • Currently 3.40 / 5
  You rated: 3 / 5 (5 votes cast)

Improved command history in the Terminal | 4 comments | Create New Account
Click here to return to the 'Improved command history in the Terminal' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
command history searching
Authored by: Chris Jones on Jan 15, '02 09:29:16AM

The terminal window already has a standard method to search command history. By typing <control>r you enter the terminal's reverse search mode. As you type a command, the terminal shows you the most recent command that matches what you have typed so far.

However, from a UI perspective, I kind of prefer what you have done.

[ Reply to This | # ]
For all users
Authored by: Franco on Jan 15, '02 10:05:00AM

To have this hint available to *all* users, I modified the default key bindings in file


so that it now looks like this:

if ($?interactive) then
if ("${version}" =~ "tcsh"*) then
bindkey "^R" i-search-back
bindkey -k down history-search-forward
bindkey -k up history-search-backward

[ Reply to This | # ]
For all users
Authored by: houchin on Jan 15, '02 12:14:30PM

I'd put it into /usr/share/init/tcsh/environment.mine instead of rc.
There's a decent chance that /usr/share/init/tcsh/rc will get overwritten on an OS upgrade, as it's supposed to be the "standard" setup. The global environment.mine is yours to play with.

[ Reply to This | # ]
Another method
Authored by: Mithrandir on Jan 18, '02 07:07:17AM

You can also use the bang character !
like this:


Will give the last command match, hit tab to see the command or just hit return to execute it. You can't scroll through the history the way you did though.


[ Reply to This | # ]