How To Use Vim Mode On The Command Line In Bash
brandon_wallace

brandon_wallace @brandonwallace

About: I am a software developer that focuses on creative web applications. I am here to bring you in-depth step-by-step articles.

Location:
United States
Joined:
Jun 7, 2020

How To Use Vim Mode On The Command Line In Bash

Publish Date: Jun 11 '21
77 19

Introduction

Learn how to use Vi mode on the command line in Bash.

Vim lovers rejoice.

Did you know you can use Vim mode on the command line?

After I found out you could enable Vim mode in bash
set -o vi is always the first line I add to by .bashrc.

set-vi-mode.jpg

In this article I will go over

  • Movement
  • Editing
  • Searching

By default, the Bash command line uses Emacs style keyboard shortcuts, such as CTRL+A to go to the beginning of the line and CTRL+E to go to the End of the line.

Here is a small table that shows a comparison of the Emacs style keyboard shortcuts and the Vim equivalent on the command line.

Emacs Vim Result
Ctrl+A 0 Move cursor to beginning of line.
Ctrl+E $ Move cursor to end of line.
Alt+B b Move cursor back one word.
Alt+F w Move cursor right one word.
Ctrl+B h Move cursor back one character.
Ctrl+F l Move cursor right one character.
Ctrl+P k Move up in Bash command history.
Ctrl+R j Move down in Bash command history.

Add this line to the bottom of your .bashrc file.



set -o vi


Enter fullscreen mode Exit fullscreen mode

Source your .bashrc file to make the changes take effect.



$ source ~/.bashrc


Enter fullscreen mode Exit fullscreen mode

After adding that line and sourcing the .bashrc file, you now have Vim commands available on the Bash command line.

You can perform a variety of commands Vim users are used to on the command line.

If you are a Vim user already you know about command mode and insert mode. By default, in the Bash command line you are in insert mode.

You must press ESC to enter command mode.

By the way, I am currently using Bash version 5.2.9.



$ bash --version | head -1
GNU bash, version 5.2.9(1)-release (x86_64-pc-linux-gnu)

Enter fullscreen mode Exit fullscreen mode




Movement

This is how you move around.

Out of the four Vim movement keys h j k l only h and l work to move the cursor left and right.

First press the ESC key to make sure you are in command mode.

$ Move the cursor to the end of the line.

0 Move the cursor to the beginning of the line.

move-to-end-of-line.gif

W Move to the next word using space character as the delimiter.

move-to-next-word.gif

w Move to the next word or special character.

B Move back one word using space character as the delimiter.

move-back-one-word.gif

b Move back one word or special character.

^ Move to the first non-blank character at the beginning of the line.

f<character> Find occurrence of the next character.

Examples

ft finds the next occurrence of the letter t.

find-letter-t.gif

f" finds the next occurrence of the double quote character ".

; Press semi-colon to move to the next occurrence of the character.

F<character> Performs a backward search for a character.

Examples

Ft finds the next occurrence of the letter t backward search.

find-letter-t-backwards.gif

F" finds the next occurrence of the double quote character " backward search.

; Press semi-colon to move to the next occurrence of the character.

Editing

This is how you edit text.

x Delete a single character under cursor.

X Delete the previous character.

delete-previous-character.gif

I Enter insert mode with the cursor at the start of the line.

A Enter insert mode at the end of the line to append text.

append-to-end-of-line.gif

cc Change the whole line. The whole line is deleted and puts you in insert mode.

C Change text from the cursor to the end of the line.

ea Move cursor to the end of the word and enter insert mode to append text.

y Yank (copy) word under cursor.

Y Yank (copy) from the cursor to the end of the line.

p or P Paste the text you copied.

r Replace the single character under the cursor.

R Replace the characters under the cursor as you type.
This is like pressing the insert key on the keyboard while in a document editing program such as Libreoffice or Microsoft Word.

replace-text.gif

. Repeat the last command by pressing the period character. This is one of the most useful commands in Vim.

u Undo last command. You can press this key multiple times to go back in time.

~ Toggle the case of the letter under the cursor.

dd or D To delete the whole line.

delete-whole-line.gif

dw Delete current word under cursor.

d3w Delete three words.

c2w Change two words. This deletes two words and puts you in insert mode.

y2w Yank (copy) two words.

xp Transpose letters.

transpose-letters.gif

Searching

This is how you search for a previous run command.

