Terminal Command History Recall

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
Minterator

Terminal Command History Recall

Post by Minterator »

The most useful feature in Terminal. Pressing Up/Down recalls previous/next command or the most recent command that matches a partial command line.

In ~/.inputrc I have:

Code: Select all

"\e[A": history-search-backward
"\e[B": history-search-forward
In LM13 it works as expected, it simply stops after it recalls the first or last command.

In LM17 It works, except after the first or last command it trashes whatever you've typed and replaces it with a [

Is there a fix for this?
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
austin.texas
Level 20
Level 20
Posts: 12003
Joined: Tue Nov 17, 2009 3:57 pm
Location: at /home

Re: Terminal Command History Recall

Post by austin.texas »

I can't help with ~/.inputrc - but I just grep the history.

Code: Select all

user@skynet1 ~ $ grep apt-get .bash_history
sudo apt-get install grml-rescueboot --dry-run
sudo apt-get install pastebinit
man apt-get
user@skynet1 ~ $ 
The default history search is useful sometimes. Press Ctrl+r and enter the search term, then Ctrl+r for the next oldest, then Ctrl+r again for the next oldest... etc.
Mint 18.2 Cinnamon, Quad core AMD A8-3870 with Radeon HD Graphics 6550D, 8GB DDR3, Ralink RT2561/RT61 802.11g PCI
Linux Linx 2018
Habitual

Re: Terminal Command History Recall

Post by Habitual »

iatest=$(expr index "$-" i)
if [[ $iatest > 0 ]]; then bind '"\e[A": history-search-backward'; fi
if [[ $iatest > 0 ]]; then bind '"\e[B": history-search-forward'; fi
in ~/.bashrc works for me, and has for years.
lmuserx4849

Re: Terminal Command History Recall

Post by lmuserx4849 »

Minterator wrote:The most useful feature in Terminal. Pressing Up/Down recalls previous/next command or the most recent command that matches a partial command line.

In ~/.inputrc I have:

Code: Select all

"\e[A": history-search-backward
"\e[B": history-search-forward
In LM13 it works as expected, it simply stops after it recalls the first or last command.

In LM17 It works, except after the first or last command it trashes whatever you've typed and replaces it with a [

Is there a fix for this?
I'm on 17.3. I do not have a ~/.inputrc. I do have a /etc/inputrc. I can move up and down the history file by using the up and down arrow keys.
Maybe to narrow it down, first rename the local version, logoff/on and try bash history. There is a command to load (bind -f ~/.inputrc), but be sure and logoff/on. Also check bind -p|grep history and check the key mapping for next-history and previous-history.

Bash Reference - Sample inputrc
Minterator

Re: Terminal Command History Recall

Post by Minterator »

lmuserx4849 wrote:
Minterator wrote:The most useful feature in Terminal. Pressing Up/Down recalls previous/next command or the most recent command that matches a partial command line.

In ~/.inputrc I have:

Code: Select all

"\e[A": history-search-backward
"\e[B": history-search-forward
In LM13 it works as expected, it simply stops after it recalls the first or last command.

In LM17 It works, except after the first or last command it trashes whatever you've typed and replaces it with a [

Is there a fix for this?
I'm on 17.3. I do not have a ~/.inputrc. I do have a /etc/inputrc. I can move up and down the history file by using the up and down arrow keys.
Maybe to narrow it down, first rename the local version, logoff/on and try bash history. There is a command to load (bind -f ~/.inputrc), but be sure and logoff/on. Also check bind -p|grep history and check the key mapping for next-history and previous-history.

Bash Reference - Sample inputrc
Thank you. I wasn't logging off because changes in ~/.inputrc take effect every time you open a new Terminal.

You're saying that local ~/.inputrc may not be needed in 17.3? Does your up/down complete a partial line from history?

I also have Tab/Shift+Tab cycling through filenames, is that default on you system?

Code: Select all

##### Filename Completion #####
##set completion-ignore-case on	# ignore case on completion. -- Doesn't work here, in ~/.bashrc: bind 'set completion-ignore-case on'
##set mark-directories on		# append slash to dirs.      -- Doesn't work here, in ~/.bashrc: bind 'set mark-directories on'
set visible-stats on			# ls -F completion
##"\C-i": menu-complete			# Tab cycles through filenames instead of listing. 
TAB: menu-complete
"\e[Z": menu-complete-backward	# Reverse with Shift+Tab
set show-all-if-ambiguous on	# list possible completions instead of ringing bell
set show-all-if-unmodified on
set mark-symlinked-directories on
#set match-hidden-files off		# same as typing * before Tab
set bell-style none				# no bell on tab-completion
set skip-completed-text on
lmuserx4849

Re: Terminal Command History Recall

Post by lmuserx4849 »

You're saying that local ~/.inputrc may not be needed in 17.3? Does your up/down complete a partial line from history?

I also have Tab/Shift+Tab cycling through filenames, is that default on you system?
I'm going to say yes and no. I've only known Linux Mint since 17, so I can't say how it worked before that version.

