Skip to content

Instantly share code, notes, and snippets.

@onlurking
Created July 24, 2020 22:42
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 onlurking/4d70d8d899553d80eb078bdb0ae9bd95 to your computer and use it in GitHub Desktop.
Save onlurking/4d70d8d899553d80eb078bdb0ae9bd95 to your computer and use it in GitHub Desktop.

%title: Command Line Productivity %author: @onlurking %date: 2020-07-24

-> # Terminal <-

a.k.a. terminal emulator.

It's a program that opens a window and lets you interact with the shell. There are a bunch of different terminal emulators you can use.

There several examples such as:

- gnome-terminal (Gnome) - konsole (KDE) - xfce4-terminal (XFCE) - xterm (Xorg) - rxvt - cool-retro-term (this presentation)


-> # Shell <-

The shell is a program that takes commands from the keyboard and gives them to the operating system to perform.

On most Linux systems a program called bash -- Bourne Again SHell, an enhanced version of the original Unix shell program, sh, written by Steve Bourne.

There is several others shells like:

- zsh - fish - xonsh


-> # I/O redirection <-

There is two types of output in a shell:

- stdout: standard output - stderr: standard error

Let's talk about the >, which stands for output redirection:

$ cat foo.txt
foo
bar
baz

Let's redirect to another file:

$ cat foo.txt > output.txt

-> # I/O redirection <-

There is also the standard error (stderr), to where programs can send their error messages.

Let's test something:

$ cat nop.txt > output.txt
cat: nop.txt: No such file or directory

-> # File Descriptors <-

There are file descriptors for the Standard Output (stdout) and Standard Error (stderr).

To be more succinct there are “ids” that identify these two locations, and it will always be:

- 1 for stdout - 2 for stderr

cat foo.txt 1> output.txt

is the same as:

cat foo.txt > output.txt

> uses stdout as default.


-> # File Descriptors <-

So, to redirect stderr, it should be just a matter of adding the right file descriptor in place:

$ cat nop.txt 2> error.txt
$ cat error.txt
cat: nop.txt: No such file or directory

-> # File Descriptors <-

You can merge the two outputs with 2>&1.

&1 is a reference to the file descriptor 1 - stdout.

2>&1 is basically saying:

“Redirect the stderr to the same place we are redirecting the stdout”.


-> # File Descriptors <-

Let's see a practical example:

$ cat foo.txt > output.txt

$ cat output.txt
foo
bar
baz

$ cat nop.txt > output.txt 2>&1

$ cat output.txt
foo
bar
baz
cat: nop.txt: No such file or directory

-> # More redirects <-

- > - redirect stdout to file (overwrite) - >> - redirect stdout to file (append) - < - input redirect (stdin)


-> # CLI Shortcuts (readline) <-

GNU made a text editor called Emacs, it has several shortcuts for text editing.

In a later time the GNU Readline lib was created to implement the Emacs shortcuts in several shells, which also several cli apps make use of, like:

- REPLs: node.js, python, ruby, psql - Shells like: bash and zsh - Also vim, which is Emacs number #1 enemy.


-> # CLI Shortcuts (readline) <-

- Ctrl-a: Move to the start of the current line. - Ctrl-e: Move to the end of the line. - Ctrl-l: Clear the screen.

- Ctrl-w: Cut last/current word. - Ctrl-k: Cut forwards to the end of the line. - Ctrl-u: Cut backwards to the start of the line.

more info at: https://readline.kablamo.org/emacs.html


-> # Let's talk about zsh <-

Z Shell or zsh is a Unix shell with several improvements, in 2019 Mac Os Catalina replaced bash with zsh.

The first version of zsh was written in 1990.

The cool thing about zsh is it's extensibility.


-> # Oh My ZSH <-

Oh my Zsh collects third-party plug-ins and themes for the zsh.

Currently it has:

- 275+ plugins - 140+ themes - 1700+ contributors


But let's see the cool ones. :)

-> # Oh My ZSH <-

https://gist.github.com/onlurking/a9537a57600486e6f7408e73f985f4ec


\- ` theme: lambda `
\- ` zsh-autosuggestions `
\- ` fast-syntax-highlighting `

-> # CLI tools <-

Not specific to zsh, but awesome tools to have:


\- ` fzf `: https://github.com/junegunn/fzf
\- ` z.lua `: https://github.com/skywind3000/z.lua
\- ` ag (the-silver-searcher) `: https://github.com/ggreer/the_silver_searcher
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment