Xiaoxing's Notes

Powerful One-Liners

September 04, 2019
488 words
2 minutes

It drives me crazy when I see people smashing their fingers on arrow keys like a mad woodpecker just to move the cursor around. They think they are better than people who use the mouse to click the cursor to places, but they are actually worse, depending on the size of your keyboards, the location of the arrow keys is far enough to stretch your shoulder muscles, with the intense and repetitive movement, it's probably doing more damage than reaching out for the mouse. Also, it takes forever to move the cursor from the end of a relatively long command (especially with file path in it, say a scp command) all the way to the beginning just to add sudo. That's why we have the fuck. If you are one of that kind of people, today is your lucky day, because I am here to save your shoulders and precious time.

Command Line Cheat Sheet

See the footnotes for a legend of the keys[1].

command effect
⌃-a move to start of current line
⌃-e Move to the end of the line.
⌥-left or M[2]-f move one word to the left
⌥-right or M-b move one word to the right
⌃-u delete everything before the cursor

These are my most frequently used ones, you have to remember them, they are so helpful.

Wait a Minute, This is Emacs

Yes, it's mimicking the emacs keybindings, I think you can even make it follow vim convention if you prefer that. I am more familiar with vim key bindings, I can not live without evil-mode in my Emacs. But vim's modal editing doesn't make a lot of sense here for me since you don't have a mode indicator. Turns out, there's a hidden program doing all these powerful things in the most unobtrusive way - readline.

About readline

readline was part of bash, but taken out later as it grows over the years. It was confusing to me when I tried to learn more about it.

➜ man readline
No manual entry for readline

After googling around. It made sense to me, of course, it's part of bash. There's a whole section for it.

➜ man bash
  This  is  the library that handles reading input when using an interactive
  shell, unless the --noediting option is given  at  shell  invocation.   By
  default,  the  line editing commands are similar to those of emacs.  A vi-
  style line editing interface is also available.  To turn off line  editing
  after  the  shell is running, use the +o emacs or +o vi options to the set
  builtin (see SHELL BUILTIN COMMANDS below).

For advanced usage, checkout the man page, also this article Things You Didn't Know About GNU Readline is a both informative and a pleasure to read through. Highly recommended.

Progressive Disclosure

readline is the most progressive design (that I know of) to date. You know how we love Apple's simplistic design of hiding away intimidating features, but when you need them you can find them? This is an extreme case of said design principle. Millions of people are using it everyday for decades, taking advantage of it to boost productivity constantly, yet rarely anyone know the name of readline, not to mention the only person behind it - Chet Ramey. This is the best of progressive design. I hope we have more Chets walking the earth.



In case you are not a mac user, here is the legend.

key mac windows
Control Ctrl
Option Alt

M means Meta, since it's fiddly to make Meta key work, I choose ⌥-left/right for that. Still beats moving one character at a time.

Read more stories about "productivity" ->