Vim practical technology (1)

Source: Internet
Author: User
Tags windows support
Vim Introduction

As one of the most important editors in the open-source world (Emacs), VIM is favored by many developers with its powerful functions and customization capabilities. However, it may be because Vim is so powerful that it is not easy to make good use of vim. The author has gradually mastered some practical technologies in years of practical use and will introduce them to you here. -- This article does not attempt to give a comprehensive and systematic introduction to VIM, but it is not just a few moments away. Instead, it hopes to attract everyone's interest by introducing some important features and providing relevant reference information, this powerful tool is used to explore its capabilities in depth.

Next we will give a basic introduction to vim and some reference information to help readers who are not familiar with vim understand and view further information.

Unlike most other editors, after entering vim, the characters entered by default are not inserted into the edited file. Vim mode is very important. You need to know that Vim has the following modes:

  • Normal Mode: the default editing mode. Unless otherwise specified, the commands mentioned are directly entered in normal mode; you can use the ESC key on the keyboard to return to normal mode in any other mode.
  • Command mode: used to execute long and complex commands. In normal mode, enter ":" (General Command), "/" (Forward search), or "? "(Reverse search) to enter this mode; enter is required for commands in command mode.
  • Insert mode: used for text input. In normal mode, enter "I" (insert) or "A" (append) to enter the insert mode (there are other commands, for example, "C" can also enter the insert mode, but these commands have other functions ).
  • Visual mode: used to select text blocks. You can enter "v" (lower case) in normal mode to select text blocks by character, and input "v" (upper case) to select text blocks by line, or enter "Ctrl-V" to select by Square.
  • Select mode: similar to the normal Windows Editor, select text blocks. After you select a text block in either visual or selection mode, you can use "Ctrl-G" to switch to another mode. This mode is rarely used in Linux and will not be described in this article.

Vim has a complete help document. In the standard release of VIM 6.4, there are more than one hundred chapter help files with nearly 0.6 million English words. Enter vim and enter ": help "(the command entered in command mode ends with the Enter key, which is not described below) to access. When introducing the features in this article, the detailed content described in this document will only be briefly described and the application examples will be provided, and commands for accessing the corresponding Vim document will be provided.

The general release version usually carries a simple 30-minute Vim tutorial. A newbie can start learning by entering the "vimtutor" command on the command line of the operating system. In addition to the simple description above, this article does not introduce the most basic Vim commands. A newbie of VIM should familiarize himself with vim through the tutorial before continuing to read it.

We recommend that all Vim users frequently access the main Vim site [1]. In addition to the basic release, installation, and download information, the most useful content is that users can upload their own Vim scripts and write tips that they think are useful ), for use by other Vim users. At the time of writing this section, there were more than one thousand three hundred scripts on the vim site, and the number of prompts just exceeded one thousand. For scripts with serial numbers NN, is the URL directly accessed a http://www.vim.org/scripts/script.php? Script_id = nn; for the prompt that the serial number is NN, the URL for direct access isHttp://www.vim.org/tips/tip.php? Tip_id = nn.

Unless otherwise stated, the content discussed in this article applies to VIM version 6 (from 6.0 to 6.4 ). We recommend that you carefully upgrade Vim users to VIM 6.4, preferably compile and upgrade all the patch packages by yourself. Related information is available on the website. We will not repeat it here.

Chinese support

Vim supports the world's major languages, including Chinese. If you use Vim to edit Chinese characters, but Chinese characters cannot be correctly displayed, there are two possibilities: the use of VIM is incomplete and does not support multi-byte languages (multi_byte features ); second, a configuration error occurs.

Speaking of multi-language support, there are two basic concepts: one is the language encoding of files, but the internal encoding of the environment. In older operating systems, regardless of Linux or windows, these two encoding codes are the same, which means that only one encoding file can be processed at a time: either only Western encoding (Latin1, that is, ISO-8859-1 [5]), or can only process Chinese encoding (gb2312 [2]). In the new operating system, the two can be different. In Linux, it is common that the internal encoding of the Environment uses UTF-8 [6], while the UTF-8 can be encoded in the same language for Lossless conversion, this ensures the multi-language processing capability of the system. Vim follows the Unix/Linux tradition in this regard, while the internal encoding makes the UTF-8, can simultaneously process do not agree with the language encoding files.

The settings related to language encoding are listed below:

  • Environment Variable Lang (language used );
  • Environment Variable lc_ctype (internal encoding used );
  • Vim option encoding (Vim internal encoding );
  • Vim option termencoding (the encoding used by VIM to interact with the screen/keyboard );
  • Vim option fileencoding (the encoding of the file currently edited by VIM during storage );
  • Vim option fileencodings (the encoding used by VIM when opening the file );
  • Vim option ambiwidth (Processing Method for "unspecified width" characters; introduced after Vim 6.1.455 ).

