Dorian's Developer Cheat Sheet

Configure terminal features

Add directories to cd search path

Using Zsh, it's easy to make it so the cd command searches a group of directories. Add to the ~/.zshrc config file:

setopt auto_cd
cdpath=($HOME $HOME/code $HOME/projects)

Set terminal colors

Best option: Use Zsh and Oh My Zsh

Zsh is a great Unix terminal, and Oh My Zsh makes it look pretty!

Customize your terminal manually

Linux Terminal Customization Guide for Beginners

# In the ~/.bashrc file, add a line:
PS1="\[\033[35m\]\u\[\033[39m\]@\[\033[95m\]\H \[\033[36m\]\W\[\033[39m\]$ "

Set ls colors

How to Change the Colors of Directories and Files in the ls Command

echo $LS_COLORS
LS_COLORS="rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:"

Setting up SSH

Create SSH key

# Best encryption to use
ssh-keygen -t ed25519 -C "username@host.com"
# or RSA
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

How to Configure SSH Key-Based Authentication on a Linux Server

Copy key from host to server

# Linux/Unix
ssh-copy-id -i ~/.ssh/KEYNAME.pub USER@HOST

# Windows
type $env:USERPROFILE\.ssh\SSH-KEY-FILE.pub | ssh IP-ADDRESS-OR-FQDN "cat >> .ssh/authorized_keys"

Additional references:

Windows 10 OpenSSH Equivalend of ssh-copy-id | Christopher Hart

How to set up an SSH config-file for beginners | GitHub

Set SSH config

First, create a file ~/.ssh/config:

touch ~/.ssh/config

# Open with your preferred editor.
nano ~/.ssh/config

Add your remote hosts to your config file.

# ~/.ssh/config
Host remote1
    User username
    Hostname sub.domain.com

Host remote2
    User username
    Hostname sub2.domain.com
    IdentityFile ~/.ssh/id_rsa  # optional: path to SSH private key
    LocalForward 9000 localhost:9000  # optional: port forwarding
    ForwardX11 yes  # optional: UI forwarding, if configured

If I would normally type

ssh user@sub.domain.com

I can just type

ssh remote1

Git

Configuring git tab completion

Tab completion for git should work by default in Linux/Unix. For Windows, see my post on Stack Overflow.

Setting git command aliases

git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%C(bold blue)<%an>%Creset' --abbrev-commit"
git config --global alias.co checkout
git config --global alias.sw switch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.br branch
git config --global alias.fix '!git add . && git ci --amend -n --no-edit && git push -f'

git_aliases | andreevWork on GitHub

Git remotes

# See all remotes:
git remote -v

# Add another remote:
git remote add <remote-name> <url>
# e.g.
git remote add diff-repo https://github.com/username/reponame

# Push to other remote
git push diff-repo master

# Change URI for remote repository
git remote set-url origin new.git.url/here

github - Pushing to a different git repo | Stack Overflow

Rebase a branch on top of changes merged to origin/main

This sequence of commands is useful when you want to update your working branch to have changes merged in main. Depending on the changes in the working branch and main, it is likely there will be conflicts when running the rebase command.

git fetch origin main
git rebase origin/main
git push -f origin HEAD

Set default pull behavior to rebase

git config --global pull.rebase true

Git cherry-pick

git cherry-pick {COMMIT-SHA}

# e.g. cherry picking commit 'f' from branch Feature into Main

# Initially:
#     a - b - c - d   Main
#          \
#            e - f - g Feature

git checkout main
git cherry-pick f

# Now:
#     a - b - c - d - f   Main
#          \
#            e - f - g Feature

Git reset changes on one file

Sometimes changes need to be reset on just one file to a previous commit in the git history. To do this, use the -- argument for the git command that switches to file names, as shown here.

git checkout COMMITSHA -- fileToReset.md

Git Cherry Pick | Atlassian Git Tutorial

Windows PowerShell grep equivalent

The Powershell equivalent of the linux grep command is Select-String. It doesn't work identical to grep, so it's best to learn the command directly from the docs and not to alias grep.

Using Out-String -Stream ensures that the pipe output is interpreted correctly by the Select-String program.

