Skip to content

Instantly share code, notes, and snippets.

@wsandin
Last active May 6, 2022 07:55
Show Gist options
  • Save wsandin/a841131ab5fc75bb6d7767094972b48e to your computer and use it in GitHub Desktop.
Save wsandin/a841131ab5fc75bb6d7767094972b48e to your computer and use it in GitHub Desktop.
FROM ZERO TO HERO ON THE TERMINAL
FROM ZERO TO HERO ON THE TERMINAL!
W. Sandin <wsandin@gmail.com>
May 2022
..::::.. ..,;;,,.
::::::::::::. .::t$$$$$$$$bu:,. .zn$$$$k
::::d$$$$bi::::.,::::$$$$$$$$$$$$i::. ur:d$$$$$$F
::::$$$$$$$$$L::::::::3$$$$$'zec`$$$d$, ?$,"bd$$$$.
`:::3$$$$$$$$$;::::::::?$$$$ `"$$`$$$$?, 4='$b'ue$"
::::$$$$$$$$$$:::::uee$$$$$L. `?:$$$$`?k 4$bedP"
::::$$$$$"""?:::z$$$$$$$$$$$$bu$$P""-.?, zMMbu'""
`:::"'.;::::::d$$$$$$$$$C73$$$$$$; `b ,MUMMMMMMMM
ueeeeec,"i."$$$$$$$$$$$ `?$$$$$$;,.,d$F;MMMMMMMMF
,eMMMMMMMMMMMc`!;"$$$$$$$$$. "?$$$$$$$$F,MMMMMMMP"
,eM6"TMMMMMMMMMMMMb`!i;"$$$$$$$$ %, .ueeP',dMMMMMM"
zMMMMMMc'MMMMP,c,_??Mk`!!;.?$$$$F$$buued$$F,dM?MMMMM"
MMMMMMMMMP"MM"d$$(?$b'ML`!!!;`?$$$bCCCdP"; MMM'dMMP"
TMMMMMF,d"ze$$$$?be ;MML`!!!!;`?$$P",;!!'dMM"P"
."?PP,$'$$$$$$$LF";MMMMb`!!!!!i;,,;i!!'dMMF
',dMMMF
. +dMMMMMMMMMMMMMeeeeedMMMMMM"
:!!!!!! `!!!!!'''` c,"TMMMMMMMMMMMMMMMMMMPF"
:!!!!!!' `!'udNu`>."$br`TMMMMMMMMPP""",
!!!!!! :MMMMb`!!;,' ;""ii!>;i!
'!!!!' 4MMMMM, JMMMMMM`!!!!!!>' i!!'
! :!! 4MMMMMMMh..zeee' i!!'
:!!!:`MMMMMMP:MMMM"
!!!!! `TMMP,dMMP"
`''' `?bdMMP"
;!!!;.'"
;!!!!!!'
''!!'`
#1: TAB COMPLETION
==================
Tab completion is an incredibly powerful feature in the shell (terminal) that allows you to increase
the speed of typing. In short... You can NOT press TAB one time to many :) Make it a habit instead.
This also works in IPython.
EXAMPLES:
---
# Example #1
$ ls -l /u<TAB>/l<TAB>/b<TAB>
yields:
$ ls -l /usr/loca/bin
--
# Example #2
$ ls -l /etc/<TAB><TAB> # Lists all files to show you options of what you can complete
$ ls -l /etc/s<TAB><TAB>
--
# Example #3
$ git co<TAB> --am<TAB> --no-<TAB>
yields:
$ git commit --amend --no-edit
---
#2: SEARCH IN SHELL COMMAND HISTORY
===========================
Make it a habit to never repeat the same command again!
By pressing Control-R you can search amongst commands that has been previously executed.
This also works in IPython, MySQL CLI client.
EXAMPLES:
---
Shell:
Long command that we don't want to remember and keep typing in manually:
$ echo "a long story short..."
$ <CTRL-R> ec <ENTER>
yields:
$ echo "a long story short..."
---
IPython:
---
ws@maybach ~ $ ipython
[ins] In [1]: print("beep boop")
beep boop
[ins] In [2]: import pdb
[ins] In [3]: <CTRL-R> pr <ENTER>
---
#3: COLORFUL SHELL PROMPT WITH GIT BRANCH
=========================================
ALTERNATIVE #1
--
1. Type the following command to install:
$ curl -sS https://raw.githubusercontent.com/diogocavilha/fancy-git/master/install.sh | sh
2. Add the fonts in the terminal as per: https://github.com/diogocavilha/fancy-git#heavy_check_mark-after-installing
3. Change theme to whichever one you prefer: https://github.com/diogocavilha/fancy-git#art-themes-and-color-schemes
ALTERNATIVE #2
---
$ parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
<ENTER>
$ PS1='\n\[\e[32m\]\h \[\e[35m\]\u \[\e[33m\]\w \[\e[36m\]$(parse_git_branch)\n\[\e[00m\]\$ '
---
If you're happy with the prompt, add the above lines to $HOME/.bash_profile by typing the following commands:
$ cat <<EOT >> $HOME/.bash_profile
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
PS1='\n\[\e[32m\]\h \[\e[35m\]\u \[\e[33m\]\w \[\e[36m\]$(parse_git_branch)\n\[\e[00m\]\$ '
EOT
<ENTER>
---
... and restart your terminal :)
#4: FZF - Fuzzy finder for improved search
==========================================
FZF is a powerful tool to allow you to search in command line history, directory trees and more.
You can read more about it at https://github.com/junegunn/fzf. I recommend you to just install and try it.
INSTALLATION
---
$ git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
$ ~/.fzf/install
---
Try to use Ctrl+R for command line history now and you should see a much more pragmatic view.
#5: KEYBOARD SHORTCUTS
========================
NOTE: Ctrl may be replaced by Cmd on Mac OS X.
NOTE: You can find a more comprehensive list of keyboard shortcuts on: https://ss64.com/osx/syntax-bashkeyboard.html.
Moving the cursor:
Ctrl + a Go to the beginning of the line (Home)
Ctrl + e Go to the End of the line (End)
Ctrl + p Previous command (Up arrow)
Ctrl + n Next command (Down arrow)
Alt + b Back (left) one word
Alt + f Forward (right) one word
Ctrl + xx Toggle between the start of line and current cursor position
Editing:
Ctrl + L Clear the Screen, similar to the clear command
Ctrl + D Send an EOF marker, unless disabled by an option, this will close the current shell (EXIT)
Ctrl + u Cut/delete the line before the cursor position.
Alt + Del Delete the Word before the cursor.
Alt + d Delete the Word after the cursor.
Shift+Insert Paste
Alt + u UPPER capitalize every character from the cursor to the end of the current word.
Alt + l Lower the case of every character from the cursor to the end of the current word.
EXAMPLES & EXERCISES
---
$ echo "bits, bytes and kangaroos" <Ctrl+A>, <Ctrl+E> <ENTER>, <Ctrl+P>
$ echo "python pandas.. 'B' as in back, 'F' as in forward" <Alt+B>, <Alt+F>
$ echo "change my mind, let's not execute the command<Ctrl+C>
$ <Ctrl+L> # Clear the terminal screen
$ echo "test test 1 2 3" <Ctrl+x+x> and <Ctrl+x+x> (keep holding down the Ctrl button)
$ echo "jump between words with arrow keys" <press Alt+Left, Alt+Right or Ctrl+Left and Ctrl+Right to "jump between words". Can't remember which one for Mac OS X>
Exit the terminal with Ctrl+D
---
#6: ALIASES
=======
You can use aliases as a short hand for other commands. You simply put them in $HOME/.bash_profile.
EXAMPLE:
---
$ alias ipy="python3 -m IPython"
$ ipy
...
$ alias l="ls -alrt"
---
#7: ITERM2 TWEAKS
===================
- Find a colorscheme you like at https://iterm2colorschemes.com/.
- Consider a coding friendly font, I like hack for instance. https://sourcefoundry.org/hack/
- You can split the screen in iTerm with Cmd+D which is really useful. I think you use Alt and the arrow to jump between the splits.
#8: Links
==========
CLI Cheatsheets
===============
https://www.git-tower.com/blog/command-line-cheat-sheet
https://towardsdatascience.com/14-must-know-pip-commands-for-data-scientists-and-engineers-a59ebbe0a439
Collection of Terminal one-liners
=================================
https://www.commandlinefu.com/commands/browse
Interactive Learning
====================
https://lab.github.com/
https://www.hackerrank.com/
Code Style & Formatting
=======================
https://google.github.io/styleguide/pyguide.html
https://black.readthedocs.io/en/stable/
EXAMPLE OF AUTOMATED CODE FORMATTING:
$ pip install black
$ black my_program.py
Data Structures
================
https://visualgo.net/en
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment