r/bash • u/TheHappiestTeapot • 15h ago
Readline tips and tricks
Readline, through the file ~/.inputrc, define how bash, tab completion, and lots of other things behave. I've been playing with it recently and it's pretty powerful, so here's the cool stuff I've found. This is in no way the complete power of these tools
You already know about searching history, but how
There's cool stuff built in (Debian 13, YMMV):
First up, let's see what you've got to work with.
$ bind -P # show the current mapping. $ bind -l # Show all available functions $ bind -v # show options (***v***ariables) $ help bind # See all optionsBasic Movement - emacs-ish and vim-ish keybindings. Easily move around text using controls you're familiar with. Check the manual for vi style bindings Much faster than using the arrow keys.
C-a- start of lineC-b- backwards a letterM-b- backwards a wordC-f- forward one letterM-f- forwards one wordC-e- end of line [insert TRON reference]C-l- clear and reset screenC-h- backspaceC-i- works as tabC-jandC-m- works like pretting enterC-_undo
cut and paste from the prompt in a terminal without using a mouse.
C-k- kill (delete) from cursorr to end of lineM-d- kill to end of wordC-u- kill from cursor to start of lineC-w- kill last wordC-y- yank (paste) the last kill.
Cool Options
set enable-bracketed-paste onThis might be the default on your distro already, but if it's not add it now. Instead of paste running a bunch of commands it inserts all the text on one line, preventingset completion-ignore-case ontab completion matches filenames regardless of capitalization.set blink-matching-paren on- Easier to seeset colored-completion-prefix on- Again, easier to read.set colored-stats on- Adds "/" to directories, and use$LS_COLORSin completionsset completion-ignore-case on&set completion-map-case onEnables case-insensitive matching and treats hyphens (-) and underscores (_) as mapaable characters.set completion-prefix-display-length 3- only show the last 3 chars when tab is ambiguousset match-hidden-files offHidden files don't match "*" or the like.set show-all-if-ambiguous onDon't beep and make me press tab again.set completion-display-width 0- only show 1 possible completion per lineset completion-query-items 1000- If you hate the y/n prompt when there are many matches, increase the threshold:set expand-tilde onensures that pressing Tab after ~ immediately expands it to your full home directory path in the completion list.
Neat Stuff
M-.inserts the last arguments from the last command, press again to cycle backwards.- e.g.:
$ mkdir -p some/really/long/path,cd [M-.]turns intocd some/really/ong/path
- e.g.:
C-x C-e(vin vi mode)- Open your current prompt in either your$VISUALor$EDITOR. Want your full editing power? Want to edit multi-line command with ease? Finally reached a version you want to save?C-M-e: Possibly the coolest. expand the current line. Expands aliases and resolves variables and subshells.$ alias df="df -h -t ext4 -x tmpfs" $ df [C-M-e] $ df -h -t ext4 -x tmpfs $ echo $COLUMNS wide, $LINES long, area: $((COLUMNS * LINES)) [C-M-e] $ echo 189 wide, 65 long, area:10773 # Still at the prompt!M-#- Ever want to comment out your current command and start a new one? This will do it for you!Make your own! Add them to ~/.inputrc: A couple of my favorites:
# `Ctrl-x o`: save output in a timestamped log file "\C-xo": "\C-e > output-$(date +%s).log"`Forget to add sudo?
# Ctrl-x !: Prepend with "sudo, then return to end of line" "\C-x!": "\C-asudo \C-e"run the last command again and pick a line from the output using
fzf# re-runs the last command and pipes it to fzf for interactive selection. "\C-x/": '$(!!|fzf)'
Next time I'll ramble about the compose key, .XCompose, and why Chrome sucks for not supporting it.