Skip to content

Instantly share code, notes, and snippets.

@jquinter
Forked from gwixted/gist:4039676
Created December 8, 2019 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jquinter/d7cd3e2451d75e67fc3d51a2a9234d2c to your computer and use it in GitHub Desktop.
Save jquinter/d7cd3e2451d75e67fc3d51a2a9234d2c to your computer and use it in GitHub Desktop.
Terminal Cheatsheet

Note

This contains commands shamelessly stolen from many authors. I will try to list all of them at the end of the file, but if I've missed someone, let me know.

Legend

  • UC/Use Case : a practical/frequently used example of the command in action (I find it easier to add commands into my daily workflow if I have an example that uses them in something I often do)

The Prompt


Navigating / Manipulating Text (emacs style terminal) [3]

Ctrl + a - Jump to the start of the line

Ctrl + b - Move back a char

Ctrl + c - Terminate the command

Ctrl + d - Delete from under the cursor

Ctrl + e - Jump to the end of the line

Ctrl + f - Move forward a char

Ctrl + k - Delete to EOL

Ctrl + l - Clear the screen

Ctrl + r - Search the history backwards

Ctrl + R - Search the history backwards with multi occurrence

Ctrl + t - Transpose the current char with the previous

Ctrl + u - Delete backward from cursor

Ctrl + w - Delete backward a word

Ctrl + xx - Move between EOL and current cursor position

Ctrl + x @ - Show possible hostname completions

Ctrl + z - Suspend/ Stop the command

Ctrl + x; Ctrl + e - Edit line into your favorite editor

Alt + < - Move to the first line in the history

Alt + > - Move to the last line in the history

Alt + ? - Show current completion list

Alt + * - Insert all possible completions

Alt + / - Attempt to complete filename

Alt + . - Yank last argument to previous command

Alt + b - Move backward

Alt + c - Capitalize the word

Alt + d - Delete word

Alt + f - Move forward

Alt + l - Make word lowercase

Alt + n - Search the history forwards non-incremental

Alt + p - Search the history backwards non-incremental

Alt + r - Recall command

Alt + t - Transpose the current word with the previous

Alt + u - Make word uppercase

Alt + back-space - Delete backward from cursor

Tab Commands

(Here "2T" means Press TAB twice)

$ 2T - All available commands(common)

$ (string)2T - All available commands starting with (string)

$ /2T - Entire directory structure including Hidden one

$ (dir)2T - Only Sub Dirs inside (dir) including Hidden one

$ *2T - Only Sub Dirs inside without Hidden one

$ ~2T - All Present Users on system from "/etc/passwd"

$ $2T - All Sys variables

$ @2T - Entries from "/etc/hosts"

$ =2T - Output like ls or dir

Command history

Working with your previous command

!! - Last command !foo - Run most recent command starting with 'foo...' (ex. !ps, !mysqladmin)

!foo:p - Print command that !foo would run, and add it as the latest to

!$ - Last 'word' of last command ('/path/to/file' in the command 'ls -lAFh /path/to/file', '-uroot' in 'mysql -uroot')

!$:p - Print word that !$ would substitute

!* - All but first word of last command ('-lAFh /path/to/file' in the command 'ls -lAFh /path/to/file', '-uroot' in 'mysql -uroot')

  • UC source your profile after editing it:
  • vim ~/.bash_profile
  • source !*

!:p - Print words that ! would substitute

^foo^bar - Replace 'foo' in last command with 'bar', print the result, then run. ('mysqladmni -uroot', run '^ni^in', results in 'mysqladmin -uroot')

Working with History [2]

note you can combine the commands above with past commands. For example: rm !5429$ will run rm on the last word of command #5429 history - Print all previous commands you've run (use history | grep "ssh" to search for command and make your life easier)

!-4 - run the command that is 4 commands back from your last command

!2 - Run command with the id of 2.

!'string' - run the last command that begins with 'string'

Executables


Find out Where a program is located

which subl - Outputs path where executable subl is located e.g. usr/local/bin

cd `dirname $(which subl)` change the current directory to the path of subl e.g. (cd /usr/local/bin) [4]

Files


Batch renaming file extensions with rename

Note: rename is not installed by default on some systems (Mac Os included) user your favorite package manager to install it. Homebrew users can just brew install rename.

The general syntax is:

rename 's/search_for_string/replace_string_with_this/' files

UC: Change the file extension of all .html files to .md files, while preserving the names

Given the following directory:

regular-old-file1.html regular-old-file2.html regular-old-file3.html regular-old-file4.html regular-old-file5.html regular-old-file6.html

The following code:

rename 's/.html/.md/' *.html

outputs:

regular-old-file1.md regular-old-file2.md regular-old-file3.md regular-old-file4.md regular-old-file5.md regular-old-file6.md

This is a pretty simple use case, but I find that I frequently have to change file extensions/names on large groups of files. This is a lot better than manually using mv everything.

Scripting


Run something: for i in a b c; do $i 'hello'; done

Do something on a bunch of files: for i in *.rb; do echo "$i"; done

If syntax: if [[ -e .ssh ]]; then echo "hi"; fi

Numerical comparison: i=0; if (( i <= 1 )); then echo "smaller or equal"; else echo "bigger"; fi

file check flags: -e: file exists -f: regular file (non directory) -d: directory -s: non-zero file -x: execute permission

Customizing


.bash_profile = sourced by login shell,

.bashrc = sourced by all shells,

.bash_aliases = should be sourced by .bashrc/.bash_profile

Making changes

Any modifications you make to your profile will only show up after you:

  • A: open a new shell window OR
  • B: source your profile with source ~/.bash_profile (preffered IMO)

Use bash in "vi" editing mode

Add the following to your .bash_profile set -o vi

  • Be warned that this takes some getting used to

Avoid duplicates in your history:

export HISTCONTROL=ignoredups

SED


Grep and Sed files

search the current directory for a string, and replace it with another grep -lir 'id="left"' * | xargs sed -ie 's/id="left"/id="left" class="sidebar"/'

VIM


Efficiency

Switching Editing modes

  • Use ctrl + c instead of esc to quickly exit insert mode

Moving Around [1]

Use of Visual Mode

  • v start highlighting characters
  • V start highlighting lines (I missed this one for so long)

Efficient Deletes

Remember, you are able to combine most vim shortcuts/motions with delete. So d + $ deletes to the end of the line, d 4 wdeletes four words in front of the cursor.

In Command Mode

  • ce (delete word after cursor and go into insert mode)
  • cb (delete part of word before cursor and go into insert mode)
  • c i w (delete currently cursored word and go into insert mode)
  • C (delete delete line after cursor and go into insert mode)
  • r (change a single character)
  • d f ) (delete to the next ")" character -- and delete the character itself)
  • d F ) (delete to the previous ")" character)
  • d t ) (delete to before the next ")") [5]
  • ci ' OR ci " OR { (change the next single/double/bracketed/etc quoted text)

Copy and paste

y - yank {number} + y + w - Yank 2 words

Thanks to

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment