Linux Lakes 02: Create Your own vim

Source: Internet
Author: User
Tags set background

Many things in a Linux system exist in the form of script code, configuration files, which are often edited when using Linux systems. Obviously, if there is no text editor, the road is unable to be difficult.

My choice is vim. Vim is the most famous text/code Editor on the Linux system, and it is also an enhanced version of the VI editor of the early years, known as the best classic ancient artifact in the text/code Editor. The reason why it is such a reputation, I think mainly for the following reasons: 1. It is ancient and mysterious, the learning curve is steep, difficult to control, but once learned to benefit. is now regarded as a master, geek dedicated editor. So although Vim is already an antique-grade software, there are countless new people who are struggling to learn to use it. 2. Lightweight, needless to say, when only a text file (such as script code, configuration file) Simple editing, the use of the tool is of course the lighter the better. Vim is also a powerful tool for reading text files. 3. Classic clever editing methods and powerful features. It all the way to use the keyboard operation, the user must remember a bunch of shortcut keys, key combinations and a variety of commands to get started, once the use of proficiency in editing efficiency far beyond the rest of the editor, and with a variety of plug-ins, syntax highlighting, code completion, color scheme and other extremely rich resources and flexible configuration;

I use vim:1 mainly in the following cases. The configuration file used to modify the server system; Most of the time, Linux as a server does not open the graphical interface, when the need for remote operation, can only use the SSH "command character" shell interface to operate it, in this case, VIM is naturally preferred. 2. Scripts to read the kernel source code and system; 3. Used to write simple programs, usually single-file programs, or multiple files but only a few files of the project, such as learning a programming language when doing exercises, like to see the folder clean, do not like a lot of similar. project files; 4. b, colorful full-text character interface, Cool.

Scenarios not suitable for VIM: 1. Large programming projects, large projects or the best use of the IDE, if you want to remotely connect a server that does not have a graphical interface to write large projects, it is best to build a code warehouse on the server; Reason: Vim does not have good project management function, VIM's auto-completion is only based on text match, There is no semantic analysis function for program language. 2. The content input requirements are not high, but often need to set the display format, and each action itself has a lot of pause work, with other GUI editor (such as LibreOffice Writer) efficiency is higher than vim.

Some people say, Vim is "into also plug-ins, loser also plug-ins." Vim's flexible configuration and rich plugins greatly enhance its functionality, but it also greatly increases our difficulty in managing vim. First, before using VIM, it must be properly configured, and the configuration of vim is extremely cumbersome. So there are many articles on the internet, such as "Universal Configuration" and "strongest configuration", which can be consulted. However, some of these articles have many errors, even the content of repetition, conflict, and these articles are "to give people to fish, rather than grant people to fishing", the configuration they gave can be copied, but often make people unclear so. Vim is rich in plug-ins, there is no lack of the use of a variety of plug-ins to configure VIM as a powerful IDE, but vim plug-in management is very difficult, because each installation of a plug-in, it is necessary to put the plug-in files into the Vim installation path plugin, autoload, syntax, Doc and other directories. Fortunately, Vundle turned out, it not only solves the problem of the Vim plug-in in the file system, while being able to automatically search and download plugins.

Installing and configuring VIM

Before using VIM, of course you need to install, but in Ubuntu this is not a matter, a sudo aptitude install command can be done. Because git is required for vundle search and download plugins, I install VIM, gvim, and git at the same time, such as:

Create the. Vim directory in the user's home directory, build the bundle directory in the. Vim directory, and save the Vundle automatically downloaded plugins here. Then, go to the bundle directory and download vundle using the git clone command.

Below, start showing my profile (. vimrc file in the home directory). My configuration is different from the "Universal Configuration", "Strongest Configuration", which is circulated on the Internet. My configuration is the "simplest configuration" that can achieve the effect I want. Is line 1th to 17th, where Vundle is configured, using Vundle to manage plugins. In some configurations on the internet, it is not right to put vundle configuration at the end of the configuration file. The configuration of the Vundle must be placed at the front. (If you do not need to vundle, use the pathogen plugin also to put the configuration to the front.) This is because, only after the Vundle plug-in run, Vim will be loaded into the ~/.vim/bundle directory of other plugins, if Vundle did not run first, vim of course cannot find the downloaded plugin.

Line 18th to 44th, for example, this is a figure cut in Gvim, by the way show the effect of Gvim:

Line 45th to 68th:

Line 69th to 86th:

As you can see, my profile is concise, and the various settings are briefly divided into classes, and a simple comment is made. The configuration file only has the most basic settings for Vim, does not contain any keyboard mappings, and does not contain any scripting code. I use the vim and gvim different color schemes, Vim uses the classic desert, and Gvim is a lot of people on the Internet is respected solarized, and the color scheme is automatically downloaded using Vundle.

Using the Vundle management plug-in

Vundle can automatically download and install plug-ins, simply use the bundle command after the "My Bundles here" comment to list the required plugins, one line per plugin, and then run: 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 vundle downloaded from github.com. Below, see how to download a plugin that simulates the hack empire from vim.org. For example, add a line of configuration in. VIMRC:

After saving the profile and restarting Vim, use the: Bundleinstall command to see the automatic download and installation of the plugin:

Input: Matrix command, you can see the following effect:

Of course, the plugin is purely entertaining. It's better to delete after the entertainment. To remove a plugin, first delete or comment the bundle ' Matrix.vim--yang ' from the configuration file, such as:

Then save the configuration file, restart Vim, run: Bundleclean command, such as, delete a plugin after the effect:

How to learn to use VIM

Use Vim before you have to do some homework, otherwise you can not play, VIM will only drip drip noise, let a person confused. Fortunately, Vim has a great help system that allows you to learn vim very easily through this help system. As shown in the previous configuration file, I did not do too much explanation, because if you want to understand the role of a setting, only need to: Help a bit. The following is an example of guioptions settings to understand the use of this Help system. As can be seen from my, my gvim is to remove the toolbar, the status bar, menu, scroll bar, only the editing interface, to achieve this effect, you need to use the set guioptions= ... To set. To have a deeper understanding of guioptions, you can use: Help guioptions commands, such as:

Pop-up Help documentation:

If you want to learn the system of vim, you can use the help command without parameters:

Because of the many commands of vim, almost every keystroke on the keyboard has a specific purpose, so be sure to take notes when learning vim. Below, I will share a vim keyboard layout diagram for you:

(The picture is very large and can be saved for viewing or opened in a new window.) )

Vim's tips for using, see here Http://vim.wikia.com/wiki/Vim_Tips_Wiki

Browse the Linux kernel source code using VIM

Let's look at the actual combat, using VIM to browse the Linux kernel source code. 工欲善其事, its prerequisite, I've talked about using Vundle to manage plugins automatically, but not what plugins are available in Vim. You can go to Vim's official website to see which plugins download rates are higher than before. Address here: http://www.vim.org/scripts/script_search_results.php?order_by=downloads

is the top 15 plug-in. If you just read the source code, I think Taglist.vim and the-nerd-tree these two plug-ins is enough, The-nerd-tree is a tool to browse the directory tree, Taglist.vim is a tool to browse the code, you can easily display the source code defined in the various types, variables, functions and other information, It is also very convenient to jump between the individual symbols. Taglist.vim must cooperate with the tags file, Linux kernel source code is C language, you can use the Ctags command to generate tags files. If you want to make vim into a strong IDE, but also need other functions, such as auto-completion, automatic generation of code blocks, automatic comments, automatic make, auto-run/debug, and later need to use the relevant features when I introduce the corresponding plug-ins. Here, just to browse the source code, I think Taglist.vim and the-nerd-tree enough.

First, use the methodology described in the previous article, "How to play Linux systems," to find the kernel source code for Linux and install it:

and find out where they all are in the filesystem:

Taglist.vim need tags file support, and tags files need to use the tags command to generate, so need to install exuberant-ctags, such as:

Unzip the Linux kernel source code, and generate tags files, such as: (both steps need a bit of patience, because the decompression and build tags both operations take time.) )

Below, install the plugin for vim. As you can see from the figure below, I've installed A.vim in addition to Taglist.vim and The-nerd-tree, which makes it easy to jump between. c files and. h files. When the plug-in name contains spaces, "-" instead, as The-nerd-tree. Configure plug-ins such as:

Install plugins such as:

Before browsing the code, you need to tell vim which tags file to use, and here is the file that was just generated using ctags, which can be set by: Set Tags=/usr/src/linux-source-3.13.0/tags. For the sake of convenience, I put this setting in the. VIMRC configuration file, such as:

Then, start vim. If you edit a directory (such as the following: E. Command, which means editing the current directory), it will automatically jump out of the The-nerd-tree interface, navigation between directories is very convenient, such as:

Here is an example of the implementation of the red-black tree in the Linux kernel. Use The-nerd-tree to navigate to the Rbtree.c file in the Lib directory and press ENTER to open the code. Enter: Tlisttoggle command to open the TagList window, such as:

When browsing in a source file, you can easily jump between the use and definition of a symbol using CTRL +] shortcut keys. The above is the console full screen after the effect, is not very pull wind?

And look at the effect of browsing the code in GVIM:

Is the effect of maximizing the Gvim window, the solarized dark theme and Ubuntu's own purple theme seem to be not very harmonious, but reading the code is very comfortable. If you're in Fedora 20, that should be refreshing (the theme for Fedora is blue).

For The-nerd-tree and Taglist.vim these two plugins, if you want to know more advanced usage of them, you can use: Help Nerd_tree.txt and: Assist Taglist.txt commands to view their own helpful documents, as shown in the two figure below:

All of these are the effects of the graphical interface. What would it look like in a full-character interface?

Under the full character interface of CentOS 5.10, the following two graphs are the effects under the desert theme:

The main problem is that the screen is too small to display a whole line of code in full.

The following two figure is the effect of using the solarized theme when Background=dark:

If you set background=light, you can't see it at all:

This shows that solarized is a color scheme designed for the GUI. This further confirms what I said in the previous article, "If you want to use the full-character interface or pull it down."

(Jingshan Ranger in 2014-06-04 published in the blog Park, reproduced please indicate the source. )

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.