If your environment only needs to process simplified Chinese, the simplest way is to use simplified Chinese for all settings. You only need to: Set lang = zh_cn.gb2312, do not set lc_ctype (same as Lang by default), do not set Vim options related to encoding (the default is determined by Lang and lc_ctype ), you do not need to set the vim option ambiwidth. That is to say, we set the language to the Chinese (zh) used by China (CN) and the encoding to gb2312 (Note: Vim does not recognize the national standard gb18030 [3] internally, so only gb2312 can be set here; see the discussion on UTF-8 below ).

However, if, according to the current practice in Linux, internal encoding will use UTF-8, there will be some additional benefits, in this case, VIM supports editing multiple files with different codes, such as simplified Chinese and Traditional Chinese (see). In addition, in this case, VIM can also support GBK [4] and gb18030 encoding and conversion. In this way, a large number of VIM options for language encoding are available. The following describes the options and recommendation settings (if applicable ):

  • Encoding = UTF-8: regardless of the file encoding, regardless of how it is displayed and entered, the encoding used inside Vim is a UTF-8; this is the foundation of international support.
  • Termencoding: depends on the actual terminal or X Window settings. For example, if you select Simplified Chinese to log on to X Window or are using cxterm [10], this option should be set to gb2312; if you log on to X Window using the default language (lang = en_US.UTF-8), or use putty [11] to remotely access the Linux machine and set the character encoding in (window-Translation in configuration) set to UTF-8, the option should be set to UTF-8. Note that using Putty to remotely connect to Linux in Windows indicates that, only when using UTF-8, putty can reliably display and input Chinese characters (the display font must be set to Chinese characters ).
  • Fileencoding: When a file is loaded, this option is set to the file encoding recognized by VIM. Therefore, the file encoding will not change during storage. The following fileencodings and fileencodings can use hundreds of encodings supported by libiconv (if the iconv feature is included in the compilation process ), related to the Chinese are gb2312, GBK, gb18030, hz-gb-2312, iso-2022-cn, big5, cp936, cp950 and so on. If you do not want to use UTF-8 as the file encoding when creating a new file, you may need to manually set this option, for example, ": Set fileencoding = gb2312 ". Note that setting this option with "set" will change the default encoding of the new file. Using "setlocal" will only affect the current file (refer ": help setlocal "). Fileencodings = ucs-bom, UTF-8, Chinese: Vim first checks whether the file starts with a BOM (byte order mark) character of Unicode [7] [8]. if yes, the rest of the file is interpreted as the corresponding unicode sequence; otherwise, try to interpret the file content as a sequence of UTF-8; otherwise, the file is interpreted as simplified Chinese (Chinese is an alias for a cross-platform simplified Chinese character set, which is equivalent to gb2312 and EUC-CN in Linux; you can also use gb2312, GBK, or gb18030 encoding as needed ). Note that this order cannot be reversed, and it is meaningless to add other encodings such as big5 and Latin1 later, because Vim cannot identify errors in 8-bit encoding, therefore, the encoding of these encoded columns will never be used.
  • Ambiwidth = double: Set all "unspecified width" characters [9] -- refer to some characters in the Unicode Character Set that are used in both eastern and western languages, for example, ellipsis, broken signs, book names, and full-angle quotation marks. In Western text, the character width is generally equivalent to ordinary ASCII characters, while in Eastern text, the character width is usually equivalent to two times of ordinary ASCII characters, therefore, the width "unknown" -- is set to double (Chinese character width ). This value is valid only when encoding is set to UTF-8 or a unicode encoding. Note that if you use Vim through the terminal, you need to make the terminal display these characters as double-width. For example, in the case of xterm [12], the option "-CJK" should be used, that is, the command "uxterm-CJK" should be used to start the Unicode X terminal that displays these characters in double width; if you use putty for remote connection, select "treat CJK ambiguous characters as wide" in the configured window-translation (SEE ).

The options to be set are usually placed in the vim resource configuration file of the user, that is, in ~ /Add the following to the vimrc file:

set encoding=utf-8
set fileencoding=chinese
set fileencodings=ucs-bom,utf-8,chinese
set ambiwidth=double

To learn more about these options, you can use ": help'options'" to view related information in the help document. You can also find the abbreviations of these options (and commands) in this article for ease of understanding, except for a few people who use the complete spelling commands such as ": E (DIT )",": S (ubstitute) "and so on, generally use complete spelling without instructions or abbreviations. For information about the configuration file. vimrc, you can use ": Help. vimrc" to view relevant information.

When using an internal encoding UTF-8, you can use the following command to edit a file other than fileencodings (which cannot be automatically recognized): "e ++ ENC = encoding file name ". For details, see ": Help ++ ENC ".

Mouse support

Both the text interface and the vim Interface Support the mouse. However, in the text interface, the mouse is not activated by default. This means that when you use the mouse on a terminal, all functions are the same as when Vim is not used, it is not affected by VIM. To activate the mouse support in the text interface, you only need to execute ": Set mouse =.

