Logo QUANTIFIEDLEAP

Home Projects AboutContact
Command Line Photo by Markus Spiske on Unsplash

Command Line Settings, Enhancements and Tips

  • Roberto Rivera
  • Created: 1/12/2022
  • Updated: 1/13/2022
  • Categories: Data Science ,Development
  • Tags: Data Engineering ,Technology

As data engineers, the command line is an ever more important part of our workflow. We use it to install packages, to test API endpoints, to commit changes in Git, and much more.

Mac developments

After starting a new data engineering job at a startup, I began developing on a MacBook. The nice thing about macOS is that it is built on UNIX, which means that we can configure the command line similar to a Linux box. Since zsh is the default UNIX shell on macOS, I decided to tweak it.

The outcome was so surprisingly good, that I proceeded to switch my Linux setup from bash to zsh. The outcome of that was an even nicer, so I decided to implement my Linux zsh enhancements back onto my Mac. The virtuous circle of life!

Unfortunately with the Mac, you have to take the good with the bad. MacOS ships with an expired version of python. A quote straight from the Python team:

We have decided that January 1, 2020, was the day that we sunset Python 2. That means that we will not improve it anymore after that day, even if someone finds a security problem in it. You should upgrade to Python 3 as soon as you can.

We need to address this problem as well. Besides potentially becoming a future security issue, we want the ability to leverage the new features of Python as it evolves.

Workstations

Homebrew is a free and open-source package manager to install, update, or remove software that our mac or Linux machine may not provide, and that can make our command line experience even more powerful.

For macOS, Xcode is an integrated development environment which contains tools that Homebrew needs to function properly. We only need to install the Xcode command line tools.

First, we need to launch our Terminal app and then run the following commands:

# On a Mac, install the xcode command line tools
xcode-select --install

# Install homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# Disable homebrew analytics data collection
brew analytics off

Afterward, let's use homebrew to install some useful programs:

# Generally good idea to ensure that homebrew is up-to-date
brew update

# Install git (if us don't already have it installed)
brew install git

# bat, A modern alternative for cat
brew install bat

# exa, A modern replacement for ls 
brew install exa

# ripgrep, line-oriented search tool that recursively
# searches the current directory for a regex pattern.
brew install ripgrep

# sd, an intuitive find & replace CLI without the quirks
# or challenges of sed or awk
brew install sd

# starship, a modern, blazing-fast and customizable 
# prompt for any shell!
brew install starship

ZSH on steroids

Whenever we start zsh, we load a file (.zshrc) in our home folder where any custom zsh configurations live. It contains environment settings and command aliases that will control our command line behavior. It is important to note that when installing command line tools, changes will likely be made to .zshrc and in some cases the whole file may be backed up and replaced.

Oh-My-Zsh is a community-driven framework for managing our zsh configuration and comes bundled with thousands of helpful functions, plugins and themes.

# Install Oh-My-Zsh
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

Plug it in!

Oh-My-Zsh has over 275 plugins which add new features to our shell and enhance its functionality. In addition, we can install third party plugins as well.

# Install autosuggestions third party plugin
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

# Install syntax-highlighting third party plugin
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

# Enable our desired plugins (standard and 3rd party)
sd '^plugins=.*' 'plugins=(alias-finder git sudo z zsh-autosuggestions zsh-syntax-highlighting)' ~/.zshrc

Python

There are two steps that we need to accomplish to fix Python. First, we need homebrew to install pyenv, which we'll use to install Python 3 and then set it as default. Second, we need to update our Zsh environment.

# Step 1: setup pyenv
# Install pyenv 
brew install pyenv

# Use pyenv to install Python 3
pyenv install 3.9.9

# Set python 3 as default
pyenv global 3.9.9

# Step 2: Update our path environment via .zshrc
echo -e '\nif command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\n export PATH=$(pyenv root)/shims:$PATH\nfi' >> ~/.zshrc

Said another way

Aliases are command line shortcuts which can save us some valuable keystrokes and simplify our command line usage. Oh-My-Zsh sets up many aliases and we are going to configure a few more below. Notice that these will get saved to our .zshrc file. The following commands will append these aliases and enable our starship prompt for our command line.

cat <<- EOF >> ~/.zshrc
alias af="alias-finder"
alias ls="exa"
alias ll="ls -l"
alias la="ll -a"
alias myip="curl http://ipecho.net/plain; echo"
EOF

# Enable starship prompt
echo 'eval "$(starship init zsh)"' >> ~/.zshrc

Notice that we setup an alias for alias-finder makes it easy to find what the many aliases are. For example, we included the git plugin in our .zshrc. To find an alias for git pull, we can run the following:

af git pull

This will show us the git pull alias:

gl

Tweak it!

First, let's install some fonts from Nerd Fonts, which are developer targeted open source fonts that include glyphs (icons) and ligatures. I'm partial to the FiraCode or Monoid fonts, but there are many more good ones available.

After downloading your preferred font, open the zip file, find the font with a name similar to Monoid Retina Nerd Font Complete Mono, and double-click to open and install the font.

Finally, open the terminal and type ⌘ , to set the Terminal preferences:

  1. Select Profiles, then select a desired theme like Pro and afterward click the Default button.
  2. Text menu: under the Font section, click Change... and select the code font you installed earlier and then set your font size.
  3. Window menu: under the Window Size section, set your Columns to 150 and Rows to 40 as a reasonable starting point.

Now we should have a development friendly terminal theme and font combination. This will come in handy especially when working in git folders or working with code.

Fusion

To wrap things up, let's highlight some useful key combinations:

Alt + D # delete from the cursor to the end of the next word.
Ctrl + W # delete from the cursor to the start of the preceding word.
Ctrl + K # delete from the cursor to the end of the line.
Ctrl + U # delete from the cursor to the start of the line.
Ctrl + L # clear the terminal.

Summary

We have set up zsh as our default shell and demonstrated how to get the most out of our terminal with the Oh-My-Zsh framework and code fonts. In addition, we have configured some aliases, highlighted some short-cuts, and enhanced our command prompt which will hopefully make our development experience more fun and productive.

Search

Social Links

Categories

  • Data Science (2)
  • Development (3)

Tags

  • Analytics (1)
  • Data Engineering (1)
  • Machine Learning (1)
  • Style (2)
  • Technology (3)
© Copyright 2022. All rights reserved.
Designed and developed by Roberto Rivera.
Powered by Plenti of Svelte, Line Awesome and tailwindcss.