You can search for commands by pressing forward slash / and typing a part of a command you ran.
It will find the last command you ran with the matching string.

I want to search for the last gunicorn command so I type /gunicorn and press ENTER.

vim-search.gif

Conclusion

If you enjoy using the Vim text editor you will be happy to know that you can also use a few Vim commands on the command line.

View my .vimrc file.

Follow me on Github and DEV.to.

Comments 19 total

  • Matthieu Cneude
    Matthieu CneudeJun 13, 2021

    Nice article!

    You can do the same if you use Zsh. I've written an article how to configure Zsh without oh-my-zsh, and I speak about Zsh vi mode too: thevaluable.dev/zsh-install-config...

    • brandon_wallace
      brandon_wallaceJun 15, 2021

      Mathhieu, thank you for sharing! The article you wrote is well written and packed full of information. I saw the Vi mode part. The changing cursor depending on the Vi mode is a great idea. I see we have some of the same Git aliases. Check out my configuration articles for my .bashrc and my .vimrc.

      Bashrc Customization Guide – How to Add Aliases, Use Functions, and More

      freecodecamp.org/news/bashrc-custo...

      Vimrc Configuration Guide - How to Customize Your Vim Code Editor with Mappings, Vimscript, Status Line, and More

      freecodecamp.org/news/vimrc-config...

  • Peter Benjamin (they/them)
    Peter Benjamin (they/them)Jul 30, 2021

    For users who have built years of muscle memory using emacs-y shortcuts in bash, like ctrl-a, ctrl-e, ctrl-k, ctrl-u, ctrl-w, alt-f, alt-b ... and so on, you can have best of the both worlds by using the shortcut ctrl-x ctrl-e which will edit current bash readline in $EDITOR.

    You can see a demo of it in this blog post: dev.to/chhajedji/bash-edit-command...

  • АнонимSep 30, 2021

    [deleted]

    • brandon_wallace
      brandon_wallaceSep 30, 2021

      Thank you sir, I have updated the article to fix the issue.

  • m
    mOct 21, 2021

    "There is no up and down on the command line so j and k don't do anything."
    well they do cycle throw the history for me [bash version 5.0.17]

    • brandon_wallace
      brandon_wallaceOct 22, 2021

      Thank you. j and k do indeed move through the command history. I have updated the article to include the new information.

      I use this a lot in my .bashrc

      function hg() {
          history | grep $1;
      }
      
      Enter fullscreen mode Exit fullscreen mode

      and this to run the second to last command

      $ !-2
      
      Enter fullscreen mode Exit fullscreen mode

      Now I can use the j and k keys.

  • ab-the-dev
    ab-the-devJan 21, 2022

    I have a question tho, what does command mode do, it doesn't seem to do anything and I can't quit it.

    • brandon_wallace
      brandon_wallaceJan 21, 2022

      Thanks for leaving a comment.

      Command mode allows you to move the cursor on the command line with the movement keys. Press a, A, i, or I to exit command mode.

  • Doug B
    Doug BApr 10, 2022

    Good little description.

    BUT

    There is ONE thing. This is NOT VIM mode. It's VI mode.
    For instance you cant press escape and do ciw to change inner word. That works in vim but not in vi.

  • Md. Mohiuddin
    Md. MohiuddinNov 25, 2022

    We use CTRL+L to clear the screen. How to do it here?

    • Smeagol
      SmeagolJan 1, 2023

      While in command mode you should be able to just use ^L to clear the screen. (This is what worked for me on WSL Ubuntu 22.04.1 with bash version 5.1.16.

  • kesnar
    kesnarOct 5, 2023

    Nice article...

    But I can't seem to be able to yank and paste between different terminals....each one seems to have it's own clipboard.
    Anyway to fix/change this?

    • brandon_wallace
      brandon_wallaceOct 21, 2023

      @kesnar I believe to copy and paste between terminals you have to highlight your text with the cursor, then click CTRL+SHIFT+c to copy and CTRL+SHIFT+v to paste. You might even need to hold down the SHIFT key while you highlight your text.

  • itsgabz
    itsgabzMar 29, 2024

    I never knew the search function existed, love it, thank you very much, what a time saver.

  • Josh Duffney
    Josh DuffneyJul 27, 2024

    For anyone wondering how to copy an entire line just do y$ or y^.

    • Peter Vivo
      Peter VivoOct 29, 2024

      yy to copy full line, p for paste

Add comment