Simple for beginners. vimrc writing guide

Source: Internet
Author: User
Tags case statement delete key eol

There are many options that can be customized for users in Vim. As a beginner, we do not need to know so much about it. The following describes some of the most commonly used configuration items. You can add these items to your. vimrc as needed.

1. Basic settings
  • Set nocp

    This command allows Vim to work in incompatible mode. Before vim, a very popular editor named VI appeared. Many Vim operations are similar to VI, but many operations are different from VI. If the compatibility mode switch is enabled using the ": Set CP" command, VIM will try its best to simulate the VI operation mode.

    Many people may like the "Most orthodox VI" operation mode. For Beginners, many operations in VI are inconvenient.

    For example, in Vim, the direction keys can be used to move the cursor in insert mode, while in VI, the cursor cannot be moved in insert mode. You must use ESC to return to normal mode.

    For another example, you can use the U command in VI to cancel a previous operation. When you press u again, the Undo action itself will be revoked, that is, redo ). In vim, you can use the U command to undo multi-step operations. The "repeat" shortcut key is Ctrl + R.

    After using the compatibility mode, VIM will discard these new features and try to imitate the various operations of VI. Only in incompatible mode can we better utilize Vim's own characteristics. Grandpa Bram strongly recommends Vim's incompatible mode, which is also recommended by dianhu. Be sure to write "set nocp" in the first line of your. vimrc ".
  • Set Ru

    Run this command to open the vim status bar ruler. By default, Vim's status bar ruler is at the bottom of the screen. It instantly displays the row number, column number, and percentage of the entire file where the current cursor is located in the file. Opening the ruler makes file editing easy.
  • Set HLS

    Highlight the found text when searching. The features of this command have been introduced in vimtutor. In fact, it seems that many people do not like this function.
  • Set is

    Search starts when the text to be retrieved is not fully entered. Vimtutor has also introduced this command. Dian Hu does not like this function, so he does not have this command in his configuration file. However, some friends around me like this very much, so Dian Hu still lists it here.
  • Syntax on

    Color the keywords. The programmer should know what the keyword coloring is, so I won't say much here. If you do not design a program, you may wish to enable this function. Although it may not be useful, it is actually a very interesting feature.
  • Set backspace = indent, EOL, start

    Suppose there are several letters in front of the current cursor. Press I to enter the insert mode, enter 3 letters, and press 5 to delete the backspace ). By default, VIM can only delete the three new letters that we entered, and then the speaker "beep. If we use "set backspace = start", we can continue to delete the original two characters after the new three letters are deleted.

    Imagine another situation: There are several lines of text. We move the cursor to the beginning of a row in the middle, press I to enter insert mode, and then click backspace. By default, the speaker will beep without any movement. If we set backspace = EOL, we can delete the carriage return at the end of the previous row, that is, we can splice the two rows.

    After we set Automatic indent, if the first line is indented for a certain distance, press enter to keep the same indent for the next line. By default, we cannot directly press backspace in insert mode to delete the first indent of a row. If we set backspace = indent, this function can be enabled.

    You can select one or more of the above three functions based on your needs and use commas to separate the options. We recommend that you select all three options.
  • Set whichwrap = B, S, <,>, [,]

    By default, when the cursor moves to the leftmost of a row in Vim, we continue to press the left button, and the cursor cannot return to the rightmost of the previous row. Similarly, when we move the cursor to the rightmost of a row, we cannot continue to press right to jump to the leftmost of the next row. However, by setting whichwrap, we can enable this function on some buttons. If you want to enable the next line for one or more buttons, you can write the code of the key to be enabled to the whichwrap parameter list, separated by commas. The following lists the key names supported by whichwrap:

    • B

      Press the delete key in normal or visual mode.
    • S

      Press space in normal or visual mode.
    • H

      Press h in normal or visual mode.
    • L

      Press L in normal or visual mode.
    • <

      In normal or visual mode, press the left arrow key.
    • >

      In normal or visual mode, press the right arrow key.
    • ~

      In normal mode, press ~ Key (flip the current letter case ).
    • [

      In insert or replace mode, press the left arrow key.
    • ]

      In insert or replace mode, press the right arrow key.
  • Set encoding = cp936

    Set the current character encoding to simplified Chinese. Here is another section that can automatically determine the file's encoding and automatically select the appropriate encoded. vimrc code. If you are interested, you can use it in your own configuration file:
" Encoding settingsif has("multi_byte")    " Set fileencoding priorityif getfsize(expand("%")) > 0set fileencodings=ucs-bom,utf-8,cp936,big5,euc-jp,euc-kr,latin1elseset fileencodings=cp936,big5,euc-jp,euc-kr,latin1endif    " CJK environment detection and corresponding settingif v:lang =~ "^zh_CN"        " Use cp936 to support GBK, euc-cn == gb2312set encoding=cp936set termencoding=cp936set fileencoding=cp936elseif v:lang =~ "^zh_TW"        " cp950, big5 or euc-tw        " Are they equal to each other?set encoding=big5set termencoding=big5set fileencoding=big5elseif v:lang =~ "^ko"        " Copied from someone's dotfile, untestedset encoding=euc-krset termencoding=euc-krset fileencoding=euc-krelseif v:lang =~ "^ja_JP"        " Copied from someone's dotfile, untesteset encoding=euc-jpset termencoding=euc-jpset fileencoding=euc-jpendif    " Detect UTF-8 locale, and replace CJK setting if neededif v:lang =~ "utf8___FCKpd___0quot; || v:lang =~ "UTF-8___FCKpd___0quot;set encoding=utf-8set termencoding=utf-8set fileencoding=utf-8endifelseechoerr "Sorry, this version of (g)vim was not compiled with multi_byte"endif
2. text editing settings
  • Set Sw = 4

    The indentation size is 4 spaces when auto indent is performed.
  • Set Ts = 4

    The tab width is 4 characters.
  • Set et

    Replace all tabs with spaces during editing.

    This option only replaces tabs with spaces during editing. If an existing file is opened, the existing tab is not replaced with spaces. If you want to replace it like this, you can use the ": retab" command ".
