|Ctrl + A||Go tobeginning of line|
|Ctrl + E||Go to end of line|
|Ctrl + U||Clear all characters to the left of the cursor|
|Ctrl + R||Search through command history|
|Ctrl + C||Kill whatever you are running|
|Ctrl + Z||Background current process. fg restores it.|
|Ctrl + W||Delete the word before the cursor|
|Ctrl + K||Clear all characters to the right of the cursor|
|Esc + T||Swap the last two words before the cursor|
|Alt + F||Move cursor forward one word on the current line|
|Alt + B||Move cursor backward one word on the current line|
|Alt + .||Cycle through arguments used in previous commands|
Redireting output in many cases is as simple as UNIX/DOS:
find ~ -name test >outputfile
This will only redirect the standard output, to redirect standard error:
find ~ -name test 2>erroroutputfile
To redirect the output of both standard output AND standard error:
find ~ -name test >outputwitherrorsfile 2>&1
Eliminate 'loser takes all' Bash History
Bash only writes the command history on exiting the shell. If multiple sessions are open simultaneously the very last session to be closed will overwrite any command history from other sessions since it was opened. Confused? try the following:
- Opening a shell and type echo 1;echo 2 (session number 1.)
- Keep this session open and open another bash session (session number 2) and use the history command and you will see that the last two commands issued are not there, as the commands are only written on exiting the shell. But that it not all...
- Close session 1 and reopen another session (session number 3) and you will now see the echo 1;echo 2
- Close session number 2 and reopen another session (session number 4) and you will now see that echo 1;echo 2 has been lost!
How to fix
- Bash to save to the history upon every command, not when the session exits
- Bash must always append instead of overwriting command history.
- The history command must re-read the newlines upon running it, by default it only reads it when the session is started.
- Add the following to .bashrc:
PROMPT_COMMAND='history -a' shopt -s histappend alias history='history -n;history'
Refer to previous dirs and commandsRefer to the last dir within a command with:
~-/Insert the last command entered with:
!!If, for example sudo is not used when it should have been sudo !! can be used as an alternative to retyping/copying/pasting the last command.
Insert all the arguments from the previous command with
!*Refer to the Nth argument of the last command:
!!:nRefer to the first argument:
!!:2Refer to the last argument:
cd with no arguments always goes to your home dir.
cd to last dir: cd -
Put current dir in the stack pushd . Then go back to it with popd
Go to dir, run a command and then return to the current dir: (cd dir && command)
Scrolling ls listings made easier
Instead of ls /etc|less this can be shortened to less /etc . If a folder is specified in less it will ls is and less the contents.
Only useful as a simple calculator as an alternative to using a full caclualator like bc -l; fractions are decimals are not supported:
$ echo $[16*2] 32 $ echo $[100/11] 9