6

How To Use Vim Mode On The Command Line In Bash

 3 years ago
source link: https://dev.to/brandonwallace/how-to-use-vim-mode-on-the-command-line-in-bash-fnn
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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.

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 and CTRL+E.
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.

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

set -o vi
Enter fullscreen modeExit fullscreen mode

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

$ source ~/.bashrc
Enter fullscreen modeExit 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.0.3.

$ bash --version | head -1
GNU bash, version 5.0.3(1)-release (x86_64-pc-linux-gnu)  
Enter fullscreen modeExit 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.
There is no up and down on the command line so j and k don't do anything.

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.

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

w Move to the next word or special character.

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

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.

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.

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.

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.

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.

. 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.

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.

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.

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.

Follow me on Github and DEV.to.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK