Fedora Team Member: How to use vi mode in the shell

As a participant in a large open source community, more specifically, the Fedora Project, I have the opportunity to meet and discuss a variety of interesting technical topics with many people. My favorite topic is the " command line" or  the shell , because seeing how well people use the shell can give you insight into their thinking, what kind of workflow they like, and in a way, what motivates them inspiration.

Introduces the use of vi mode in command line editing.

As a participant in a large open source community, more specifically, the Fedora Project, I have the opportunity to meet and discuss a variety of interesting technical topics with many people. My favorite topic is the "command line" or the shell, because seeing how well people use the shell can give you insight into their thinking, what kind of workflow they like, and to some extent what motivates them inspiration. With many devs and ops folks openly sharing their "dot files" (common slang for their shell configuration files) on the Internet, this will be an interesting collaborative opportunity for everyone from those with experience with the command line to Learn tips and tricks and share shortcuts and productivity tricks with others.

Today I'm here to introduce you to the vi mode in the shell.

There are many shells in the vast ecosystem of computing and operating systems. However, in  the Linux  world, bash has become the de-facto standard and, at the time of writing, is the default shell on all major Linux distributions. So it's what I call a shell. Note that bash is also a fairly popular option on other UNIX-like operating systems, so it's probably not too different from what you're using (for Windows users, cygwin is fine).

When exploring a shell, the first thing to do is to enter a command into it and get the output, like so:

$ echo "Hello World!"
Hello World!

This is a common exercise that probably everyone has done. People who have never been in touch and novices may not realize that the default input mode of the bash shell is Emacs mode, which means that the line editing functions used in the command line will use Emacs-style "keyboard shortcuts". (The line editing functionality is actually performed by GNU Readline.)

For example, if you type echo "Hello wrld" and realize you want to quickly jump back a word (space-separated) to correct a typo without holding down the left arrow key, you can press Alt+b at the same time, The cursor will jump back to w.

$ echo "Hello Wrld!"
              ^
        Cursor is here.

This is simply done using one of the many Emacs shortcut key combinations available to shell users. And much more like copying text, pasting text, deleting text, and using shortcuts to edit text. It might seem silly to use complicated shortcut key combinations and remember, but they can be very powerful when working with longer commands or when calling a command from the shell history and want to edit the execution again.

While the keybindings for Emacs are all good, and it's fine if you're familiar with Emacs editors or find them easy to use, there are still some people who feel more comfortable with "vi-style" keybindings because they use the vi editor a lot ( Usually vim or nvim). The bash shell (again, via GNU Readline) can provide us with this functionality. To enable it, the command $ set -o vi needs to be executed.

As if by magic, you are now in vi mode, and can now easily edit using vi-style keybindings to copy text, delete text, and jump to different positions in the line of text. This isn't very different from Emacs mode in terms of functionality, but it does have some differences in how you interact with the shell to perform operations, and it's a strong option depending on your preferences.

Let's look at the previous example, but in this case once you enter vi mode in the shell, you are in INSERT mode, which means you can enter commands as before, now hit the Esc key and you will be  in  NORMAL mode, you can freely browse and modify text.

Looking at the previous example, if you typed echo "Hello wrld" and realized you wanted to jump back to a word (again, space-separated words) to fix that typo, then you could hit Esc to change from INSERT mode to It is NORMAL mode. You can then type B (ie Shift+b) and the cursor will come back to the front as before. (See here for more information on vi mode.):

$ echo "Hello Wrld!"
              ^
        Cursor is here.

Now, for vi/vim/nvim users, you'll be pleasantly surprised that you can use the same shortcuts all the time, not just when writing code or documentation in the editor. If you've never seen these and want to learn more, then I might suggest you check out this interactive vim tutorial to see if there's anything vi-style editing you didn't know.

If you prefer to interact with the shell in this style, you can set it persistently by adding the following line at the bottom of your ~/.bashrc file in your home directory.

seen -o we

For emacs mode users, hopefully this will give you a quick and enjoyable look at the "other side" of the shell. Before I wrap up, I think everyone should use whatever editor and shell line editing mode makes them more productive. If you use vi mode and this article opened a new page for you, then congratulations! Get more productive now.

 

Guess you like

Origin blog.csdn.net/yaxuan88521/article/details/131622071