Ask HN: Can I see your cheatsheet?

346 points by fastily ↗ HN
Almost everyone I know keeps a list of (easily forgettable) command line snippets somewhere. I can't imagine that HN folks would be any different :)

So that said, could I please see your cheatsheet?

I'll go first: https://github.com/fastily/cheatsheet

172 comments

[ 3.6 ms ] story [ 232 ms ] thread
tldr is one of the best things to happen to the command line
I'm probably missing something here, but it seems to me that it's just a summary of the command plus a few examples?

I usually just man the command and / my way either to the Examples section, or search for whatever other question I may have.

But I'm on OpenBSD, where man pages are worth the time you spend reading them.

Other than that, although I always intend to add useful stuff to my ~/txt/scratch, in the end it's either Ctrl-R or history | grep.

Man pages are worth reading. Once you have a good handle on the command TLDR is great. It gives you you just enough to jog your memory.

Searching history or keeping your own notes work but this is easier for me. Everyone finds a work flow they like. It might not be the tool for you.

  tldr <command>
is great. It's a cheatsheet for every command and one of the first things I advise newbies to install.
I have maybe 50 pages of cheat codes worth. I haven't had the time to post them somewhere yet. Remind me... a week from now :D
Remind me... a week from now :D

--> This is actually a real problem on forums --> just to remind a stranger on a forum to commment on something. --> I´m working on a concept / tool to make it simple.

--> Would you like to talk about that? Ron.