3. Disconnection settings
  • Set LBR

    The row is not interrupted in a word. After this option is set, if a line of text is very long and cannot be displayed in a line, it will be disconnected from the blank space between words, try not to split a word into two different rows.
  • Set fo + = MB

    Enable support for the Asian language in the disconnected line module. M indicates that a break between two Chinese characters is allowed, even if there is no space between the Chinese characters. B indicates that when the two rows are merged into one row, do not fill in spaces between Chinese characters and Chinese characters. For more options supported by this command, see the user manual.
4 C/C ++ encoding settings
  • Set Sm

    The matching conditions of parentheses are displayed. When this option is enabled, the cursor will jump back to the parentheses for a moment when you enter the parentheses (including parentheses, braces, and braces), and then jump back to display the matching conditions of the parentheses.
  • Set CIN

    Enable the automatic indent of the C/C ++ style. After automatic indent is enabled, you can use the "v" command to select a text section and press "=" to re-adjust the indent format, to a certain extent, it plays a role in code beautification.

    In addition, after automatic indentation is enabled, when Vim under the term is used, the format is often messy when the code is pasted. That's because the term does not know that you are pasting the code. It directly "paste" the action and explains it to VIM as a type. Therefore, VIM adjusted the indentation for your code according to the set Automatic indent format. Unfortunately, the pasted Code contains indentation, so the layout is messy. This problem won't occur in gvim, because it can know that you are pasting it now.

    After knowing the cause of this symptom, the solution is obvious: Disable all automatic indentation during pasting, and enable it after pasting. It is too tedious to manually disable auto indent. Vim provides us with a very useful command. You only need to enter ": Set paste" to disable all auto indent. After pasting, enter ": Set nopaste" to re-open the original auto indent settings.
  • Set cino =: 0g0t0 (Sus

    Set the option of C/C ++ style Automatic indent. Here we will briefly introduce the meaning of the options used in this Code. There are many options supported by cino. For more options, see the user manual.
//: 0 // The case statement under the switch statement is indented with 0 spaces, that is, it is not indented. It is flat with the switch block // and adopts the following style:Switch(X ){Case1: A = B;Break;Default:}/G0 // class, struct, and other access permission control statements, such as public, protected, and private, // indent the block of a class or struct with 0 spaces, which is the same as that of a class. // style:ClassFoo {Public:IntA;Private:IntB ;}; // t0 /// if the function return value is not in the same line as the function name, the return value is Indented by 0 spaces, that is, not indented, as shown in: // set cino = T4IntFunc1 () {}// set cino = T0IntFunc () {}// (SUS /// when a pair of parentheses spans multiple rows, the following rows indent the distance specified by Sw. The effect is as follows:IntA = (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8) * 9;
  • Set AI

    Enable Automatic indentation for common file types. This automatic indent is not as intelligent as cindent, but it can help you edit non-C/C ++ files.
5 other settings
  • Set selectmode =

    Selectmode is not used.
  • Set mousemodel = popup

    When you right-click a window, a shortcut menu is displayed.
  • Set keymodel =

    Select text without "Shift + direction key". "Shift + direction key" indicates a word jump to the specified direction. If you like this feature, you can use "set keymodel = startsel, stopsel" to open it.
  • Set selection = random sive

    Specify that when selecting text, the cursor position also belongs to the selected range. If selection = exclusive is specified, some text may not be selected.
  • Colo torte

    Select the torte color scheme. Vim has many built-in color schemes for keywords. You can also download more color schemes from the Internet or write them by yourself. Click "edit"> "color palette" to list all supported color schemes. You can cut down the "palette" sub-menu, and then select one of your favorite color schemes.
6 graphic interface settings
  • Set nowrap

    Specifies no rows. If a line is too long and exceeds the screen width, it will extend to the right side to the outside of the screen. If the graphic interface is used, it would be much better to specify a line without compromise.
  • Set guioptions + = B

    Add a horizontal scroll bar. If you specify no rows, it is necessary to add a horizontal scroll bar for the window.
  • Set guifont = courier \ 9

    Set the font in the graphic interface. Click "edit"> "select font". Then, select the font and font size you like in the dialog box. After the selection, press the appropriate number of ESC commands to confirm that the font is in normal mode, then run the following command: ": Set guifont?" After you press enter, the name and font size of the current font will be displayed at the bottom of the gvim screen. Write the obtained result to the configuration file. Note that if the font name contains spaces, you must add a slash before all spaces.
7. Conditional Selection

The same color scheme works quite differently in gvim and VIM on the Character interface. Dian Hu's personal habit is to use torte color scheme in gvim and Ron color scheme in Vim. Therefore, it is necessary to make different settings for gvim and vim.

In addition, we did not use line breaks in gvim to enable the horizontal scroll bar, but in Vim, there is no scroll bar available, so it is necessary to retain automatic lines for vim.

The format of the Condition Selection settings is as follows:

If (Has("Gui_running"))"Settings in the graphic interfaceSetNowrapSetGuioptions + = BColoTorteElse"Character interface settingsSetWrapColoRonEndif

From: http://learn.tsinghua.edu.cn: 8080/2001012074/html/_vimrc_for_beginners.html

========================================================== ======================================

PS:

In fact, the author only talked about some of them. For example, to display the row number: set number and not show the row number: Set nonumber, the two can be abbreviated as set Nu and set Nonu, it is more practical for development.

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.