How do I change bash history completion to complete what's already on the line?
Probably something like
# ~/.inputrc
"\e[A": history-search-backward
"\e[B": history-search-forward
or equivalently,
# ~/.bashrc
if [[ $- == *i* ]]
then
bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'
fi
(the if statement checks for interactive mode)
Normally, Up and Down are bound to the Readline functions previous-history
and next-history
respectively. I prefer to bind PgUp/PgDn to these functions, instead of displacing the normal operation of Up/Down.
# ~/.inputrc
"\e[5~": history-search-backward
"\e[6~": history-search-forward
After you modify ~/.inputrc
, restart your shell or use Ctrl+X, Ctrl+R to tell it to re-read ~/.inputrc
.
By the way, if you're looking for relevant documentation:
Bash uses The GNU Readline Library for the shell prompt and history.
Bash complete function - Separating completion parts with character other than space
Here's a draft example:
_python_target()
{
local cmd=$1 cur=$2 pre=$3
if [[ $pre != -m ]]; then
return
fi
local cur_slash=${cur//./\/}
local i arr arr2
arr=( $( compgen -f "$cur_slash" ) )
arr2=()
for i in "${arr[@]}"; do
if [[ -d $i ]]; then
arr2+=( "$i/" )
elif [[ $i == *.py ]]; then
arr2+=( "${i%.py}" )
fi
done
arr2=( "${arr2[@]//\//.}" )
COMPREPLY=( $( compgen -W "${arr2[*]}" -- "$cur" ) )
}
complete -o nospace -F _python_target python
Try with the python-2.7.18 source code directory:
Bash history expansion with tab completion
The conceptual terms you're looking for is [command] history and history expansion - if you run man bash
, you'll find sections HISTORY
and HISTORY EXPANSION
devoted to this topic.
That said, in this particular case, it is not history expansion, but the special shell variable $_
that is your friend:
It expands to the last (expanded) argument of the most recent command.
Try the following, which mimics your scenario:
ls "$HOME"
# Type this on the command line and press TAB (possibly twice)
# _before_ submitting to TAB-complete to matching files in "$HOME"
# (irrespective of what the current directory is).
# $_ at this point contains whatever "$HOME" expanded to, e.g. "/Users/jdoe".
cp $_/
Note: Whether tab-completion works for a given command is unrelated to whether $_
is used or not. See man bash
, section Programmable Completion
, for how to manually enable tab-completion for commands of interest.
Preserve bash history in multiple terminal windows
Add the following to your ~/.bashrc
:
# Avoid duplicates
HISTCONTROL=ignoredups:erasedups
# When the shell exits, append to the history file instead of overwriting it
shopt -s histappend
# After each command, append to the history file and reread it
PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$'\n'}history -a; history -c; history -r"
Bash command history is not written when a window is closed
You need to leave the shell by typing exit
or Ctrl+d rather than just closing the window. exit
will write the history implicitly, you can explicitly trigger a history write with history -w
at any time.
Related Topics
Find All Files with Name Containing String
Init Function Invocation of Drivers Compiled into Kernel
Create Zip File and Ignore Directory Structure
How Have Both Local and Remote Variable Inside an Ssh Command
How to Get Amount of Queued Data for Udp Socket
How to Check If a Key Was Pressed in Linux
Cross-Compile a Rust Application from Linux to Windows
Difference Between Arm-Eabi Arm-Gnueabi and Gnueabi-Hf Compilers
What Is the Correct Way to Start a Mongod Service on Linux/Os X
Randomly Shuffling Lines in Linux/Bash
How to Get the Difference Between Two Dates Under Bash
Should I Use Libc++ or Libstdc++
Append Date to Filename in Linux
Shell: Find Files in a List Under a Directory