October 14, 2012 | Read Online
If you enjoy the command line then you don’t need to be sold on efficiency or purity of action. But even those who grok CLI are unaware of a number of ways to be more elegant accomplishing their tasks. Here are a few of my favorites.
- Remap Your CAPSLOCK Key
- Use ctrl-r For History Autocomplete
- Switch to iTerm on OS X
- Switch to Zsh
- Remap your Vim Escape Key
- Remap your Vim Leader Key
- Use vi-mode in Your Shell
- Add tmux to Your Workflow
- Synchronize Your Environments
1. Remap Your CAPSLOCK Key
When was the last time you used your CAPSLOCK key? Yeah, me too. It’s a relic, and it’s right there on the home line wasting useful real estate. Let’s make it functional by remapping it to the Control key! Here‘s how to do it in various operating systems.
Now you can stay on your home keys and hit ctrl-whatever with your pinky to the left the ‘a’ key.
2. Using ctrl-r to Search and Auto-complete from History
Many know about using the arrow keys to invoke previous commands from history, but relatively few know that you can invoke ctrl-r to be brought to a history auto-complete prompt.
Once you find what you were looking for you can hit enter and execute it or you can modify it before doing so. And now that your CAPSLOCK key is remapped, it’s easy to get in the habit.
3. Move to iTerm on OS X
iTerm isn’t necessarily an efficiency, but it’s definitely an improvement. Some features:
- Split panes
- Mouseless copy
- Paste history
- Full screen
- Unixyness
- Expose tabs
- Tagged profiles
I’ve never known anyone to switch to iTerm and then switch back. Definitely give it a go.
4. Switch to Zsh as Your Shell
Most come up using Bash as I did, and have to be shown the merits of another shell. After having multiple people rave about it I gave it a chance and haven’t gone back. Some features:
- Expand [remote] paths ve/pl/re --> vendor/plugins/redmine
- Expand variables: $PATH --> /your/full/path
- Intelligent correction: /pbulic/html becomes /public/html
- Spelling correction
- Shared command history across running shells
- Highly themable prompts
- Most of your Bash configuration will work seamlessly
- Can emulate Bash when run as /bin/sh
- Supports vim mode commands
- OhMyZsh support
The last one is critical, as ohmyzsh makes using the shell an absolute pleasure by including a massive number of plugins (rails, git, OS X, brew, etc.), over 80 terminal themes, and an auto-updater. Definitely give it a try.
5. Remap Your ESCAPE Key in Vim
Having multiple modes in vim isn’t so much of an issue; it’s more that it sucks to move between them. The ESC key is a bit out of the way, and it doesn’t need to be. The first thing I do when I stand up a new vim environment is add some mappings to my .vimrc.
inoremap jj <ESC>
6. Remap Your Leader Key in Vim
If you’re not familiar with the leader key and what it can do for you, you should start there. It basically serves as the activation key for shortcuts you can define. So you can say:
nnoremap j VipJ
This will make it so that pressing your leader key combined with the letter ‘j’ will visually select the entire paragraph and then join the lines in it.
I personally use the comma key ‘,’ as my leader since the concept is barely moving off of my home keys. So here I can activate all my shortcuts by using ‘,’, and leave insert mode by pressing ‘jj’. You can map it like so in your .vimrc:
let mapleader = “,”
7. Use vi-mode in Your Shell
Whether you’re using zsh or bash you want to be using the same muscle memory as much as possible, and if you’re a vim user that means switching your shell from Emacs mode (default) to vi mode.
This means you edit your shell commands the same way you edit inside vim:
Unsupervised Learning — Security, Tech, and AI in 10 minutes…
Get a weekly breakdown of what's happening in security and tech—and why it matters.
- b to go back a word
- dd to delete the entire line
- 0, $ to move to the line ends
- …etc.
You do this by adding the following line to your .zshrc or .bashrc file:
bindkey -v
You can also map your alternate escape key just as you do in vim (shown for zsh):
bindkey -M viins ‘jj’ vi-cmd-mode
Another added bonus here is that you navigate your history via j,k as will come natural, and you can even have your ctrl-r functionality as well by adding this to your .zshrc file:
bindkey ‘^R’ history-incremental-search-backward
8. Add tmux to Your Workflow
tmux is a terminal multiplexer, which allows you to connect to and manage multiple server-side terminal sessions. You can spin them up, connect to them, disconnect from them, and then connect to them another time or from somewhere else.
You may be familiar with a similar solution, GNU Screen, but tmux has a number of advantages over screen:
- screen is a largely dead project, and its code has significant issues
- tmux is an active project with a modern, efficient codebase
- tmux is completely client/server based whereas screen tries to emulate this behavior
- tmux supports both vim and emacs key layouts
- tmux supports autorenaming windows based on what’s running
- tmux is easily scriptable from the shell
- vertical window splitting is native in tmux, as opposed to being bolted on and buggy in screen
If you’re not using terminal multiplexing, give tmux a try and enjoy the benefits. [ UPDATE: I now have a full primer on tmux here. ]
9. Synchronize Your Environments
It’s quite annoying to have a brilliant shell and vim experience on your MBP, only to have it look and feel completely different when you SSH into your Linux boxes. Fix that by syncing up.
- Create a git repository for all your customization files, e.g., Bash, Zsh, Vim, etc. and keep it updated.
- Go to each of your systems that you use and clone that directory so that you’re working with one set of files across your environments.
- A commenter mentioned using Dropbox for synchronization as well, which seems attractive, but I’m not sure how clean that would be on headless Linux servers.
Ping me with similar tips so I can incorporate them into my workflow and add them here for others.
Related posts:
- Vim Config Update: 2019 Edition
- Consciousness is a Movie Screen Without an Audience, Theater, or Universe
- The Fundamental Differences Between Vim and Other Editors