A list of command line tips to optimize your terminal experience. All the recommended features are linked to their appropriate sources.
1. Bootstrap your terminals: Bashstrap & oh-my-zsh
Bashstrap is a quick way to spruce up your OSX terminal. It cuts out the fluff, adds in timesaving features, and provides a solid foundation for customising your terminal style.
- Faster directory navigation
- Customized bash prompt line
- Updated color scheme
iTerm is also a recommended terminal replacement for the Mac OSX default one. iTerm supports more features such as split pane view, hotkey window for instant terminal anywhere, mouseless copy. It improves working efficiency.
For zsh lovers: Check out oh-my-zsh which super-power zsh like Bashstrap to Bash.
2. Set editing-mode vi
More familiar with vim than Emacs? Did you know you can use vim instead of Emacs hotkeys on bash? (Such as $ instead of Ctrl+A for jumping to start of line)
Set editing-mode vi into this file
~/.inputrc . Then you can use vim hotkeys in the terminal.
echo 'Set editing-mode vi' >> ~/.inputrc
After setting this up, open a new tab on the terminal. Type anything on it.
This is, of course, not a command line. I would like to delete it using vim command “dd” which is “delete current line”.
All you need to do it is press “escape” on your keyboard to enter the vim mode. Then press “dd”. The whole current line will be deleted.
Here is a cheat sheet for vi by Peteris Krumins – http://www.catonmat.net/download/bash-vi-editing-mode-cheat-sheet.pdf
Byobu is a GPLv3 open source text-based window manager and terminal multiplexer. You can do window management using the terminal. Byobu was originally designed for the Ubuntu server distribution, but the Tmux terminal multiplexer works on most Linux, BSD, and Mac distributions.
Go to byobu.co to check out the demo video.
4. pushd / popd
popd are two commands for you to jump between directories like a stack. The
pushd command ‘pushes’ the target directory to the top of the stack and saves the previous directory. The previous directory can be ‘popped’ back with the
pushd [path | ..]
pushd == save current directory + optional path: change to new directory
popd == return to saved directory.
Here is an example:
I started in /Users/mayyeung/Documents/SkygearCMS directory.
pushd ~/Downloads/Skygear\ Chat\ SDK . This means I saved the path
/Users/mayyeung/Documents/SkygearCMS at the top of directory stack and at the same time I change directory to Skygear Chat SDK.
Finally, I can switch back to
~/Documents/SkygearCMS by typing ‘
If the path parameter is omitted, the path at the top of the directory stack will be used, which looks like toggling between 2 directories.
ctrl-r , you can search your terminal command line history. Sometimes you might forget the exact command, but you still remember some characters. You can then use
ctrl-r to search it.
I typed ‘brew’ and then saw the previous command lines I’d used. Then, I just press ‘enter’ to run the command line!
6. ssh config
Hate waiting for the SSH handshake and also typing username / hostname for a frequently used host? Take 5 mins to set up ssh config (~/.ssh/config), and that shortens the ssh email@example.com into
ssh w! With ssh session caching to save you from waiting for the handshake.
7. F*ck China’s GFW
Have you been behind China’s Great FireWall? Of course, there are holes. Here is a handy way to get a quick SOCKS server with SSH.
First, type the command on the terminal
ssh -c arcfour -D 8080 [server.com] . It opens a SOCKS proxy server via server.com
Then open your browser and go changing your proxy setting. By choosing the “change proxy settings”, you can check the SOCKS Proxy and type the SOCKS Proxy Server address 127.0.0.1:8080. The Great FireWall is no longer an obstacle to Google or Facebook.
8. mkdir -p what/the/hell
Most of you have opened a new directory by typing
mkdir [name of the directory]. When you need to open a new sub-directory inside a new directory, eg
~/What/the/hell, what will you do? This command comes in handy.
mkdir -p directory/subDirectory/sub-subDirectory
It’s like telling mkdir: if there is any non-existing directory along the path, just create it.
9. open . (Mac Only)
Command line for opening a Finder.
10. pbcopy / pbpaste (Mac Only)
These 2 commands provide copying and pasting to the clipboard in terminal. For example, when you need to copy a public key from a file to a setting page on your browser, you can type cat ~/.ssh/id_rsa.pub | pbcopy and then use keyword ‘command + v’ to paste into the terminal.
11. sudo !!
When we type the commands, the terminal notified you ‘Permission denied’ that you don’t have access. Then what will you do? Add sudo at the front and type command again right?
Now you have a smarter way to do that. Just type
sudo !! so that you can execute the same command without entering the whole line it again!
These 11 small hacks make my day a bit easier and hope it helps you too.
What are your favourite command hacks? Please share it in the comments! And don’t forget to share this post with your engineer friends! ^^
If you find this post interesting, subscribe to our newsletter to get notified about our future posts!