


Comprehensively learn the Vim editor and become a 'super user' under the Linux command line
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.
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

Visual Studio Code (VSCode) is a cross-platform, open source and free code editor developed by Microsoft. It is known for its lightweight, scalability and support for a wide range of programming languages. To install VSCode, please visit the official website to download and run the installer. When using VSCode, you can create new projects, edit code, debug code, navigate projects, expand VSCode, and manage settings. VSCode is available for Windows, macOS, and Linux, supports multiple programming languages and provides various extensions through Marketplace. Its advantages include lightweight, scalability, extensive language support, rich features and version

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.