After mouse support is enabled, VIM mainly supports the following mouse operations:

  • Click to move the cursor to the clicked position;
  • Double-click the help keyword to display help information related to the keyword;
  • Double-click the word at the selected position on the normal text;
  • Drag the mouse to select the text;
  • Scroll the text in the current buffer using the scroll wheel;
  • When Editing Multiple windows, you can drag the position of the window column.

For more information, see ": Help 'mouse", ": help Mouse-using", and ": Help scroll-mouse-wheel ".

It is particularly worth mentioning that the mouse can also be used for remote access to the Linux system. If you use the X Window System, you don't have to say it. However, when you use SSH for remote connection, most terminal client programs in Linux, for example, xterm, gnome-terminal [13], later versions of konsole [14], and putty in Windows support the use of the mouse: you only need to simply start vim and execute ": Set mouse = A" (of course, you can also remove the starting colon from the preceding statement and put it in. in the vimrc file ).

Spaces, tabs, and indentation

Indentation is one of the most basic concepts for writing code. As for whether indentation uses spaces, tabs, or whether indentation exactly uses a tab, many programmers, especially programmers who are developed in windows, are easily confused. Fortunately, VIM has very complete support for these concepts and is sufficient to cope with various complex situations. The following are the main Vim options:

  • Shiftwidth (number of indented spaces );
  • Tabstop (Tab width );
  • Expandtab (whether to use spaces to replace indentation and Tab keys; Use noexpandtab to cancel settings );
  • Softtabstop (soft tab width. If it is set to a non-zero value, the number of cells moved by the tab key and backspace time mark is equal to this value, but the actually inserted characters are still controlled by tabstop and expandtab );
  • Autoindent (Auto indent, that is, the indent value of each line is equal to that of the previous line; Use noautoindent to cancel the setting );
  • Cindent (using C language indentation, auto indent is adjusted based on special characters such as "{", "}", ":", and whether the statement ends; it is automatically set when editing C/C ++ and other types of files; Use nocindent to cancel the settings );
  • Cinoptions (for details about how to indent C language, see ": Help cinoptions-values ");
  • Paste (paste mode, will cancel all the effects of the above options to ensure the subsequent operations-usually paste the code from the clipboard-keep the original code style; Use nopaste to cancel the settings ).

Below are some common combinations:

  • Shiftwidth = 4 tabstop = 4: many windows programmers will get used to this setting to make indentation equal to the tab width.
  • Shiftwidth = 4 tabstop = 8: Many Unix programmers still use the commonly used four-cell indent, but the tab width is the Standard 8.
  • Cinoptions => 4, N-2, {2, ^-2,: 2, = 2, G0, H2, P5, T0, + 2, (0, U0, W1, m1 shiftwidth = 2 tabstop = 8: Standard GNU encoding style settings, which make many minor adjustments to Vim's default C indent style, for example, "{" and "}" in the IF statement must be indented after "if", but the function definition part "{" and "}" is still aligned with the function name. Open-source software often uses this indent style.

When editing the code, a useful command is to adjust the code indent, which can easily increase or reduce several levels of indentation, and automatically use the correct spaces or tabs according to the options. You only need to use "v" to select the line of code you want to adjust, and then type "<" (or ">") to increase (or decrease) the first level of indentation; if you type a number before you type "<" (or ">"), you can specify the indentation level to increase (or decrease.

The last related command we want to discuss is ": retab ". When the expandtab option is set, this option converts all tabs into spaces. If the expandtab option is not set, use ": retab! "White space characters can be converted to tabs (which may be converted by mistake and used with caution).": retab N "can be used to reset tabstop to n, convert the consecutive blank characters containing tabs into appropriate combinations of tabs and spaces to ensure that the rows containing tabs do not look changed. For more information, see ": Help: retab ".

Modeline)

No one wants to manually enter a lot of tab and indent settings every time. However, it does not seem to be a good idea to put it in the. vimrc file: What if I edit more than one style of code? -- If you participate in open-source software projects, can you ensure that all the projects you participate in, software projects in your company, and the code style are the same? -- Vim is the first editor I have used to record code settings in files. This feature is called a mode line in Vim. In fact, what it does is to set relevant Vim options according to the vim command in the file when opening the file. The following is a pattern line embedded in the C source code:

/* vim: set tabstop=4 shiftwidth=4 expandtab: */

The Mode row has several forms. This article only introduces this form (other forms are similar, please refer to ": Help modeline "): the "/*" at the beginning of the line and the "*/" at the end of the line tell the C compiler that this is a line of comment, not part of the code. Vim can use the following "VIM: "identifies the start of a Mode line (a blank character must appear at the beginning or front of the line); followed by a string of VIM options separated by" set "and spaces ;": "indicates that the mode row ends.

This method is very simple and powerful. In addition, for the sake of security, the options in the mode line only affect the current file (": Help modeline-local"), and cannot do anything other than the setting options.

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.