To CLI or not to CLI, that is the question...

Eduardo Bellido Bellido

edubxb.net

www.entredevyops.es

@edu2b

by

Some words about me

  • +10 years of experience in systems administration
  • Currently working as Systems Engineer at Schibsted Spain
  • Interested in SRE, monitoring, Continuous integration...
  • Running a Podcast with other crazy people

About this talk

Pick a powerful terminal emulator and master it's usage

First of all

Some (opinionated) suggestions

GNU/Linux: Tilix, Terminator, Konsole

Mac OS X: iterm2 obviously...

Windows: I've no idea... recommendations?

Or use tmux...

If you prefer not to be tied to a GUI app, use tmux, no worries about the terminal emulator, the cool stuff will be done by tmux

The shell is the essence when working in the terminal, so it's important to configure it properly to boost your productivity

Know your shell

The triad

Zsh

The three are powerful and pretty configurable, just read the docs, compare it and pick one, the one that suits best to you

Bash, the GNU shell

The default shell in practically every system

Config files

When bash starts as login shell:

  ~/.bash_profile

When bash starts interactively:

  ~/.bashrc

GNU Readline config file (not only for Bash):

  ~/.inputrc

Simplify things

Put (almost) all your configuration in .bashrc

Then, in .bash_profile, put:

if [[ -f ~/.bashrc ]]; then
    source ~/.bashrc
fi

Some useful options

autocd → enter directories without typing cd
cdspell → correct minor spelling errors in directory names

globstar → enable the pattern '**'
histappend → history list is appended rather than overwritten

Enable it adding the corresponding entry to your .bashrc:

shopt -s <option name>

Know your history

Don't repeat yourself

A better history

Some variables to control the history behavior:

HISTCONTROL='ignorespace:ignoredups:erasedups'
HISTSIZE=1000
HISTFILESIZE=3000
HISTTIMEFORMAT='[%F %T]  '
"\e[A": history-search-backward
"\e[B": history-search-forward
"\C-p": history-search-backward
"\C-n": history-search-forward

Filtered history search, add this lines to .inputrc file:

View your history

$ history
1 cd /tmp
2 wget https://www.kernel.org/pub/linux/kernel/v4.x/linux-4.0.4.tar.xz
3 tar xvJf linux-4.0.4.tar.xz
4 cd linux-4.0.4
5 make oldconfig
6 make -j 4
7 make modules_install
8 make install
# append history lines from current session to the history file
$ history -a

# update the current history list
$ history -r

Repeat your history (I)

!n → Refer to command line n

!! → Refer to the previous command (tip: sudo !!)

!string → Refer to the most recent command preceding the current position in the history list starting with string

^ string1 ^ string2 ^ → Quick substitution. Repeat the last command, replacing string1 with string2

Repeat your history (II)

!$ → the previous command last argument

!^ → the previous command first argument

!* → the previous command all arguments

Browse your history

C-p → Move 'back' through the history list, fetching the previous command

C-n → Move 'forward' through the history list, fetching the next command

C-r → Search backward starting at the current line and moving 'up' through the history as necessary

C-s → Search forward starting at the current line and moving 'down' through the the history as necessary (try "stty -ixon" if doesn't work)

C-g → Escape from history searching mode

Meet the "magic-space"

Expands the history commands

$if Bash
  Space: magic-space
$endif

Enable it adding these lines to your .inputrc file:

Bonus:

C-M-e → expand the line as the shell does

Movement & Edition

be faster!

Movements

C-a → Move to the start of the current line
C-e → Move to the end of the line
C-f → Move forward a character
C-b → Move back a character
M-f → Move forward to the end of the next word
M-b → Move back to the start of the current or previous word

Edition

M-Del → Delete the Word before the cursor
M-d → Delete the Word after the cursor
C-d → Delete character under the cursor
C-h → Delete character before the cursor (Backspace)

C-w → Cut the Word before the cursor to the clipboard
C-k → Cut the Line after the cursor to the clipboard
C-u → Cut/delete the Line before the cursor to the clipboard

C-y → Paste the last thing to be cut (yank)
C-_ → Undo

C-x C-e → Edit the current input using $VISUAL or $EDITOR

Aliases, Functions and Completion scripts

Save keystrokes and time!

Aliases FTW

Create aliases for common command usages, with the arguments you usually use the most

Write your own functions

If an alias is not enough, simplify complex tasks writing shell functions, there's no need to look for new commands or creating new ones

The completion scripts

A lot of commands have a completion script, that can help us a lot doing suggestions when hitting TAB, like an IDE when programming

Enabling completion

You need to "source" the completion scripts in your Bash config files (p.e. .bashrc)

 

Can be a bit tricky depending on your platform and environment

The Prompt

Keep informed after every command you run

Customize the prompt

Bash supports a plenty of options to show information about your environment, hostname, cwd, etc.

 

But luckily exists PROMPT_COMMAND, to put literally almost everything in your prompt

Improving Bash

The power of the community

Some cool projects

Bash-it

Collections of scripts, aliases, and completions for Bash, also provides some useful improvements for the prompt.

 

Powerline

Prompt improvements, written in python, a bit slow. Also supports more applications, Vim, tmux...

 

Rainbow bash!

Prompt improvements, supports plugins, currently only have a few.

 

Oh my git!

Improved git prompt, very very verbose.

 

Getting to know those weird commands

The old boys

grep   tee   cut   find   nl xargs   watch   tail   sort   diff   uniq   wc   sed   cat

curl   tr   comm   head

jq  rg  icdiff  jid  fd  hub

httpie  dbcli  pup  spark  jj 

yank  tig  exa  awless  pv

Let's go fuzzy

Meet fzf

fzf, the fuzzy finder

Official project description:

 

fzf is a general-purpose command-line fuzzy finder.

 

It's an interactive Unix filter for command-line that can be used with any list; files, command history, processes, hostnames, bookmarks, git commits, etc.

A bunch of (useful) links

Questions?

Thanks!

$ sudo applause

To CLI or not to CLI, that is the question

By Eduardo Bellido Bellido

To CLI or not to CLI, that is the question

Boost your productivity mastering the usage of the terminal, the shell, and CLI applications.

  • 2,656