Shell tips collection

I am starting this thread with the goal of gathering all the useful shell tips in a single place. We have a very diverse crowd here, and it would be really great to have some knowledge trickle from the shell ninjas to people who’re only starting their shell journey.

Below is the set of tips that I have originally prepared for the DevCamp 7 in summer 2020, specifically for the Bash shell (which I use).

Please feel free to post here tips for the shell of your preference, and don’t forget to say which shell this is! :slight_smile:

Bash

shortcuts

Ctrl + R

Performs reverse search in bash history that is stored at ~/.bash_history . Reverse search means that it will be performed from the latest commands going back in time.

Press it and start typing part of command that you’ve already used before. Once the command appears in the line, you can press Enter to execute it.

For example, instead of typing nix-shell https://holochain.love every time you want to launch Holochain shell (after doing it for the first time), you can just do Ctrl + R and then type .love : chances are, your bash history only contains a single command with this substring.

Ctrl + L

Clears the currently displayed shell screen. Use it instead of typing Enter several times to visually separate your next command from previous ones.

syntax & commands

autocomplete with Tab

Most shells implement the autocomplete that is triggered by pressing Tab button. Use it to avoid typing the entire thing you wanted to type.
Things autocomplete works for:

  • shell commands
  • contents of the directory you’re in

For example, if I want to run command cat on a file really-unnecessary-long-filename.txt that is located in the same directory I’m running my shell in, I can do the following:

  1. type cat (using autocomplete on a short command like that usually gives more options to choose from than needed, so I prefer typing it manually)
  2. continue typing start of the filename: cat r and then press Tab
  3. either autocomplete will instantly complete the entire filename or I’ll get suggestions on all contents of the current directory whose name starts with r .
  4. if there are multiple suggestions, I need to type few more letters so that there’s only a single option

create multiple files with curly braces syntax

You can use curly braces to mention multiple things in the same command and avoid running this command multiple times.

For example, if I want to create empty files anchor.rs , entry.rs , validation.rs in the directory course/ I can do it like this:

touch course/{anchor,entry,validation}.rs
5 Likes

A helpful resource I’ve been learning from that may be beneficial: https://missing.csail.mit.edu/

1 Like

This is a great one! Thank you for sharing :slight_smile: