Build your own Vim in Linux

Many things in the Linux system exist in the form of script codes and configuration files. When using the Linux system, these files need to be edited frequently. Obviously, if there is no text editor, the road to the world is hard to move.

My choice is Vim. Vim is the most famous text/code editor on Linux systems, and an enhanced version of the Vi editor in the early years. It is known as the most outstanding classic ancient artifact among text/code editors. The reason why it has such a reputation, I think, are mainly due to the following reasons: 1. It is ancient and mysterious, with a steep learning curve and difficult to control, but once learned it will benefit a lot. It is now regarded as a dedicated editor for masters and geeks. So although Vim is already an antique-level software, there are still countless newcomers facing difficulties to learn and use. 2. Lightweight and needless to say. When only simple editing of a certain text file (such as script code, configuration file) is required, of course the lighter the tool used, the better. Vim is also a great tool for reading text files. 3. Classic and ingenious editing methods and powerful functions. It uses the keyboard throughout the operation. Users must remember a bunch of shortcut keys, key combinations and various commands before they can start using it. Once they are proficient, the editing efficiency is far superior to other editors, and it has various plug-ins, syntax highlighting, and code completion , Color scheme and other extremely rich resources and flexible configuration;

I mainly use Vim in the following situations: 1. Used to modify the configuration file of the server system; In many cases, Linux as a server will not open the graphical interface. When remote operation is required, only the SSH "command character type" can be used Shell interface to operate it, in this case, Vim is naturally the first choice. 2. Used to read the kernel source code and system script; the reason is the same as above. 3. Used to write simple programs, usually single-file programs, or multi-file projects with only a few files, such as doing exercises when learning a programming language, and like to see the folders clean , I don’t like a lot of files similar to .project; 4. Install B, colorful full-text character interface, very cool.

Scenarios that are not suitable for Vim: 1. For large programming projects, it is best to use IDE for large projects. If you want to remotely connect to a server without a graphical interface to write a large project, it is best to build a code repository on the server; reason: VIM does not Good project management function, VIM's automatic completion is only based on text matching, and there is no semantic analysis function of programming language. 2. The content input requirements are not high, but it is often necessary to set the display format, and there are a lot of pauses between each action, and other GUI editors (such as LibreOffice Writer) are more efficient than VIM. 

Some people say that Vim is a "successful plug-in, a failure plug-in". Vim's flexible configuration and rich plug-ins greatly enhance its functions, but also greatly increase the difficulty for us to manage Vim. First of all, before using Vim, it must be properly configured, and Vim's configuration is extremely cumbersome. So there are many articles such as "Universal Configuration" and "Strong Configuration" on the Internet. You can refer to these articles. However, some of these articles have many errors, and even the content is repeated and conflicted. Moreover, these articles are "teaching people to fish, not teaching people how to fish." Although the configurations they give can be copied, they are often unclear. . Vim has a wealth of plug-ins. There are many articles on the Internet that use various plug-ins to configure Vim into a powerful IDE. However, the management of Vim's plug-ins is very difficult, because every time you install a plug-in, you need to put the plug-in file separately under the Vim installation path. Plugin, autoload, syntax, doc and other directories. Fortunately, Vundle was born. It not only solves the problem of Vim plug-ins scattered in the file system, but also can automatically search and download plug-ins.

 

Install and configure Vim

Of course, you need to install it before using Vim, but this is not a problem in Ubuntu, and it can be done with a sudo aptitude install command. Since Vundle search and download plug-ins require git, you need to install vim, gvim and git at the same time, as follows:

sudo aptitude install vim
sudo aptitude install gvim
sudo aptitude install git


Then, create a .vim directory in the user's home directory, and create a bundle directory in the .vim directory. All plug-ins automatically downloaded by Vundle will be stored here. Then, enter the Bundle directory and use the git clone command to download vundle.

