Published on

Command line productivity

The command line has become an integral part of my development workflow. I like Terminal simply because it does exactly what I ask for it. If I mess up, I know exactly how to fix it. Of course, except for rm -rf * which sometimes I run in my home folder. UIs and IDEs are great in the sense that, it hide lots of stuff behind abstractions and provide a simple interface to operate. Unfortunately, I do not like magic. UIs are sometimes better -- the human brain can perceive colors better than text. It's a person's preference to choose between UI and Terminal.

It's hard to remember and learn commands. The trick is to keep using it until you get comfortable and know what each command can do. But you don't have to remember all the commands with the handful of options that it provides.

Here I discuss some of the command line shortcuts and features that I use at my daily tech job to boost my productivity.

I use zsh primarily because of its features and extensibility. It's similar to bash but with more features!

ZOXIDE

I don't want to talk about the number of times I have to cd workspace/codebase/repo/someproject between projects and other directories. zoxide is a better cd command that remembers. It works by pattern matching the frequent and recent directory that you visited.

z is a wrapper over the command and you can use it just by typing z. When you type z repo, it checks all the directories that have the word repo in it, and just switches to that directory.

[~][00:23:04]$ z repos/dotfiles
*[main][~/repos/dotfiles][00:23:09]$ cd
[~][00:23:11]$ z dot
*[main][~/repos/dotfiles][00:23:13]$

What makes life easier is this and forget the plain old cd.

alias cd=z

Repo - https://github.com/ajeetdsouza/zoxide

Alternatives:-

  1. autojump

FZF

fzf is a command line fuzzy finder. You provide some text to it, and it parses and provides a search panel to quickly search the text. You can cat a file and pipe to fzf to search through text easily.

cat something.txt | fzf

One of the commands that I use more often is the shells reverse interactive search - The one you invoke by pressing CTRL+R. fzf can create a function to search through the command line history quickly.

fzf reverse search

Other functionalities include tab search directories, listing all running processes in the system, etc.

Repo - https://github.com/junegunn/fzf

ALIASES

Aliases are something that I cannot live without. I alias my frequent commands to one or two letters to avoid typing a bunch of words.

An example

alias gp="git push origin HEAD"
alias md="mkdir -p dir && cd dir"

zsh has an additional feature called global aliases. In bash, the alias would expand only if it is at the beginning of the command. Global alias will be expanded at any part of the command.

Let's say I want to cut the second field and extract out the unique values from a text file, I would create something like

alias -g uniqcut=" | cut -d ' ' -f 2 | sort | uniq"

# Now
$ cat a.txt
1 2 3
2 3 4
5 6 7

$ cat a.txt uniqcut
2
3
6

ZSH AUTOSUGGESTIONS

It is an extension that remembers history and can auto-complete any command that you typed earlier. When you start typing the command, it will suggest the recent command that you typed earlier with the same starting characters. An introduction to shell bindings can get you started.

Repo - https://github.com/zsh-users/zsh-autosuggestions

LOT MORE

  1. https://github.com/jesseduffield/lazygit
  2. https://github.com/ohmyzsh/ohmyzsh
  3. https://github.com/tmux/tmux
  4. https://github.com/nethish/dotfiles