Bash Notes:
default
# Esc = ALT
# (esc + b = alt +b) (in case you can't use alt / mod1)
set -o vi
# Put readline in vi mode
set -o emacs
# Put readline in emacs/default mode
Tab
# Autocompletes from the cursor position.
alt+bksp
^w - delete word backward
# Delete one word back non-alphanumerically delimited
^|
# (that's a pipe) sends SIGQUIT.
^_
# Undo typing
alt+_
# Insert last argument
alt+.
# Insert the final argument to the last command at the cursor point
^a
# Cursor to beginning of line (home-key)
^b
# Move back one character.
alt+b
# Cursor one word back
^c
# Sending signal SIGINT the current process you run (usually the process will be killed)
alt+c
# Capitalizes the character under the cursor and moves to the end of the word.
^d
# Delete char under cursor. If blank line send EOF, Exit/logout shell.
alt+d
# Cut one word after the cursor
alt+Del
# Cuts the word before the cursor
^e
# Cursor to end of line (end-key)
^f
# Move forward one character.
alt+f
# Cursor one word forward
^g
# Cancel history search
^h
# Clear one character before the cursor. (backspace)
^j
# Select command from history search ^r ^s
^k
# Cut all characters after the cursor.
^l
# Clear screen
alt+l
# Lowercases one word in front of the cursor
^n
# Scroll next command (downArrow-key)
^o
# Executes the found command from ^r or ^s and moves to next command in history
^p
# Scroll up previous commands (upArrow-key).
^r
# Recall history from previously used commands. (each ^r next corresponding command)
alt+r
# Cancels changes and put back the line as it was (revert)
^s
# Back to the next recent command of the search
# (careful no to execute it from a terminal because this command also launches its XOFF).
# If you changed that XOFF setting, use Ctrl+q to return.
^t
# Swap the last two characters before the cursor.
alt+t
# Swap the last two words before the cursor
^u
# Cut all characters before the cursor.
alt+u
# Uppercase one word after the cursor
^v tab/enter (or any char)
# Insert a tab, or ^M ,etc
^w
# Cut one word before the cursor
^x ^e
# Edits the current line in the $EDITOR program, or vi
^x ^x
# Jump cursor to last position
^y
# Paste the last cut characters by CTRL+u or CTRL+k or ALT+d (yank clipboard to cursor)
^alt+y
# Insert first argument of previous command, (ALT + 2-9, CNTRL + ALT +y : insert nth argument)
^z
# Put current process into background process with signal SIGTSTP.
# To restore it, use command fg [process name or job id]
PROMPT_COMMAND='history -a && history -n'
# Collects history from current open terminals
cp ./test.txt{,.bak}
# Copies test.txt to test.txt.bak
echo $1
# Echos last argument passed to console
# Used inside a script
add() (IFS=+; echo $(($*))
add 2 4 2
# Creates a function called add
# Adds 2 4 2 together
find -name '*.jpg' | gawk 'BEGIN{ a=128 }{printf "mv %s banner-kat-%04d.jpg\n", $0, a+=4}' | bash
# Rename files sequentially by 4.
M-b
Esc-b
# Move back one word (Meta = alt, winkey, etc, depending on settings)
# Escape-b may also work (good if window manager is eating your Meta key)
a=1
for i in *.jpg; do
new=$(printf "%04d.jpg" ${a}) #04 pad to length of 4
mv ${i} ${new}
let a=a+1
done
# Rename jpg files with leading 4 digit numbers
find -name '*.jpg' | gawk 'BEGIN{ a=1 }{ printf "mv %s %04d.jpg\n", $0, a++ }' | bash
# Rename jpg files with leading 4 digit numbers
###############################################
# Get ascii values of a character using printf:
printf "%x" "'A"
printf "%x" "'\""
# Hex
printf "%x" "'A"
printf "%x" "'\""
# Decimal
printf "%o" "'\""
printf "%o" "'\""
# Octal
printf "\047"
#Ascii char from octal
printf "\x41"
#Ascii char from hex
printf "\x$(printf %x 97)"
printf "\x$(printf %x 34)"
#Ascii char from decimal via hex
#end-get-ascii-values-of-a-character-using-printf
#################################################
######################
# Bash Prompt Examples
PS1="\n\[\e[0;36m\]┌─[\[\e[0m\]\[\e[1;33m\]\u\[\e[0m\]\[\e[1;36m\] @ \[\e[0m\]\[\e[1;33m\]\h\[\e[0m\]\[\e[0;36m\]]─[\[\e[0m\]\[\e[1;34m\]\w\[\e[0m\]\[\e[0;36m\]]\[\e[0;36m\]─[\[\e[0m\]\[\e[0;31m\]\!\[\e[0m\]\[\e[0;36m\]]\[\e[0m\]\n\[\e[0;36m\]└─[\[\e[0m\]\[\e[1;37m\]\$\[\e[0m\]\[\e[0;36m\]]› \[\e[0m\]"
# Creates a two level prompt with user, host, directory, bash history
# on first line and $ on second.
PS1="\n\[\e[0;36m\]┌─[\[\e[0m\]\]\[\e[1;34m\]\w\[\e[0m\]\[\e[0;36m\]]\[\e[0m\]\n\[\e[0;36m\]└─[\[\e[0m\]\[\e[1;37m\]\$\[\e[0m\]\[\e[0;36m\]]› \[\e[0m\]"
# Same as above with some of the extras ripped out.
# Still has to many color changes.
PS1="\[\e[0;36m\]┌─[\[\e[0m\]\]\[\e[1;34m\]\w\[\e[0m\]\[\e[0;36m\]]\[\e[0m\]\\[\e[0;36m\]└─[\[\e[0m\]\[$\[\e[0;36m\]]› \[\e[0m\]"
# Same as above, get rid of newline at beginning.
PS1="\[\e[0;36m\]┌─[\[\e[0m\]\]\w\[\e[0;36m\]]\[\e[0m\]\n\[\e[0;36m\]└─[\[\e[0m\]\[$\[\e[0;36m\]]› \[\e[0m\]"
# Finally get rid of color around current folder.
PS1="\[\e[0;36m\]┌─[\[\e[0m\]\]\w\[\e[0;36m\]]\[\e[0m\]\n$ "
# Leave only the $ on the bottom line
# Special characters
\a : an ASCII bell character (07)
\d : the date in "Weekday Month Date" format (e.g., "Tue May 26")
\D{format} : the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required
\e : an ASCII escape character (033)
\h : the hostname up to the first '.'
\H : the hostname
\j : the number of jobs currently managed by the shell
\l : the basename of the shell’s terminal device name
\n : newline
\r : carriage return
\s : the name of the shell, the basename of $0 (the portion following the final slash)
\t : the current time in 24-hour HH:MM:SS format
\T : the current time in 12-hour HH:MM:SS format
\@ : the current time in 12-hour am/pm format
\A : the current time in 24-hour HH:MM format
\u : the username of the current user
\v : the version of bash (e.g., 2.00)
\V : the release of bash, version + patch level (e.g., 2.00.0)
\w : the current working directory, with $HOME abbreviated with a tilde
\W : the basename of the current working directory, with $HOME abbreviated with a tilde
\! : the history number of this command
\# : the command number of this command
\$ : if the effective UID is 0, a #, otherwise a $
\nnn : the character corresponding to the octal number nnn
\\ : a backslash
\[ : begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
\] : end a sequence of non-printing characters
# Colors changing
# put \[ and \] around color codes because bash
# does not count them when creating line wraps
\e[ : Start color scheme.
x;y : Color pair to use (x;y)
$PS1 : Your shell prompt variable.
\e[m : Stop color scheme.
Color Code
Black 0;30
Blue 0;34
Green 0;32
Cyan 0;36
Red 0;31
Purple 0;35
Brown 0;33
Blue 0;34
Green 0;32
Cyan 0;36
Red 0;31
Purple 0;35
Brown 0;33
Change the 0 to a 1, for a lighter version
of the color.
#end-Bash-Prompt-Examples
#########################
default