Below I start to show my configuration file (.vimrc file in the home directory). My configuration is different from the "universal configuration" and "strongest configuration" circulating on the Internet. My configuration is the "simplest configuration" that can achieve the effect I want. The figure below is lines 1 to 17, where Vundle is configured, and Vundle is used to manage plugins. In some configurations on the Internet, the Vundle configuration is often put at the end of the configuration file, which is wrong. The configuration of Vundle must be placed first. (If you don’t use Vundle, you need to put the configuration first when you use the Pathogen plugin.) This is because, only after the Vundle plugin is running, Vim will load other plugins in the ~/.vim/bundle directory. Run, Vim can't find the downloaded plug-in of course.

Lines 18 to 44 are shown in the following figure. This is a screenshot taken in GVim. By the way, it shows the effect of GVim:

Lines 45 to 68:

Lines 69 to 86:

As you can see from the screenshots, my configuration file is very concise, and the various settings are roughly classified into categories, and simple comments have been made. This configuration file only carries out the most basic settings for Vim, does not contain any keyboard mapping, nor does it contain any script code. I use different color schemes for Vim and GVim. Vim uses the classic desert, while GVim is solarized, which is highly respected by many people on the Internet, and the color scheme is automatically downloaded using Vundle.

 

Use Vundle to manage plugins

Vundle can automatically download and install plug-ins, just use the Bundle command after the comment "My Bundles Here" to list the plug-ins you need, one line for each plug-in, and then run the :BundleInstall command. Vundle supports github.com and Vim's official website vim.org. In the previous configuration file, the solarized color scheme is the plug-in downloaded by Vundle from github.com. Next, let's see how to download a plug-in that simulates the Matrix from vim.org. As shown in the figure below, add a line of configuration to .vimrc:

After saving the configuration file and restarting Vim, use the :BundleInstall command to see the automatic download and installation of plug-ins:

Enter the :Matrix command, you can see the following effects:

Of course, the plug-in is pure entertainment. It is better to delete it after the entertainment. To delete a plugin, first delete or comment out the Bundle'matrix.vim--Yang' line from the configuration file, as shown below:

Then save the configuration file, restart Vim, and run the :BundleClean command, as shown below, the effect of deleting a plug-in:

 

How to learn to use Vim

You must do some homework before using Vim, otherwise you won't be able to play at all, and Vim will only make noises, making people confused. Fortunately, Vim has a great help system through which you can learn Vim very conveniently. For example, in the configuration file shown above, I didn't explain too much, because if you want to understand the function of a certain setting, you only need to :help. Let's take the guioptions setting as an example to understand the usage of this help system. As you can see from my screenshots, my GVim removes the toolbar, status bar, menu, and scroll bar, and only retains the editing interface. To achieve this effect, you need to use set guioptions=... to set it. To have a deeper understanding of guioptions, you can use the :help guioptions command, as shown below:

The help document that pops up:

If you want to learn Vim systematically, you can use the :help command without parameters, as shown below:

There are many Vim commands. Almost every key on the keyboard has a specific purpose. Be sure to take notes when learning Vim. Below, I will share with you a Vim keyboard layout:

(The picture is very large, you can save it for viewing, or open it in a new window for viewing.)

For tips on using VIM, see here http://vim.wikia.com/wiki/Vim_Tips_Wiki

 

Use Vim to read the Linux kernel source code

A practical demonstration of how to use Vim to read the Linux kernel source code. If you want to do well, you must first sharpen your tools, so you need to install the appropriate plug-ins before you start working. I mentioned earlier that using Vundle can automatically manage plug-ins, but I didn't talk about which plug-ins are available in Vim. You can go to Vim's official website to see which plugins have relatively high download rates. The address is here: http://www.vim.org/scripts/script_search_results.php?order_by=downloads