My only real cheatsheet is a list of shortcuts I maintain for programs/apps I use:

    system:
    ctrl+space=switch keyboard input language
    
    iterm2:
    cmd+; = autocomplete
    fn+cmd+< = scroll to top of buffer
    fn+cmd+> = scroll to bottom of buffer
    fn+cmd+^ = pgup buffer
    fn+cmd+v = pgdown buffer
    fn+< = beginning of line
    fn+> = end of line
    shift+cmd+[ = previous tab
    shift+cmd+] = next tab
    cmd+K = delete til end of line
    cmd+enter = full-screen on/off
    cmd+shift+e = show command timeline!!!
    
    chrome:
    opt+cmd+J = devtools
    cmd+] = devtools tab right
    cmd+[ = devtools tab left
    opt+space = start/stop toggl
    fn+cmd+< = move tab left
    fn+cmd+> = move tab right
    fn+cmd+^ = move tab to start
    fn+cmd+v = move tab to end
    
    sublime:
    ctrl+` = open python console
    cmd+shift+P = open command palette
    cmd+P = search open files
    ctrl+opt+C = open in browser with View in Browser package (create tmpfile if dirty)
    cmd+shift+[ = Move to left pane
    cmd+shift+] = Move to right pane
    ctrl+0 = move to sidebar
    cmd+k+b = show/hide sidebar
    cmd+k+u = uppercase
    cmd+k+l = lowercase
    ctrl+shift+v = multiple cursor down
    ctrl+shift+^ = multiple cursor up
    shift+tab = back up tab indentation (at beginning of line)
    cmd+[ = dedent (when highlighted)
    cmd+[ = indent (when highlighted)
    cmd+shift+. = show dotfiles in open file view
    cmd+shit+g (when opening file) = navigate to path for file selection
    
    # edit package override files in /User/user/Library/Application Support/Sublime Text 3/Packages/User/
    
    VS Code:
    option+^ = drag line up
    option+v = drag line down
    ctrl+` = open terminal
    cmd+p, :#<enter> = go to line
    ctrl+enter = open alongside
    cmd+b = open/close side panel
    cmd+option+shift+> = move tab right
    cmd+option+shift+< = move tab left
    cmd+option+F = toggle fullscreen
    cmd+shift+l = highlight all selected
    cmd+shift+o = type fn name to jump to in open file
    cmd+option+shift+c = copy path of active filename
    
    Asana:
    cmd+shift+M: monospace highlighted text
(comment deleted)
I keep some as shell aliases, shell scripts or Git aliases, but mostly I just use my shell history, finding commands with Ctrl+R and a few characters that I know to be in there somewhere. (If you want to do this, make sure you set its limit high enough so you don’t lose stuff.)
My cheatsheet happens to be just my muscle memory. And, for applications I don't use often, mostly just intuitive guesses.

Sounds pretty messy but I've never felt the need to maintain a cheatsheet.

I don't see why one would. I know I'm possibly being naive.

On a related note: has anyone used a good tool for using/inserting these kinds of custom snippets? It would be neat to have a snippet manager with quick search bound to a keypress.
On Linux, you can make your own very quickly using Rofi and a clipboard manager.
Make sure to create a cheatsheet for how to use your cheatsheet manager.
I just maintain one big org-mode file and check it into git.
Honestly I just store them as files (bash scripts if you will), make them executable, chuck ~/scripts into $PATH

Bonus: it's all stored in a git repo

I do kinda live on the command line, ymmv

I made my own with search and autocomplete, it works for me and I have it on GitHub, but it's pretty specific to my workflow and there are so many different options in this thread (a lot of which are probably better).

I also liked the idea here to prefix all your scripts https://news.ycombinator.com/item?id=31846902 and would personally recommend using fish or zsh with the improvements from ohmyzsh for autocomplete and hints.

I never did have a cheatsheet. Strange since I've been on the *nix journey since 1998.

I do like the cheat command but it's still not habitual to use it, I grew up with Google so I'm far too dependent on it.

tab tab tab tab tab tab tab tab As someone who is quite severely dyslexic tab complete changed my life.
My extremely short shell cheatsheet for things I do rarely enough to forget each time:

    Process substitution:
        bash: diff <(echo hello) <(echo world)
        fish: diff (echo hello | psub) (echo world | psub)
    
    Rsync directories:
        rsync -a -v --dry-run other:dir/ dir/
        Syncs the contents of dir (`/` needed)
    
    Script exit handler:
    bash:
        function finish {
          # exit handling code
        }
        trap finish EXIT # EXIT is special and works on normal exit as well as interrupts
Process substitution is one of those tricks that I don't have to look up each time as I find that I'm always using it - but that probably says more about the number of times that I'm given randomly ordered files that I need to compare with diff to figure out what's actually changed.
http://cht.sh/ - I use this a lot. It outputs a short summary of common commands for many cli tools. You can curl to it https://cht.sh/grep for example or install their cli client
This is awesome! Thank you! I am so frequently visiting https://tmuxcheatsheet.com/, and it's not even that great of a cheat sheet.
Glad you found it useful. It has become one of the things I install when I setup a machine.

Being able to use curl as a client as well is so powerful as well. Whenever you are using ssh on some vps you can still get cheatsheets for all your commands.

No maintained cheat sheet here either. I do use `history | grep` quite a lot though.

One thing I try to do is to keep my keyboard shortcuts consistent between programs/systems. That way the chance of my instinct of pressing `<CTRL>+a` to move the cursor to the start of the line will be correct more often.

Another thing I try to do when a project has a set of complex commands that are needed, is to add them as a rule in its Makefile, that way they're available for my colleagues as well.

I use Ctrl+R chronically for the same thing.
I've been loving mcfly[0], a history search that can be bound to <ctrl+r>. It makes suggestions based on your current directory as well as your command history.

[0] https://github.com/cantino/mcfly

same here - my cheatsheet is `history | grep`
I'm curious why history | grep and not ctrl-R?
Probably because ctrl-R doesn't present you a list in most shells. Maybe you want to scan the list visually and look at neighboring commands. ctrl-R doesn't really take advantage of human vision by showing all results next to each other.
For me that’s it indeed; grep, sometimes with -A -B is much faster in general for me because it might be similar commands which now will appear next to eachother.
Agreed on the value of the list. I additionally dedupe as follows:

H() { history | egrep -v '^ *[[:digit:]]+ +H +' | grep "$@" | sort -rk 2 | uniq -f 1 | sort; }

That's pretty much it. I used to have to use quite a few different shells, so `<CTRL> + r` wasn't always available, but `history | grep` usually was. Once I'd got in the the habit of using `history | grep` I found that I liked the extra context provided by seeing a group of commands with a few variations between the lines. So often it helps jog my memory of what parameters I'll need to change before using it.
Regular expressions. Sometimes you remember two non-adjacent parts. Ctrl-R doesn't let you find "when I used find with awk" unless you type in everything in between.
and when history grepping starts to repeat - memorize it
I use zsh-histdb[1], which records my entire shell history for all time in a database, which also stores:

  • The start and stop times of the command
  • The working directory where the command was run
  • The hostname of the machine
  • A unique per-host session ID, so history from several sessions is not confused
  • The exit status of the command
This the working directory of the command has been especially useful for me to get the context of what I did, not only the command itself.

[1] - https://github.com/larkery/zsh-histdb

Don't really use a cheat sheet. history and fzf is mostly what i use
All kept in batch files in a directory with a meaningful name. When I need something what I do a ls <keyword> on the directory and can recon the command I need.
Try using flashcards (Anki) to remember shortcuts if you haven't yet. Just like with learning a foreign language, it's a quick way to get "action -> shortcut" mappings into your brain permanently for recall at any time, rather than relying on a cheatsheet.
This surprised me that people do this. I don't have any, since if I have to look up one, it's not really useful :)

