Programming Cheatsheet

Command Line for Unix and Windows

ActionUnixWindowsNotes
Print working directorypwdpwd
Change directorycdcd
Make directorymkdir
List contentslsdir-a to include hidden files
Remove directoryrmdirOnly works if directory is empty
Remove directory and contentsrm -rf
Create filetouch
Remove filerm

Git

ActionCommandNotes
List local branchesgit branch
Prune local branchesgit fetch -p ; git branch -r | awk '{print $1}' | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk '{print $1}' | xargs git branch -dLink
Delete branch locallygit branch -d localBranchName
Delete branch remotelygit push origin --delete remoteBranchName
Reset the local branch to the most recent commit of the remote branchgit reset --hard origin/mastermaster in this case
Merge main branch into feature branchgit checkout feature && git merge main or git merge feature mainCreates a new “merge commit” in the feature branch that ties together the histories of both branches
Rebase the feature branch onto maingit checkout feature && git rebase mainMoves the entire feature branch to begin on the tip of the main branch, effectively incorporating all of the new commits in main

Docker

ActionCommandNotes
List imagesdocker image ls
List running containersdocker ps
List all containersdocker ps -a
Remove all docker thingsdocker system prune
Remove unused imagesdocker image prune
Remove non running containersdocker container prune
Get output from containerdocker logs
Start a stopped containerdocker start <container_id>
Stop container with SIGTERMdocker stop <container_id>
Stop container with SIGKILLdocker kill <container_id>
Run extra commands inside containerdocker exec -it <container_id> <command>-i makes stuff you type get set to the STDIN and back from the STDOUT and STDERR processes. -t makes it get show up on your screen in nicely formatted way
Copy file into containerdocker cp <path> <container_id:path>

tmux

Remember shift key for lots of the characters in these commands

ActionCommandNotes
Installsudo apt install tmux
Create new pane to the rightCtrl+b %
Starttmux
Create new pane belowCtrl+b "
Navigate between panesCtrl+b → Ctrl+b ← Ctrl+b ↑ Ctrl+b ↓
Close current paneexit
Create new windowCtrl+b c
Switch to window by numberCtrl+b [WINDOW NUMBER]
Rename current windowCtrl+b , + [NEW NAME] + Enter
Close windowexit when there is only one pane left
Detach sessionCtrl+b d
List background sessionstmux ls
Attach to sessiontmux attach -t [SESSION NAME]
Rename sessiontmux rename-session -t [CURRENT NAME] [NEW NAME]name is 0, 1, etc by defualt.
Create new sessiontmux new -s [SESSION NAME]tmux names it after next number
Delete sessiontmux kill-session -t [SESSION NAME]
Enter scroll mode in paneCtrl+b [q to quit mode

Unix

Various Linux and/or MacOS things that I find myself forgetting how to do a lot

Commands

ActionCommandNotes
Add a directory to start of your PATHexport PATH=<DIRECTORY>:$PATH
Get the size of of a directorydu -sh <DIRECTORY>See more
Change the ownership of a directorysudo chown -R <username> <path>
Add new useradduser <username>
Delete a usersudo deluser --remove-home <username>
Add higher privilegesusermod -aG sudo <username>

Excel

ActionMacWindowsNotes
Go to first cellCtrl+homeCtrl+homeOn a MacOS laptop this means Ctrl+fn+left arrow. On a desktop keyboard there are home up and down arrows

Visual Studio Code

ShortcutDescription
shift+alt+arrowDuplicate the line the cursor is on
cmd+alt+arrowNew cursor on line below or above current cursor
cmd+shift+LCursors on every occurance of highlighted text
cmd+gMove cursor to next occurance of highlighted text
cmd+dHighlight next occurance of highlighted text as well
ctrl+`Open integrated terminal pane

Python

F Strings

InputOutput
f"{'text':10}""text\\\\\" (\ = whitespace)
f"{"test":#>10}""######test"
f"{"test":#<10}""test######"
f"{"test":#^10}""###test###"
f"{12345:0>10}""0000012345"
f"{-12345:0=10}""-000012345"
f"{12345:010}""0000012345"
f"{-12345:010}""-000012345"
f"{math.pi:.2f}""3.14"
f"{1000000:,.2f}""1,000,000.00"
f"{1000000:\_.2f}""1_000_000.00"
f"{12345:+}""+12345"
f"{-12345:+}""-12345"
f"{-12345:+10}""\\\\\-12345" (\ = whitespace)
f"{-12345:+010}""-000012345"
f"{10:b}""1010"
f"{10:o}""12"
f"{200:x}""c8"
f"{200:X}""C8"
f"{345600000000:e}""3.456000e+11"
f"{65:c}""A" (ASCII value)
f"{10:#b}""0b1010"
f"{10:#o}""0o12"
f"{10:#x}""0xa"
f"{0.25:0%}""25.000000%"
f"{0.25:.0%}""25%"

Tags: Programming