The picture above is a screenshot of the top 15 plugins. If you just read the source code, I think the two plugins taglist.vim and The NERD tree are enough. The NERD tree is a powerful tool for browsing the directory tree, and taglist.vim is a powerful tool for browsing the code, which can display the source code very conveniently. The various types, variables, functions and other information defined in can also be easily jumped between various symbols. taglist.vim must be used with the tags file. The Linux kernel source code is in C language. You can use the ctags command to generate the tags file. If you want to make Vim a powerful IDE, you need other functions, such as auto-completion, auto-generation of code blocks, auto-comment, auto-make, auto-run/debug, etc. I will introduce you when you need to use related functions in the future The corresponding plug-in. Here, it is only Read Only, I think taglist.vim and The-NERD-tree are sufficient.

First of all, use the method introduced in the previous article " Methodology for Playing with Linux Systems " to find the Linux kernel source code and install it:

Then find out where they are in the file system:

taglist.vim needs the support of the tags file, and the tags file needs to be generated using the tags command, so you need to install exuberant-ctags, as shown below:

Then, install the plugin for Vim. As can be seen from the figure below, in addition to taglist.vim and The-NERD-tree, I also installed a.vim, which can easily jump between .c files and .h files. When there is a space in the plug-in name, replace it with "-". For example, the The NERD tree plug-in needs to be written as "The-NERD-tree" in the configuration file. Configure the plug-in as shown below:

Install the plug-in as shown below:

Then, enter the installation directory of the Linux kernel source code package, unzip the Linux kernel source code, and generate the tags file, as shown in the figure below: (These two steps require a little patience, because the two operations of decompression and generation of tags take time .)

Before reading the code, you need to tell Vim which tags file to use. Here, it is the file generated just using ctags, which can be set by :set tags=/usr/src/linux-source-3.13.0/tags. For convenience, I put this setting in the .vimrc configuration file, as shown below:

Finally, start Vim, find the corresponding code to browse. If you are editing a directory (such as the following :e. Command, which means to edit the current directory), the interface of The-NERD-tree will automatically pop up. It is very convenient to navigate between directories, as shown in the following figure:

Take the implementation of the red-black tree in the Linux kernel as an example. Use The-NERD-tree to locate the rbtree.c file in the lib directory, and press Enter to open the code. Enter the :TlistToggle command to open the Taglist window, as shown below:

When browsing in the source file, use the Ctrl+] shortcut key to easily jump between the use and definition of symbols. The screenshot above is the effect after the console is fully screened. Isn't it cool?

Let's take a look at the effect of reading the kernel code in GVim:

The picture above is the effect of maximizing the GVim window. The dark theme of Solarized and Ubuntu's own purple theme do not seem to be very harmonious, so I hide the launcher on the left side of Ubuntu in the system settings. Ubuntu can not only hide the launcher on the left, but also the Gvim menu and the system status bar are merged together, which increases the working area, and it is very comfortable to read the code.

Regarding the two plugins The NERD tree and taglist.vim, if you want to know more advanced usage about them, you can use the :help NERD_tree.txt and :help taglist.txt commands to view their own help files, as shown in the following two pictures:


The above screenshots are all effects under the graphical interface. What would it look like in a full-character interface?

Take a screenshot under the full-character interface of CentOS 5.10. If it is the default 80×25 character interface, the working area is too small, and it is often impossible to display a whole line of code. So I turned on Framebuffer and worked at a resolution of 1024×768. The color scheme in the figure below is the effect of desert, background=dark:

Still very beautiful, isn't it? This is also my favorite working environment. But now it is hard to find a 1024×768 4:3 monitor. For today's high-definition widescreen displays, the dot pitch is too small and the screen is too large, and the eyes will be very difficult.

The following figure is the effect when using the solarized color scheme and background=dark:

The contrast is stronger than desert, and it's decent.

If you set background=light in the solarized color scheme, you won’t be able to see at all:

It can be seen that solarized is a color scheme specially designed for GUI. The same color scheme has different effects in the graphical interface and in the character interface.

In addition to the classic desert, there is also a classic color scheme, evening, the effect is shown in the following figure:

 

Guess you like

Origin blog.csdn.net/daocaokafei/article/details/113822544