Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How linux uses vi mode in shell

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how linux uses the vi model in shell, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

There are many shell 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 this writing, it is the default shell on all major Linux distributions. So, it's what I call shell. It is important to note that bash is also a quite popular option on other UNIX-like operating systems, so it may not be different from what you use (for Windows users, you can use cygwin).

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

$echo "Hello World!" Hello World!

This is a common exercise that everyone may have done. People and novices who haven't come into contact with it may not realize that the default input mode for bash shell is Emacs mode, which means that all line editing functions used on the command line will use Emacs-style keyboard shortcuts. (row editing is actually done by GNU Readline.)

For example, if you type echo "Hello Wrld!" and realize that you want to quickly jump back to a word (separated by spaces) to correct typos without pressing the left arrow key, you can press Alt+b at the same time, and the cursor will jump back to W.

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

This is done using just one of the many Emacs shortcut key combinations available to shell users. There are many other things, such as copying text, pasting text, deleting text, and using shortcuts to edit text. Using complex shortcut key combinations and remembering may seem silly, but they can be very powerful when you use longer commands or call a command from shell history and want to edit execution again.

Although Emacs keyboard bindings are good, and if you are familiar with Emacs editors or find them easy to use, there are still some people who find "vi style" keyboard bindings more comfortable because they often use vi editors (usually vim or nvim). Bash shell (again, through GNU Readline) can provide us with this functionality. To enable it, you need to execute the command $set-o vi.

Like magic, you are now in vi mode, and you can now easily edit it using vi-style key binding to copy text, delete text, and jump to different locations in the text line. This is not much different from the Emacs mode in terms of functionality, but it has some differences in how you interact with shell, which is a powerful choice depending on your preference.

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

Looking at the previous example, if you type echo "Hello Wrld!" and realize that you want to skip back a word (again, a space-separated word) to fix that typing error, you can click Esc to change from INSERT mode to NORMAL mode. Then, you can type B (that is, Shift+b) and the cursor will go back to the front as before. (for more information about vi mode, see here.) :

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

Now, for vi/vim/nvim users, you will be pleasantly surprised to find that you can always use the same keyboard shortcuts, not just when writing code or documentation in the editor. If you've never known this and want to know more, I might suggest you take a look at this interactive vim tutorial to see if there's anything you don't know about vi-style editors.

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

Set-o vi thank you for reading this article carefully. I hope the article "how linux uses vi mode in shell" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report