Home >System Tutorial >LINUX >Comprehensively learn the Vim editor and become a 'super user' under the Linux command line

Comprehensively learn the Vim editor and become a 'super user' under the Linux command line

WBOY
WBOYforward
2024-02-11 14:12:271125browse

If you are a Linux user, then the Vim editor is one of the indispensable tools. Vim is an efficient, powerful and flexible text editor that can be used under the command line interface. However, due to its unique design and high degree of customization, some novices feel unfamiliar and confused about it, and some even give up using it. However, this article will introduce you to the basic usage methods and techniques of Vim to help you master this powerful editor.

Comprehensively learn the Vim editor and become a super user under the Linux command line

I've been using Linux for about 25 years, and a few years of Unix before that. During this time, I developed preferences for a few tools that I use daily. Vim is one of the most important tools I use.

I started using Vi when I was learning Solaris in the early 1990s because I was told it would work on any system. From my experience, this is indeed the case. I've also tried other editors and they all do the job. However, for me, Vim works best. I use it so often that I've developed muscle memory and even subconsciously hit Vim shortcuts when using other editors.

In addition, I just simply like Vim.

Many configuration files use the name Vi instead of Vim, you can run the vi command. However, the vi command is actually a link to the vim command.

Many Linux tools use editors that emulate or directly call Nano, Emacs or Vim. Other tools allow users (for example, those who have a clear preference) to use their preferred editor. To give two examples that have the greatest impact on me, one is the Bash command line, which uses Emacs by default; the other is the Alpine text mode mail client, which uses Pico by default. In fact, Pico was written specifically for the Pine mail client, the predecessor of Alpine.

Not all programs that use external editors are configurable. Some programs only use the editor specified by the developer. For those applications that are configurable, there are different ways to choose your preferred editor.

Editing from the Linux command line

In addition to actually editing text files, another tool that I often use and is closely related to editing is the Bash shell. Bash's default editor is Emacs. Although I've also used Emacs, I definitely prefer Vim. So many years ago I changed my default editor for the Bash command line from Emacs to Vim, which is more comfortable for me.

There are many ways to configure Bash. You can use a local configuration file, such as /home/yourhomedirectory/.bashrc, which only makes default modifications to your user account and not to other users on the same system. I personally prefer to make these changes global, basically to my personal account and root. If you also want to configure it globally, you can create your own configuration file and place it in the /etc/profile.d directory.

I added a file named ??myBashConfig.sh?? in /etc/profile.d. The startup files for all installed shells are stored in the /etc/profile.d directory. When starting a terminal session, each shell reads only the startup file it was prepared for, based on the filename's extension. For example, the Bash shell only reads files with a .sh extension.

alias vim='vim -c "colorscheme desert" '
# 把 vi 设置为 Bash 的默认编辑器
set -o vi
# 为所有检查 $EDITOR 变量的程序设置默认编辑器为 vi
EDITOR=vi

In this global Bash configuration file section, set -o vi sets Vi as the default editor. The -o option in this set command defines vi as the editor. For the configuration to take effect, you need to close all running Bash sessions and open new ones.

Now you can use all the Vim commands you are familiar with, including cursor movement. Just press the Esc key to enter Vim editing mode. I particularly like the ability to use ??b?? multiple times to move the cursor back multiple words.

Set Vim as the default for other programs

Some Linux command line tools and programs check the $EDITOR environment variable to determine which editor to use. You can check the current value of this variable with the following command. I ran this command on a freshly installed virtual machine to see what the default editor was.

# echo $EDITOR
/usr/bin/nano
#

By default, Fedora programs that check the $EDITOR environment variable use the Nano editor. Adding a line EDITOR=vi to myBashConfig.sh (as shown in the snippet above) changes the default to the Vi (Vim) editor. However, not all command-line programs that use external editors check this environment variable.

Edit Email in Alpine

A few weeks ago I decided that Pico was not a good fit as my email editor. I can use it, and I did for a while after switching from Thunderbird to Alpine. But I found that Pico got in the way and I was always stuck using Vim key sequences, which affected my productivity.

I saw in Alpine's user help that the default editor can be modified. I decided to change it to Vim. It's actually very easy to do.

在 Alpine 主菜单上,按 S 键进入设置,然后按 C 键进行配置。在 “编辑器设置Composer Preferences” 部分,按 X 选择 “启用外部编辑器命令Enable Alternate Editor Command” 和 “隐式启用外部编辑器Enable Alternate Editor Implicitly” 项目。在往下滚动几页的 “高级用户设置Advanced User Preferences” 部分,找到 `Editor 那一行。如果它还没有被修改的话,它应该是这样的:

Editor  = 

用光标栏突出显示 Editor 这一行,然后按回车键来编辑。将 改为 vim ,再按回车键,然后按 E 键退出,最后按 Y 键保存修改。

要用 Vim 编辑电子邮件,只需进入电子邮件正文,Vim 就会自动启动,就像 Pico 那样。所有我喜欢的编辑功能都还在,因为它实际上是在使用 Vim。甚至退出 Vim 的 Esc :wq 序列也是一样的。

通过本文的介绍,您已经了解了Vim的基本使用方法和部分高级技巧,包括创建、打开、编辑、保存文件等操作。当然,Vim是一个非常庞大和灵活的工具,还有许多其他的功能和命令需要您探索和学习。希望本文能够为您提供足够的启发和指导,并且鼓励您深入学习Vim编辑器,成为Linux命令行下的“超级用户”。

The above is the detailed content of Comprehensively learn the Vim editor and become a 'super user' under the Linux command line. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lxlinux.net. If there is any infringement, please contact admin@php.cn delete