How I use bash
Posted on Fri 04 June 2021 in blog
bash
I presented this as a lightnign talk at « En attendant la PyCon Fr », there's a video (in french).
How to read:
When I write ^A it mean I press Ctrl-A, which is noted C-a
(bash/emacs notation).
M-a means Meta-a (it's left alt on current keyboards).
I avoid singleline prompt
Because of the following issue:
PS1='$ '
printf "pouette"
ls^A
ls -l^A
Issue is: readline have two ways to move at the beginning of a line, and have no way to know where the cursor is, and assume the line start at column 0.
Easy, daily, shortcuts
Killing and Yanking from the killing stack
C-a C-k: Move to the beginning, then kill the line.C-y,M-y: Yank and yank pop.
Example:
$ git push origin HEAD^A^Kgit commit -m "FIX"^A^Kgit add -u
C-y
Enter
C-y
M-y
Enter
Moving
C-a(beginning of line)C-e(end of line).C-pandC-n: like up and down arrows, to browse history.
Avoid C-pC-pC-pC-pC-p or ↑↑↑↑↑↑, use C-r.
Fixing typos
C-t(swap chars),M-t(swap words).M-l,M-u,M-c(lowercase, uppercase, capitalize)C-g(abort)
Cleaning
C-l (clear screen). Oh, if it's not enough, like after killing sl,
use reset, so ENTER reset ENTER to ensure you type it in a clear
line, or C-a C-k reset ENTER to avoid executing blindly.
sudo !!
$ man bash | grep '!!'
!! Refer to the previous command. This is a synonym for `!-1'.
so:
$ apt upgrade
E: ... are you root?
$ sudo !!
sudo apt upgrade
...
Globstar
shopt -s globstar:
$ rm **/*.md
same as:
$ find -name '*.md' -delete
example:
sed -i '1i#!/usr/bin/env python3' **/*.py
Other shortcuts I use
And I like them for the nice symetry:
C-fto move forward a char,M-fto move forward a word.C-bto move backward a char,M-bto move backward a word.C-dto delete a char,M-dto delete a word.
Pipelines for the win!
$ man bash | grep -C1 C-a
Commands for Moving
beginning-of-line (C-a)
Move to the start of the current line.
Subshells for the win!
emacs $(git grep -l PATTERN)
Bonus
Those are the same in emacs, save brain space, use emacs.