Stop pressing the up (arrow) to find a previous command
Tags: tutorial, beginners, productivity, todayilearned
Published: 2020-07-11 15:20:37

Ever wonder if there are ways to be more efficient in our terminal and shell? Feeling a little clunky? Here are a few good-to-knows for those of us that have never dabbled in sys-admin or dev-ops work

---

**problem** : Do I have to push up (arrow) multiple times to find a previous command that I need to run? What if I just want to run the last command again?

**solution** : Please, for the love of anything, stop (pressing up multiple times and eyeballing)! This is ridiculously inefficient! Three things!

**-- FIRST --**  To simply run the previous command without moving off our keyboard's home row: `ctrl-p` (mnemonic for "previous") followed by the `<Enter>` key. Went past our command? `ctrl-n` (mnemonic for "next").

**-- SECOND --** If we know it's not the last command, the following is probably the utility that I rely on most: `reverse-i-search`. This utility treats our (command) `history` like a stack and (fuzzy) finds the most recent match for any term/keyword that exists in our history. To initialize it, hit `ctrl-r`. Once we've typed something it should arrive at our first match. If the first match isn't what we're looking for, then hit `ctrl-r` again and it'll go to the next previous match. And if we've gone past it, use `ctrl-s`/`<Shift>-ctrl-r` (depending on our operating system). To exit the search, use `ctrl-g`. This has almost completely eliminated all my use of aliases.

Let's say that our history looks like this. NOTE: the numbers on the left correspond to the line number in my bash history
```sh
$ history
2311  ls -l
2312  rg --files | fzf
2313  vim reverse_singly_linked_list.py
2314  python -m unittest reverse_singly_linked_list.py
2315  ping google.com
2317  mix routes
2318  mix phx.routes
2319  git branch
2320  git log -3 --reverse
2322  htop
2326  jobs
2327  git status
2328  git diff -w
2329  ./deploy.sh
2330  less README.md
2331  man awk
```

Now, I'm at my prompt and I'm searching for the last command that contained the following characters: `test`. If we eyeball above, we'll notice that it's command number `2314` that corresponds with a unit test that I ran with Python for a linked list. What most people do is press up twelve, I repeat, TWELVE, times! When hitting `ctrl-r`, followed by entering the characters, `test`, will find that command in its entirety. We can just hit `<Enter>` to invoke (run) it
```sh
                     👇 # what we type will display here
$ (reverse-i-search)`test': python -m unittest reverse_singly_linked_list.py
                                          👆 # match begins here
```

**-- LASTLY (third) --** we can "pipe" our `history` into `grep` (without the angle brackets). This will pull up ALL matches of a command that contains the given term. This is useful if we can only remember some fragments of a command AND to avoid having to hit `ctrl-r` many times when there are multiple matches.
```sh
$ history | grep <term/keyword(s)>
```

---

**problem** : What are the options for this thing? (some Unix utility) Do I have to go to Google every time?

**solution** : built-in Unix utilities (i.e. `ls`, `cd`, `rm`, `xargs`, `ps`) typically have many **options**. One of the quickest references available is the **manual**, also known as man-pages, that can simply be invoked with `$ man <utility>`, (e.g. `$ man ls`). I've found `ls -l` (long format), `ls -la` (long with directories), and `ls -lt` (long ordered by time) to be particularly useful.

---

**problem** : Okay, so I'm now using man-pages, but how do I navigate this thing or find what I'm searching for? This thing is massive!

**solution** : man-pages typically adopt [vi](https://en.wikipedia.org/wiki/Vi) movements (popular text editor found on almost all Unix/Linux-based machines including macOS). They can simply be searched by first hitting the `/` (forward slash) to initiate a search, followed by some keyword or option (e.g. `/foobar` or `/-a`) will search for `foobar` or `-a` within the page.

---

**problem** : Okay, so that search only takes me to the first matching instance. How do I find the **next** one? Or go back to the **previous** one?

**solution** : We can use `n` (lowercase and mnemonic for "next") to go to the next instance, and `<Shift>-n` (capital) to go back (to the previous instance)

---

**problem** : Is there a way to make a new directory and switch into it immediately?

**solution** : `$ mkdir foo && cd $_`. The last command argument is captured in the `_` (underscore) variable. To access variables, we use the `$` prefix

---

**problem** : Is there an easier way to switch back and forth between directories?

**solution** : Use `-`
```sh
$ pwd
/Users/me/Desktop

$ cd foo
foo $

$ pwd
/Users/me/Desktop/foo

$ cd -
/Users/me/Desktop

$ cd -
/Users/me/Desktop/foo
```

---

**problem** : How do I move around the text for a command that I've already typed or edit a previous command? How do I go to the beginning of the line or the end of the line?

**solution** : readline shortcuts - allow us to navigate within a line and perform edits. This is a huge efficiency gain. Surprisingly, most of our tools are built with readline, so these typically work in any place that allow us to type text (i.e. Chrome or Firefox address bar, Google Doc, etc) so they are worth learning even outside of a shell

**Try these out**. NOTE: on macOS, we must configure the meta keys for the option-key-based commands to work (how-to after the commands below). If we care about ergonomics a common key re-map is changing our caps-lock key to `ctrl`. This can be done through our system keyboard preferences' modifier keys (on macOS):

    alt-b / option-b (macOS): move cursor back one word
    alt-f / option-f (macOS): move cursor forward one word
    ctrl-a: move cursor to the beginning of line
    ctrl-b: move cursor backward (←)
    ctrl-d: delete a character
    ctrl-e: move cursor to the end of line
    ctrl-f: move cursor forward (→)
    ctrl-k: kill the line after the cursor, add to clipboard
    ctrl-n: next line, next command in history (↓)
    ctrl-p: previous line, previous command in history (↑)
    ctrl-u: kill the line before the cursor, add to clipboard
    ctrl-w: delete a word
    ctrl-y: undo / paste from the clipboard

**HOW-TO** set up meta keys for forward and backward using option keys in iTerm2 on macOS:
1. Go to Profile > Keys
2. Load Preset > Natural Text Editing
3. Switch Left/Right Option from Normal > Esc+