As I use the up/down arrows, they show a complete line of history, as does ctrl-p, and the arrows on the numeric keypad. Their existence is confirmed by the command:

Code: Select all

===> bind -p|grep previous-history
"\C-p": previous-history
"\eOA": previous-history
"\e[A": previous-history
I only use tab for completion... for files I get a list.
complete (TAB)
Attempt to perform completion on the text before point. The actual completion performed is application-specific. Bash attempts completion treating the text as a variable (if the text begins with ‘$’), username (if the text begins with ‘~’), hostname (if the text begins with ‘@’), or command (including aliases and functions) in turn. If none of these produces a match, filename completion is attempted.
In the bash reference, "menu-complete" is shown with no meta keys (menu-complete ()). I didn't see it in the output of bind -p either. I tried it and it wasn't until I added the two lines below in ./inputrc that I was able to cycle through files instead of just getting a list.

Code: Select all

"\t": menu-complete
"\e[Z": menu-complete-backward
Minterator

Re: Terminal Command History Recall

Post by Minterator »

LM13 had none of this functionality, I simply wrote my own ~/.inputrc and ~/.bashrc trying to emulate Windows that I was used to, and JP Software's "Take Command Console", the command-line gurus. It worked fine, and it still does in LM17 except for the problem in my OP.

Up/down command History recall doesn't just bring up previous commands. If I type "gksudo pluma /" and press up/down, it fills-in all previous commands like "gksudo pluma /etc/fstab". If I type "sudo", up/down will bring up all previous commands starting with sudo. Except now if there aren't any, it deletes whatever I've typed and replaces it with [. It would be nice if it could recall from substrings as well, like typing "grep" would bring up all commands that include "grep".

Also Esc clears the line, like in Windows, but I can't get Ctrl+Home /End to work:

Code: Select all

ESCAPE: kill-whole-line			# Escape clears line

# Delete from cursor to beginning/end of line
##"\e[1;5H": backward-kill-line	# Ctrl+Home -- Doesn't work
##"\e[1;5F": kill-line			# Ctrl+End  -- Doesn't work

"\e[1;6D": backward-kill-line	# Ctrl+Shift+Left
"\e[1;6C": kill-line			# Ctrl+Shift+Right

"\e[1;2D": backward-kill-word	# Shift+Left
"\e[1;2C": kill-word			# Shift+Right
If you have more than one Terminal, commands from all Terminals are shared & preserved instead of overwritten/lost. So commands you type in one Terminal are immediately available in all other Terminals. Here's my ~/.bashrc

Code: Select all

# ~/.bashrc

export HISTCONTROL=ignoredups:erasedups
shopt -s histappend

export HISTSIZE=10000
export HISTFILESIZE=10000

bind 'set completion-ignore-case on'	# ignore case on completion
bind 'set mark-directories on'			# append slash to dirs

# Save and reload the history after each command finishes
PROMPT_COMMAND="history -n; history -w; history -c; history -r; $PROMPT_COMMAND"

export EDITOR=pluma

Like you said, the problem may be due to newer system files, like /etc/inputrc, conflicting with mine. I'm gonna try fix it and post again.


PS: It looks like somebody moved this thread to the wrong forum. This topic is about an Application, Terminal and its options, and has nothing to do with bash scripting.
lmuserx4849

Re: Terminal Command History Recall

Post by lmuserx4849 »

Minterator wrote:...
Like you said, the problem may be due to newer system files, like /etc/inputrc, conflicting with mine. I'm gonna try fix it and post again.

PS: It looks like somebody moved this thread to the wrong forum. This topic is about an Application, Terminal and its options, and has nothing to do with bash scripting.
That sounds like a plan. I'd rename the ./.inputrc and add back as needed, one at a time.

You might be interested in these for bash history (.bashrc):

Code: Select all

shopt -s histappend                            # history file appended rather than overwritten on exit
shopt -s histverify                               # history cmds reloaded for edit, verses immediatedly parsed
shopt -s histreedit                               # failed history substitutions are reloaded for edit
PROMPT_COMMAND="history -a; history -n; $PROMPT_COMMAND"
There are some tradeoffs trying to maintain bash history between multiple consoles.

I just use the emacs mode.

I've had a thread moved too. I'm not sure what the policy is... notification and that sort of thing.
Minterator

Re: Terminal Command History Recall

Post by Minterator »

I've experimented to no avail. Everything works as expected until you get to the beginning or end of history search.

e.g. I've previously run some scripts in ~/Documents so if I type ~/Doc or ~/Documents and press Up or Down, command history recall works perfectly and fetches previous commands with arguments. But if it can't find any matching history or reaches the beginning or end, it simply trashes whatever is there and replaces it with [. So I'm thinking that it simply doesn't work in 17.3 MATE. Does this scenario work in your Terminal?

Also from Bash manual, history-substr-search-backward and history-substr-search-forward should work even better than history-search-backward but it doesn't work at all. Lines in ~/.inputrc are simply ignored.
Locked

Return to “Scripts & Bash”