Ctrl-r search history
- then Ctrl-r again to show next match
- then Tab to show all options
Ctrl-p previous command or arrow up
Ctrl-n next command or arrow down
export HISTCONTROL=ignoreboth:erasedups
Add to .bashrc to avoid duplicate entries
Ctrl-a to beginning of line
Ctrl-e to end of line
Alt-b one word back
Alt-f one word forward
Ctrl-k delete to end of line
Ctrl-u delete to beginning of line
Alt-d delete to end of word
Ctrl-w delete to beginning of word
Alt-Backspc same
cd - change to last dir
pushd <dir> mark current dir and go to <dir>
popd go to marked dir
z fuzzy cd, install from https://github.com/rupa/z
j fuzzy cd and more, install via autojump
Ctrl-z to background & suspend
bg recent background app continue running
fg bring recent background app to front
disown -h remove recent background app from current tty
fg %n bring nth app to front, e.g.: fg %2 for second
less better than cat, doesn't flood screen, same keys
find find files, e.g. find / -name <filename>
ag install via the_silver_searcher, faster grep
tree shows dir like a GUI app, install
!! last command, e.g. sudo !!
fish bash alternative with more sensible defaults
man bash read more about bash
There is Ctrl-y for pasting deleted strings, which goes along very well with these commands:
Ctrl-k delete to end of line
Ctrl-u delete to beginning of line
Alt-d delete to end of word
Ctrl-w delete to beginning of word
Alt-Backspc same
Bash keybindings behave like Emacs by default, hence every time you invoke one of these, it will put the string into a buffer, which can be later pasted using Ctrl-y.
This is very useful. For instance, if you remembered that you didn't `mkdir /mnt/disk` while in the middle of the command `mount /dev/sdb /mnt/disk`, you can delete what you have already typed with Ctrl-u; issue `mkdir /mnt/disk`; than you can paste the previous command with Ctrl-y. Very useful and I use it all the time! (Ctrl-u have never tied to my muscle memory, so I usually do Ctrl-a Ctrl-k to move to the beginning of the line and delete what comes next).
Other tips:
cd goes to home dir
Ctrl-] moves cursor to character (such as vim f)
Ctrl+Meta+] moves to character backwards (vim F)
I also put these in my .bashrc for searching history pressing up/down.
This is different of searching with Ctrl-r. When you type part of a command, such as
mkdir /dev
and press up, it will complete with previous ocurrences of commands starting with `mkdir /dev`. It is faster than Ctrl-r if you already know what to do.
Here are some notes I took about ZSH that may be helpful to someone
* zsh bang
previous commands:
!! Previous command
!-1 Previous command
!-2 Second previous command
Getting individual arguments:
!$ Last arg
!* All args
!$:h Last argument, strip one level
!!:1 First arg
Modifying commandlines
!ls:$:h Head
!ls:$:t Tail
!ls:$:r Rm Suffix
!ls:$:s/user/dude/ Substitute user by dude
* Filename Modifiers
:h head (dirname)
:t tail (basename)
:e extension
:r remove extension
:l lowercase conversion
:u uppercase conversion
:p print command but do not execute
:q quote substituted words
* zsh globbing
**/* Picks out all files and directories, recursively.
***/* Ditto, following symlinks
* Any string, including the null string.
? Any character.
[...] Any of the enclosed characters. Ranges of characters can be specified by
separating two characters by a -.
[^...] Inverse
[!...] Inverse
<x-y> Any number in the range x to y, inclusive. Either can be omitted.
^x Anything except the pattern x.
x|y Either x or y.
x# Zero or more occurrences of the pattern x.
x## One or more occurrences of the pattern x.
globbing qualifiers
* file type
(.) Regular files
(/) Directories
(@) Symlinks
(=) Sockets
(p) Named pipes
(%) Device
(%b) Block devices
(%c) Character devices
* permissions
(r) Readable by owner
(w) Writable by owner
(x) Executables by owner
(R) Readable by world
(W) Writable by world
(X) Executable by world
(A) Readable by group
(I) Writable by group
(E) Executable bu group
alternatively a chmod style syntax is supported:
ls **/*(.:g-w:)
* Ownership
(U) Your own user
(G) Your own group
(u:userid:) User with userid
(g:userid:) group with groupid
example: list files belonging to user www-data with ls -l /(u:www-data:)
Modification and access time
(m) Modification time
(a) Access time
(-) Before
(+) After
(M) Months
(w) Weeks
(h) Hours
(m) Minutes
(s) Seconds
example: list files accessed last month with ls /(.aM-1)
file size
(L) File size (defaults to bytes)
(k) use kilobytes
(m) use megabytes
(p) use 512-byte blocks
example: list all files larger than 10 megabytes with ls /(.Lm+10)
ls /some_file # Lists file under current directory
zargs -- /(.) -- ls -l # Ditto
* zsh Iteration
for i (/home/**/q*) rm $i
for i in /**/b*(u:miklophone:); do rm $i ; done
for f in http://zsh.sunsite.dk/Guide/zshguide{,{01..08}}.html; do...
zargs -- /**/b*(u:miklophone:) -- rm
* Renaming files with Zsh
Numerical prefix:
$ i=1; for j in *; do mv $j $i.$j; ((i++)); done
$ i=1; for f in *; do mv $f $(echo $i| awk '{ printf("%03d", $0)}').$f;
((i++)); done
$ integer i=0; for f in *; do mv $f $[i+=1].$f; done
$ for a in ./**/*\ *(Dod); do mv $a ${a:h}/${a:t:gs/ /_}; done
Substitute r for l
s/l/r[/]
Unless preceded immediately by a g, with no colon between, the
substitution is done only for the first string that matches l. For
arrays and filename expansion, this applies to each word of the
expanded text.
The left-hand side of substitutions are not regular expressions,
but character strings.
Any character can be used as the delimiter in place of '/'. A
backslash quotes the delimiter character.
The character '&', in the right-hand-side r, is replaced by the
text from the left-hand-side l. The '&' can be quoted with a
backslash.
* zsh if
# see also -z below
if [[ $foo = '' ]]; then
print The parameter foo is empty
fi
# quote the term to avoid pattern matching (not regexp, btw)
if [[ biriyana = b* ]]; then
print Word begins with a b
fi
# Numbers
if [[ $number -gt 3 ]]; then
if [[ $number -lt 3 ]]; then
if (( $number > 3 )); then
if (( 3 )); then
# Files
if [[ file1 -nt file2 ]]; then # newer than
if [[ file1 -ot file2 ]]; then # older than
if [[ file1 -ef file2 ]]; then # same file
# Variables
if [[ -z "$var is blah; test fails" ]]; then # zero length?
if [[ -n "$var is blah; test passes" ]]; then # non-zero length?
For all but the simplest renaming tasks, I use "qmv" (from the renameutils[1] package). It loads up the target filenames/paths in a buffer in your $EDITOR, and then you can use the full power of your editor to rename. When you save the buffer, qmv does a sanity check and then does the actual renaming.
Here's a simple example: [2], but it can get arbitrarily complex -- in a much more visual and interactive way than what a shell alone would be capable of.