ls | Out-String -Stream | Select-String "some phrase"
# Use Regex
ls | Out-String -Stream | Select-String -Pattern "$r*g[a-z]x"
# Search a file
Select-String -Path myfile.txt -Pattern "words to find"

Vim Cheat Sheet

It's important to be familiar with at least one common command-line text editor, whether it be Vim, Nano, or Emacs.

Need help choosing which editor is best for you? Consider this.

My personal favorite is Vim because it's almost always installed by default and very powerful. It's complicated, though, so it's always good to have the Vim Cheat Sheet bookmarked for reference.

Softlinks

Unix

ln -s {source-filename} {symbolic-filename}

# Remove a link just like you'd remove a file:
rm {symbolic-filename}

How to: Linux / UNIX create soft link with ln command

Windows

New-Item -Path C:\LinkDir -ItemType SymbolicLink -Value F:\RealDir

# Command Prompt
mklink [[/d] | [/h] | [/j]] <link> <target>

Creating hard and soft links using PowerShell | Stack Overflow

Running a program disconnected from a session

Screen

Screen is a Linux terminal program that continues to run even after disconnecting. You can have multiple windows and go between them.

For example, if running a long Jupyter notebook, run it within screen and it will continue to run, even if your terminal session ends.

# One way: Screen  -> tty emulator
^A C # create window
^A N # next window
^A D # disconnect from screen

screen -R # reconnect to screen

Tmux

Cheat Sheet A beginner's guide to tmux | Red Had

tmux detach-client -P -s session-name-or-id

Adding GCC Versions for C++ compilation

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt update
sudo apt install gcc-11 g++-11

Set alternatives

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 100 --slave /usr/bin/g++ g++ /usr/bin/g++-10 --slave /usr/bin/gcov gcov /usr/bin/gcov-10

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90 --slave /usr/bin/g++ g++ /usr/bin/g++-9 --slave /usr/bin/gcov gcov /usr/bin/gcov-9

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 80 --slave /usr/bin/g++ g++ /usr/bin/g++-8 --slave /usr/bin/gcov gcov /usr/bin/gcov-8

Then, we run:

sudo update-alternatives --config gcc

We can select the version we want with:

Output

There are 3 choices for the alternative gcc (providing /usr/bin/gcc).

  Selection    Path            Priority   Status
------------------------------------------------------------
* 0            /usr/bin/gcc-10   100       auto mode
  1            /usr/bin/gcc-10   100       manual mode
  2            /usr/bin/gcc-8    80        manual mode
  3            /usr/bin/gcc-9    90        manual mode

Press <enter> to keep the current choice[*], or type selection number:

See directory storage size

du -sh PATH_TO_DIR

Fast high compression

### This requires 7z and tar
# Create a tar archive:
7z a ARCHIVE.tar PATH_TO_DIRECTORY

# Compress the archive using xz: (-mmt4 specifies using 4 processors)
7z a -mmt4 ARCHIVE.tar.xz ARCHIVE.tar

# Extract
tar -xvf ARCHIVE.tar.xz
# or
7z e ARCHIVE.tar.xz
7z e ARCHIVE.tar

Python

Anaconda

Consider using Mamba, a fast, robust, and cross-platform Anaconda alternative. For Mamba, replace conda with mamba in almost any conda command.

# Create conda environment
conda create -n myenv python=3.9

# Create conda environment from file
conda env create -f environment.yml

# List environments:
conda env list

# Switch environments:
conda activate {ENVIRONMENT_NAME}

# See installed packages
conda list
conda list -n myenv

# install packages with pip
conda install -n myenv pip
conda activate myenv
pip <pip_subcommand> 

# Add local packages
# (local packages must have `setup.py` file)
conda activate myenv
conda install pip
pip install ./path/to/packagedirectory

# Save environment.yml file
conda env export > environment.yml

For instructions on how to create an environment.yml file, see the Anaconda docs.

Running Jupyter

# This runs whatever jupyter notebook in the directory its run on
jupyter notebook --no-browser --port=#####

# Jupyter Lab version:
jupyter-lab 

# Connect to the remote on that port to see the notebook:
ssh -L localhost:PORT:localhost:PORT REMOTE_MACHINE_NAME

Convert between char and int

>>> chr(97)
'a'
>>> ord('a')
97

How can I convert a character to a integer in Python, and viceversa? | Stack Overflow