guest@linuxthings:~/guides$ man tips-tricks
Tips & Tricks
Here are some productivity boosters and handy shortcuts to make your terminal experience smoother.
Keyboard Shortcuts
Mastering these shortcuts will significantly speed up your typing.
Shortcut | Action |
|---|---|
Ctrl + A | Move cursor to the beginning of the line |
Ctrl + E | Move cursor to the end of the line |
Alt + B / Alt + F | Move back / forward one word |
Ctrl + U | Delete from cursor to the beginning of the line |
Ctrl + K | Delete from cursor to the end of the line |
Ctrl + W | Delete the word before the cursor |
Ctrl + Y | Paste back the text you just deleted |
Ctrl + R | Search your command history (press again for older matches) |
Ctrl + L | Clear the screen (same as |
Ctrl + C | Interrupt (stop) the current running process |
Ctrl + Z | Suspend the current process (use |
Ctrl + D | Exit the shell (or end input to a program) |
Alt + . | Insert the last argument of the previous command |
Tab Tab | Show all possible completions |
Prefer vi-style keys? Run set -o vi (and add it to ~/.bashrc). set -o emacs restores the default.
Useful Aliases
Aliases allow you to define shortcuts for long commands. Add these to your ~/.bashrc or ~/.zshrc.
# Easy navigation
alias ..='cd ..'
alias ...='cd ../..'
# Better ls defaults
alias ll='ls -alFh'
alias la='ls -A'
alias l='ls -CF'
# Safe file operations (prompt before overwriting)
alias cp='cp -i'
alias mv='mv -i'
alias rm='rm -i'
# System (Debian/Ubuntu; use dnf on Fedora)
alias update='sudo apt update && sudo apt upgrade'
alias ports='ss -tulpn'
alias myip='hostname -I'alias # List all current aliases
unalias ll # Remove an alias for this session
\rm file # Bypass an alias and run the real commandChaining Commands
You can run multiple commands in a single line.
# Run command2 only if command1 succeeds
mkdir newdir && cd newdir
# Run command2 only if command1 FAILS
ping -c 1 google.com || echo "Network down"
# Run command2 regardless of command1's outcome
ls; pwd
# Group commands and redirect their combined output
{ date; uptime; df -h; } > status.txtSudo History trick
Forgot to use sudo? Use sudo !! to rerun the last command with root privileges.
apt install git
# E: Could not open lock file... (Permission denied)
sudo !!
# Runs: sudo apt install gitFix the Last Command
^teh^the # Rerun the last command, replacing "teh" with "the"
fc # Open the last command in your editor, run it on saveReusing Arguments
mkdir -p ~/projects/new-site
cd !$ # !$ is the last argument: ~/projects/new-site
cp config.yml{,.bak} # Quick backup: copies config.yml to config.yml.bakBetter History
Add these to ~/.bashrc to keep more history and skip duplicates:
HISTSIZE=10000
HISTFILESIZE=20000
HISTCONTROL=ignoreboth # Skip duplicates and commands starting with a space
HISTTIMEFORMAT='%F %T ' # Show timestamps in the history command
shopt -s histappend # Append rather than overwrite across sessionsStarting a command with a space (with ignoreboth set) keeps it out of your history, which is handy for commands that contain secrets.
Rescue a Garbled Terminal
If the terminal stops echoing what you type, often after a program crashed or you printed a binary file, press Ctrl+J, type reset (even if you can't see it), and press Ctrl+J again.
Keep Jobs Running After Logout
nohup long_task & # Ignore the hangup when you disconnect
tmux # Or work inside tmux and detach with Ctrl+B d
tmux attach # Pick up where you left off laterQuick Calculations
echo $(( 1024 * 1024 )) # Integer maths in the shell
echo "scale=3; 22/7" | bc # Decimal maths
seq 1 5 # Print a sequence of numbers