Using Vim for Navigating The Terminal
What if you could navigate the terminal with the same efficiency as you do in Vim?
Many tools nowadays support Vim-like key bindings. When using a tool for writing, you can always check if it supports Vim key bindings. For example, I use Vim in my note taking app Obsidian among so many other apps and it’s great!
I have used the Vim key bindings in my terminal for a long time and it unifies my shortcuts in a way that I don’t need to remember too many shortcuts anymore.
In this newsletter, we will explore some of the benefits of using Vim mode in your CLI and how can you enable it.
The Problem with Default Shortcuts
My biggest problem with the default shortcuts is that they are never enough and they force me to use some of the keys in the keyboard which are a bit hard to reach. This seems trivial for some but for me, I don’t like moving my hands away from my keyboard at no time.
Just an example, I use auto-suggesting in my terminal that suggests me commands based on my history. When I want to accept a suggesting, I have to either use the right arrow key (which I hate) or modify the default key bindings to my liking. I was using CTRL + E to accept a suggesting in the past.
A few more reasons for me disliking the default terminal shortcuts:
Inconsistencies across environments: Every app has its own key bindings and they differ per app. In addition to that, some apps even use different key bindings in different platforms. For example, the key bindings between MacOS and Windows for the same application could be different.
Lack of Precision and Flexibility: For example, in your default terminal,
CTRL + E
takes you to the end of the line andCTRL + A
brings you back to the beginning. However, if you want to move to a specific location in the line, you need to use some of those keys you dislike such as the arrow keys.No “Undo” Functionality: If you screw up a command you have already typed, chances are, it’s easier to fix it using Vim than going back and forth between the words to fix them.
Use of Arrow Keys: Yes, I hate arrow keys because they break my flow. I need to move my hands away from the keyboard to access the arrow keys. With Vim key bindings, this problem is solved.
These are only a few of the reasons I dislike default terminal key bindings.
Enabling Vim Mode in Any Shell Real Quick
Enabling Vim Mode is super easy no matter what shell you use. The following command is the standard option in most popular Unix-like shells. In your terminal, type the following and press Enter:
set -o vi
Yes, that simple. Now you can enjoy Vi/Vim motions in your CLI. Keep reading for the most useful key bindings.
Enabling Vim Mode Permanently
Enabling Vim Mode permanently simple means adding the above command in our shell configuration.
ZSH is one of the most popular shells so I use ZSH as the example here, but this should work in almost all Unix-like shells. For ZSH, the configuration is ~/.zshrc
and for bash it is ~/.bashrc
. If these files do not exist in your home directory, feel free to create them.
To add the command to your config, you can use echo command to append it to the end of the file:
echo "set -o vi" >> ~/.zshrc
Once added, reload your ZSH profile:
source ~/.zshrc
There is a slight inconvenience here, which we will address next.
Differentiating the Normal and Insert Modes
In Vim, Insert Mode is when you are actually writing (inserting keys) and Normal Mode is the opposite, for example, moving up and down with j and k are done in the normal mode.
You’ll see that it’s not visually noticeable in the CLI whether you are on Insert Mode or Normal Mode.
For many, this is not a big deal, including myself. However, some may find it inconvenient.
I like to keep things simple and avoid complicating things. Moreover, I want consistency across environments and I try to avoid customisation as much as possible. Therefore, I simply use keyboard cues:
Insert Mode: If you can type immediately and see the input, you're in insert mode.
Normal Mode: If you try typing and nothing happens (or only moves the cursor), you're in normal mode.
Tactile Shortcut: To quickly check which mode you're in, press
Esc
twice. If you're already in normal mode, it will remain there. If you’re in insert mode, it will switch you to normal mode.
If you are a Oh-My-ZSH user, you may see the angular brackets change direction by default when you switch between the normal mode and insert mode.
Insert Mode:
Normal Mode:
You can definitely customise this based on your liking, but I won’t go there in this post.
Over all, here a few things to keep in mind:
Train yourself to always press
Esc
twice to ensure you're in normal mode before you start navigating or editing.Use shortcuts like
Ctrl + [
(which is the equivalent ofEsc
) if you’re more comfortable with that. It can be faster and more reliable when switching modes.
Using Vim Mode in The CLI
We can go through a few key bindings to help you faster navigate the terminal.
Navigation:
h
,j
,k
,l
: Move the cursor left, down (next line in multiline input), up, right.0
: Move to the beginning of the line.$
: Move to the end of the line.w
: Move forward by word.b
: Move backward by word.
Editing:
x
: Delete the character under the cursor.dw
: Delete the word starting from the current cursor position.d$
: Delete from the cursor position to the end of the line.u
: Undo the last action.
Inserting:
i
: Insert text at the cursor position.I
: Insert text at the beginning of the line.a
: Append text after the cursor.A
: Append text at the end of the line.
Command Mode Search:
/text
: Search for a string in your previous commands (similar to searching in Vim).
Final Thoughts
My philosophy for productively working with any tool is to Vimify it, so I don’t have to learn new shortcuts and just only master one tool - Vim.
Is it worth switching to Vim Mode in your terminal? I would say absolutely. You don’t need to permanently enable it either. You can simply set it whenever you like with set -o vi
I would suggest give it a try and see whether it enables you to navigate the terminal faster. For me, that is absolutely the case.
Happy Vimming!