But my cheat sheet is google, I usually find what I want in no time.

"my cheat sheet is google"

Useful information disappears from the internet all the time.

If I find something useful I record it myself so I can easily find it again instead of wasting my time searching for something that may no longer exist.

> Useful information disappears from the internet all the time.

If something is really useful it will stick around, maybe some site goes down but there will be others with the same information.

  - Find all files modified in the last 90 minutes  
    find . -type f -mmin -90  
  - Find all files and do something to each one
    find . -type f -mmin -90 | xargs ls -l  
  - Find all files modified before 2 days ago  
    find . -type f -mtime +2  
  - Removing files older than 7 days  
    find . -type f -mtime +7 -name '*.gz' -exec rm {} \;  
  - Find all files modified after last 2 days  
    find . -type f -mtime -2  
  - Find all files modified at the last 2 day mark  
    find . -type f -mtime 2
There needs to be a new version of find with a more intuitive UI. (I suspect it exists, but I can't remember the name right now)
This is fd.

    fd --newer 90min
    fd --newer 90min -x ls -l {}
    fd --older 2d
    fd -g '*.txt' --older 7d -x rm {}
    fd --newer 2d
    fd --newer 3d --older 1d
Find all .txt files containing the word 'foo': find ./ -mount -type f -name "*.txt" -exec grep -iHn 'foo' {} \;

(the -mount stops it traversing into mounted shares)

With zsh:

  grep -iHn 'foo' **/*.txt(.)
Though I'm not sure how to prevent zsh from descending in to other filesystems, as you can with find.
With zsh you don't need to use find, fd, or any other external tool.

zsh's built-in globbing features can be used instead.

  - Find all files modified in the last 90 minutes
    print -l **/*(mm-90)
  - Find all files and do something to each one
    print -l **/*(mm-90) | xargs ls -l
  - Find all files modified before 2 days ago
    print -l **/*(m+2)
  - Removing files older than 7 days
    zargs -- **/*(m+7) -- rm
  - Find all files modified after last 2 days
    print -l **/*(m-2)
  - Find all files modified at the last 2 day mark
    print -l **/*(m2)
In most of the above examples "print -l" is used to print the results to the terminal, but you can instead use whatever command you want (as in the "rm" example above).

Many more examples and tips can be seen here: [1]

[1] - http://www.zzapper.co.uk/zshtips.html

The zsh rm command doesn't with too many (say, 100 000) files, execve() fails with: Argument list too long. This works and it is relatively fast (runs rm in big batches): find ... | xargs -d '\n' rm --
You can do the same with:

  zargs -- **/*(m+7) -- rm
I updated my answer above accordingly.
For you can skip xargs for that and just add the -delete argument to find, for readability and profit(!?)
Perl is also helpful to pipe to for any tool that outputs lists of files, like

  find . -type f -name '*foo' | perl -ne 'print;chomp;unlink'
And it has the same -0 switch as xargs to pair with find's -print0.

Not helpful in this case since you have other options, but maybe helpful for things like bulk touch, chmod, etc.

Please use `find -print0 | xargs -0` (or -exec) when running commands on files, otherwise xargs will mangle filenames in some cases.
Depending on how you name your files, you want to add -print0 to your find and -o to your xargs. If you have spaces in your file names you want that to carry over in your xargs.

Secondly, you may want to add -n 200 to your xargs so if you have very, very many files bash doesn't complain.

So:

Find all files and do something to each one

    find . -type f -mmin -90 -print0 | xargs -0 -n 1024 ls -l
I feel almost embarassed for sharing some of these, but I either remember things the first time I'm exposed to them or am forever doomed to have to look them up.

    ln -s /the/target the_link (can never remember order)

    for f in *.mp4; do mv "$f" "$(echo "$f" | sed s/S1E/S01E/)"; done

    tar -theuniverse (insert xkcd here)

    grep -E 'foo|bar' file (can never remember the flag for regex)
I started remembering the order for ln when I think of it as a copy which just happens to be special. cp SRC DEST is intuitive to me so I just hang ln off that.
I did that as well, also running cp with the -s argument will create a symlink
25 years into this and i still have to look up the ln order. I know for more complex things, but this damn order won't stick in my mind.