r/bash 15h ago

Readline tips and tricks

26 Upvotes

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 options
    
  • Basic 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 line
    • C-b - backwards a letter
    • M-b - backwards a word
    • C-f - forward one letter
    • M-f - forwards one word
    • C-e - end of line [insert TRON reference]
    • C-l - clear and reset screen
    • C-h - backspace
    • C-i - works as tab
    • C-j and C-m - works like pretting enter
    • C-_ undo
  • cut and paste from the prompt in a terminal without using a mouse.

    • C-k - kill (delete) from cursorr to end of line
    • M-d - kill to end of word
    • C-u - kill from cursor to start of line
    • C-w - kill last word
    • C-y - yank (paste) the last kill.
  • Cool Options

    • set enable-bracketed-paste on This 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, preventing
    • set completion-ignore-case on tab completion matches filenames regardless of capitalization.
    • set blink-matching-paren on - Easier to see
    • set colored-completion-prefix on - Again, easier to read.
    • set colored-stats on - Adds "/" to directories, and use $LS_COLORS in completions
    • set completion-ignore-case on & set completion-map-case on Enables 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 ambiguous
    • set match-hidden-files off Hidden files don't match "*" or the like.
    • set show-all-if-ambiguous on Don't beep and make me press tab again.
    • set completion-display-width 0 - only show 1 possible completion per line
    • set completion-query-items 1000 - If you hate the y/n prompt when there are many matches, increase the threshold:
    • set expand-tilde on ensures 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 into cd some/really/ong/path
    • C-x C-e (v in vi mode)- Open your current prompt in either your $VISUAL or $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.


r/bash 12h ago

shell-scheduler update

2 Upvotes

About a week ago I announced my project shell-scheduler here. Some people expressed interest, so this is a heads-up for them and for anyone else interested. Since that post, I've done some more work on this project:

New features: - Implement support for per-job timeouts - Implement support for job termination callback - Implement helper libraries for job termination (three independent libraries with different dependencies) - Implement automatic job termination callback selection (best fit from available helpers) - Support assigning job param values to custom variables with job_get_params <job_id> <var_name>=<param_name> - Implement jobs_init(): helper to reset previously configured job parameters (see updated README)

Bug fixes/reliability improvements: - Fix race between timeout and job completion record write to FIFO - Disallow duplicate job IDs - Improve validation of values received via environment variables - Significantly expand tests coverage - Various minor bugfixes

Documentation updates: - Improve the documentation and refactor it into 3 files: README.md, REFERENCE.md, TIMEKEEPING.md

At this point I have pretty much implemented everything I was planning to implement and no remaining bugs are known, so I consider this project feature-complete.

Bug reports and feature requests are still welcome.

Project's Github page