Using VI mode in the shell

Source: Internet
Author: User
Tags readline webp

As a participant in a large open source community, or rather as a member of the Fedora project, I have the opportunity to meet with many people and discuss various interesting technical topics.

My favorite topic is "command line" or shell, because understanding how people use the shell can give you a deep understanding of what they think, what kind of workflow they like, and what inspires them in a way.

Many development and OPS people share their "dot files" (common slang for their shell profiles) on the Internet, which will be an interesting collaboration opportunity for everyone to learn tips and tricks and to share shortcuts and efficient skills from people with extensive command-line experience.

Today I'll introduce you to the VI pattern in the shell.

There are many shells in the vast ecosystem of computing and operating systems. However, in the Linux World, Bash has become a de facto standard, and at the time of writing this is the default shell on all major Linux distributions. So, it's what I call the shell.

It is important to note that bash is also a popular option on other UNIX-like operating systems, so it may not be as much of a difference as you can use (Cygwin for Windows users).

The first thing to do when exploring the shell is to enter the command in it and get the output as follows:

650) this.width=650; "Src=" http://mmbiz.qpic.cn/mmbiz_png/ Ip70vic417dnjt0mkjcpia8t9iaotzfvrrzjwetxv8tucdr4hl0ulvxtekxq8baxogbsfue6umowicmoahwycrhsibw/640?wx_fmt=png &tp=webp&wxfrom=5&wx_lazy=1 "style=" Margin:0px;padding:0px;height:auto;width:auto; "alt=" 640?wx_fmt= Png&tp=webp&wxfrom=5&wx_lazy= "/>

This is a common practice that everyone may have done. People and novices who haven't been in touch may not realize that the default input mode for bash Shell is Emacs mode, which means that the line editing features used in the command line will use the Emacs style keyboard shortcuts. (The line editing function is actually performed by the GNU Readline.) )

For example, if you enter echo "Hello wrld!" and realize that you want to quickly jump back to a word (space delimited) to modify the typing error without holding down the left arrow key, then you can press alt+b at the same time and the cursor will jump back to W.

650) this.width=650; "Src=" http://mmbiz.qpic.cn/mmbiz_png/ ip70vic417dnjt0mkjcpia8t9iaotzfvrrzcfb9nqyfommuju9nw5nzmqpsywddd51qhjyiwblyulnirjz8bafyaq/640?wx_fmt=png& Tp=webp&wxfrom=5&wx_lazy=1 "style=" Margin:0px;padding:0px;height:auto;width:auto; "alt=" 640?wx_fmt=png &tp=webp&wxfrom=5&wx_lazy= "/>

This is done using one of the many Emacs shortcut key combinations provided to the shell user. There are many more 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 invoke a command from the shell history and want to edit the execution again.

Although Emacs's keyboard bindings are good, if you're familiar with the Emacs editor or find them easy to use, there are still some people who think the "VI style" keyboard bindings are more comfortable because they often use the VI editor (usually vim or nvim).

Bash Shell (again, through GNU Readline) can provide this functionality for us. To enable it, you need to execute the command $ set-o VI.

Just like magic, you are now in VI mode and can now be easily edited with VI-style key bindings to copy text, delete text, and jump to different locations in lines of text. This is not much different from the Emacs pattern in terms of functionality, but it has some differences in how you interact with the shell, which is a powerful choice depending on your preferences.

Let's take a 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 the command as before, now click the ESC key, you will be in NORMAL mode, you can freely browse and make text changes.

     look at the previous example, if you enter  echo "Hello wrld!", and realize that you want to jump back a word (again, with a space-delimited word) to fix that typing error, then you can click  Esc  Change from INSERT mode to NORMAL mode. You can then enter  b (that is,  shift+b), and the cursor can go back to the front as before. (For more information about the VI mode, see here.) ):

650) this.width=650; "Src=" http://mmbiz.qpic.cn/mmbiz_png/ Ip70vic417dnjt0mkjcpia8t9iaotzfvrrzc1qzzymohxblicqhiaz4nu3fici5ylkt9p4sflv0vz0hiaxq2nxbh6ko8a/640?wx_fmt=png &tp=webp&wxfrom=5&wx_lazy=1 "style=" Margin:0px;padding:0px;height:auto;width:auto; "alt=" 640?wx_fmt= Png&tp=webp&wxfrom=5&wx_lazy= "/>

Now, for Vi/vim/nvim users, you will be pleasantly surprised to find that you can always use the same shortcut keys, not just when writing code or documents in the editor. If you've never known this and want to know more, then I might suggest you take a look at this interactive vim tutorial to see if the VI-style editor has something you don't know about.

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

650) this.width=650; "Src=" http://mmbiz.qpic.cn/mmbiz_png/ ip70vic417dnjt0mkjcpia8t9iaotzfvrrzhrzszrztkgcsnrpfjpa0gzcfqkokzn0dlrjvqfcbhc2khewrhq9xgq/640?wx_fmt=png& Tp=webp&wxfrom=5&wx_lazy=1 "style=" Margin:0px;padding:0px;height:auto;width:auto; "alt=" 640?wx_fmt=png &tp=webp&wxfrom=5&wx_lazy= "/>

For users of Emacs mode, hopefully this will allow you to quickly and happily see the "Other side" of the shell. Before I finish, I think everyone should use any editor and Shell line editing mode that makes them more efficient, and if you use VI mode and this article launches a new page for you, congratulations! become more efficient now.

Author Profile:

Adam Miller is a member of the Fedora Engineering team, focusing on the Fedora release project. His work includes next-generation build systems, automation, RPM package maintenance, and infrastructure deployment. Adam completed a bachelor's degree in computer Science with a master's degree in information security and safety from Sam Houston State University. He is a Red Hat Certified Engineer (CERT#110-008-810) and an active member of the open source community, and has a long history of contributing to the Fedora project (FAS account name: maxamillion).

This article is from the "Marco Linux Training" blog, so be sure to keep this source http://mageedu.blog.51cto.com/4265610/1913513

Using VI mode in the shell

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.