1. Overview

Once we get used to Vim‘s powerful editing capabilities, it can be tough to settle for another text editor. In fact, when dealing with complex shell commands in the terminal, we might often wish to leverage Vim’s familiar editing functions.

In this tutorial, we’ll learn how to enable vi mode in the terminal and use Vim’s full capabilities for editing command lines.

2. Using vi Mode

By default, the Readline library uses Emacs key bindings. Therefore, any Readline program, such as Bash, interprets key commands in Emacs style unless we change this behavior.

2.1. With Bash

To use editing functionalities similar to those in Vim, we can indirectly configure Readline via Bash to make it read key commands using the vi style:

$ set -o vi

In this case, the set command instructs the shell using the -o switch (short for option) to adjust its command-line editing behavior to vi. Instantly, the current shell switches to vi mode, letting us edit commands using the key bindings available in the Readline vi library.

This effect temporarily impacts the current shell session. To make it permanent, we can simply add the command to the .bashrc or similar initialization file.

2.2. With Any Readline Program

We don’t have to rely on Bash alone to use vi key bindings. The Readline init file enables us to set the default editing mode to vi style for any program that uses Readline for input.

Let’s open or create the ~./inputrc file with our favorite (vi) editor and add a line to it:

set editing-mode vi

After saving the changes, any terminal session we invoke should automatically set its editing-mode to vi.

2.3. With Zsh

The Zsh shell doesn’t use Readline; instead, it relies on the Zle (Zsh Line Editor) module for input. So, if we want to switch to vi mode, we have to do it the Zsh way, which is through the bindkey command:

% bindkey -v

The -v option above (short for vi) instructs the current shell session to use the Zsh vi keymap. What’s more, adding this command to the .zshrc file ensures we’re using the vi-editing style every time we open up the terminal.

3. Using Vim’s Complete Key Bindings

The vi mode in Bash doesn’t support all key bindings and functionalities that usually come with Vim. Given this limitation, we might be forced to dial back on those advanced editing features and keep things simple.

If vi mode isn’t enough, we can still edit command lines directly with Vim. Thus, we have full access to all existing key bindings and plugins.

3.1. Setting Vim as the Default Text Editor

First, we should make sure the system recognizes Vim as the default text editor.

There are many methods to change and set the default editor, but we go with one of the most common ones:

$ sudo update-alternatives --config editor

There are 4 choices for the alternative editor (providing /usr/bin/editor).

  Selection    Path                Priority   Status

------------------------------------------------------------

* 0            /bin/nano            40        auto mode

  1            /bin/ed             -100       manual mode

  2            /bin/nano            40        manual mode

  3            /usr/bin/vim.basic   30        manual mode

  4            /usr/bin/vim.tiny    15        manual mode

Press <enter> to keep the current choice[*], or type selection number: 3

Here, the update-alternatives –config command outputs all the alternatives available for editor (text editors on the system). Next, it prompts us to choose which one to use by default. So, after selecting vim.basic (number 3), we hit Enter to set it as the primary editor. We might need to execute this operation as root or via sudo since it impacts the entire system.

3.2. Summoning Vim

If we ever feel the vi mode is lacking for a complex command, we can easily call Vim to edit it from the terminal. To do that, we just hit the Esc key to switch to Normal mode, and then tap v:

call vim from vi mode

When done with editing, let’s press Esc, type :wq, and hit Enter to save the changes and exit Vim:

type wq to execute command

Vim sends back the edited command to the terminal so the shell can execute it:

commad executed back in terminal

Notably, the screenshot above shows that the shell ignored the first command and executed the one that we produced with Vim.

However, it isn’t necessary to be in vi mode to pop into Vim for editing. Even if we’re using Bash in Emacs mode, we can call the default editor via Ctrl+X and Ctrl+E.

4. Conclusion

In this article, we learned how to enable vi key bindings in the terminal and how to use Vim’s full capabilities to edit and execute commands effortlessly.

In conclusion, vi-style bindings might be useful for simple command editing. However, directly using Vim is often preferred, as it enables us to use custom key bindings and plugins that we’re already familiar with.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments