Pipe anything to pbcopy
to copy it to the clipboard: cat ~/.ssh/id_rsa.pub | pbcopy
To insert the last argument of the previous command wherever the cursor currently is, press alt+.
(or, on a mac, esc+.
)
touch {a,b,c}.txt
- writes a.txt
, b.txt
, c.txt
rm [ab].txt
- deletes a.txt
and b.txt
You can use Ctrl+a
and Ctrl+e
to move the cursor to the start and end of the line respectively
The cd command has a history stack. cd -
will take you to the previous directory you were in (running it again will take you back). cd -3
will take you three directories back. (so, cd -
is the same as cd -1
)
You can use the dirs -v
command to see the current stack. The ten most recent ones can be shown in oh-my-zsh with the d
command.
lk -al
- throws an error - lk: command not found
^lk^ls
- re-runs the previous command, replacing lk
with ls
Bang arguments can be appended with :p
to preview the command before executing it (oh-my-zsh enables this all the time automatically)
The one that everyone has hopefully heard of is !!
- repeat the last command. Most commonly used:
rm -rf /
This operation requires root
sudo !!
!3
will run the 3rd command in your bash history
!-3
will run the 3rd last command in your bash history
!-1
is the same as !!
!ec
- runs the last command that starts with “ec” (likely echo).
!?cho
- runs the last command that has “cho” anywhere in it (likely echo). Basically, a non-interactive version of Ctrl-r
reverse searching.
!!:3
or !:3
- expands to the third argument of the preceding command (change the number for the different arguments obviously)
!$
- the last item of the previous command
!^
- the first argument of the previous command (same as !:1
)
!*
- all arguments (non-command) from the previous command
Use !:t
to strip the filename out of the last argument of the previous command.
wget ftp://ruby-lang.org/pub/ruby/1.8/ruby-1.8.7-p330.tar.gz
tar xzvf !:t
The unix shell automatically expands file path patterns. When multiple files match, they are listed, separated by spaces.
The star character matches 0 or more characters, apart from /
(the directory separator.)
For example, on OSX:
echo ~/Do*
/Users/user/Documents /Users/user/Downloads
### Question Mark ?
The question mark matches exactly one single character, apart from `/`.
```bash
echo /var/logs/nginx/error.log.?.gz
/var/logs/nginx/error.log.1.gz /var/logs/nginx/error.log.2.gz /var/logs/nginx/error.log.3.gz
Unlike the *
and ?
matchers, brace expansion will happen regardless of whether or not a file exists.
Brackets can be used to expand a list into a filepath:
echo {a,b,c}.txt
a.txt b.txt c.txt
Brackets can also deal with ranges:
echo error.{9..12}
error.9 error.10 error.11 error.12
echo {a..e}
a b c d e
esc+'
will put single quotes around the current command
esc+_
will insert the last argument of the previous command. release all the keys and press again to go back in history
esc+q
will clear the line, but repaste that line into the next command (so you can do another command before it)
cd one two
will replace all instnaces of one
in the CWD with two