📄 starting.txt
字号:
Standard setup:Create a vimrc file to set the default settings and mappings for all your editsessions. Put it in a place so that it will be found by 3b: ~/.vimrc (Unix and OS/2) s:.vimrc (Amiga) $VIM\_vimrc (MS-DOS and Win32)Note that creating a vimrc file will cause the 'compatible' option to be offby default. See |compatible-default|.Local setup:Put all commands that you need for editing a specific directory only into avimrc file and place it in that directory under the name ".vimrc" ("_vimrc"for MS-DOS and Win32). NOTE: To make Vim look for these special files youhave to turn on the option 'exrc'. See |trojan-horse| too.System setup:This only applies if you are managing a Unix system with several users andwant to set the defaults for all users. Create a vimrc file with commandsfor default settings and mappings and put it in the place that is given withthe ":version" command.Saving the current state of Vim to a file:Whenever you have changed values of options or when you have created amapping, then you may want to save them in a vimrc file for later use. See|save-settings| about saving the current state of settings to a file.Avoiding setup problems for Vi users:Vi uses the variable EXINIT and the file "~/.exrc". So if you do not want tointerfere with Vi, then use the variable VIMINIT and the file "vimrc" instead.Amiga environment variables:On the Amiga, two types of environment variables exist. The ones set with theDOS 1.3 (or later) setenv command are recognized. See the AmigaDos 1.3manual. The environment variables set with the old Manx Set command (beforeversion 5.0) are not recognized.MS-DOS line separators:On MS-DOS-like systems (MS-DOS itself, Win32, and OS/2), Vim assumes that allthe vimrc files have <CR> <NL> pairs as line separators. This will giveproblems if you have a file with only <NL>s and have a line like":map xx yy^M". The trailing ^M will be ignored. *compatible-default*When Vim starts, the 'compatible' option is on. This will be used when Vimstarts its initializations. But as soon as a user vimrc file is found, or avimrc file in the current directory, or the "VIMINIT" environment variable isset, it will be set to 'nocompatible'. This has the side effect of setting orresetting other options (see 'compatible'). But only the options that havenot been set or reset will be changed. This has the same effect like thevalue of 'compatible' had this value when starting Vim. Note that thisdoesn't happen for the system-wide vimrc file.But there is a side effect of setting or resetting 'compatible' at the momenta .vimrc file is found: Mappings are interpreted the moment they areencountered. This makes a difference when using things like "<CR>". If themappings depend on a certain value of 'compatible', set or reset it beforegiving the mapping.The above behavior can be overridden in these ways:- If the "-N" command line argument is given, 'nocompatible' will be used, even when no vimrc file exists.- If the "-C" command line argument is given, 'compatible' will be used, even when a vimrc file exists.- If the "-u {vimrc}" argument is used, 'compatible' will be used.- When the name of the executable ends in "ex", then this works like the "-C" argument was given: 'compatible' will be used, even when a vimrc file exists. This has been done to make Vim behave like "ex", when it is started as "ex".Avoiding trojan horses: *trojan-horse*While reading the "vimrc" or the "exrc" file in the current directory, somecommands can be disabled for security reasons by setting the 'secure' option.This is always done when executing the command from a tags file. Otherwise itwould be possible that you accidentally use a vimrc or tags file that somebodyelse created and contains nasty commands. The disabled commands are the onesthat start a shell, the ones that write to a file, and ":autocmd". The ":map"commands are echoed, so you can see which keys are being mapped. If you want Vim to execute all commands in a local vimrc file, youcan reset the 'secure' option in the EXINIT or VIMINIT environment variable orin the global "exrc" or "vimrc" file. This is not possible in "vimrc" or"exrc" in the current directory, for obvious reasons. On Unix systems, this only happens if you are not the owner of thevimrc file. Warning: If you unpack an archive that contains a vimrc or exrcfile, it will be owned by you. You won't have the security protection. Checkthe vimrc file before you start Vim in that directory, or reset the 'exrc'option. Some Unix systems allow a user to do "chown" on a file. This makesit possible for another user to create a nasty vimrc and make you the owner.Be careful! When using tag search commands, executing the search command (the lastpart of the line in the tags file) is always done in secure mode. This worksjust like executing a command from a vimrc/exrc in the current directory. *slow-start*If Vim takes a long time to start up, there may be a few causes:- If the Unix version was compiled with the GUI and/or X11 (check the output of ":version" for "+GUI" and "+X11"), it may need to load shared libraries and connect to the X11 server. Try compiling a version with GUI and X11 disabled. This also should make the executable smaller.- If you have "viminfo" enabled, the loading of the viminfo file may take a while. You can find out if this is the problem by disabling viminfo for a moment (use the Vim argument "-i NONE", |-i|). Try reducing the number of lines stored in a register with ":set viminfo='20\"50". |viminfo-file|. *:intro*When Vim starts without a file name, an introductory message is displayed (forthose who don't know what Vim is). It is removed as soon as the display isredrawn in any way. To see the message again, use the ":intro" command.To avoid the intro message on startup, add the 'I' flag to 'shortmess'.==============================================================================5. Suspending *suspend* *iconise* *CTRL-Z* *v_CTRL-Z*CTRL-Z Suspend Vim, like ":stop". Works in Normal and in Visual mode. In Insert and Command-line mode, the CTRL-Z is inserted as a normal character.:sus[pend][!] or *:sus* *:suspend* *:st* *:stop*:st[op][!] Suspend Vim. If the '!' is not given and 'autowrite' is set, every buffer with changes and a file name is written out. If the '!' is given or 'autowrite' is not set, changed buffers are not written, don't forget to bring Vim back to the foreground later!In the GUI, suspending is implemented as iconising gvim. In Windows 95/NT,gvim is minimized.On many Unix systems, it is possible to suspend Vim with CTRL-Z. This is onlypossible in Normal and Visual mode (see next chapter, |vim-modes|). Vim willcontinue if you make it the foreground job again. On other systems, CTRL-Zwill start a new shell. This is the same as the ":sh" command. Vim willcontinue if you exit from the shell.==============================================================================6. The vimrc file *vimrc-intro* *vim-script-intro*A vimrc file can be used for settings you intend to use more-or-less for everyof your Vim sessions. Normally the file is called $HOME/.vimrc, but otherfiles can also be used, see |vimrc|. Vim will read it (them) when startingand interpret the commands in them.You can also use Vim script files for other purposes. The files used forsyntax highlighting are an example. There are enough commands in Vim toaccomplish just about any task. *vimrc_example.vim*You should be able to find an example vimrc file at$VIMRUNTIME/vimrc_example.vim. You could use this as a start for your ownvimrc file. You can also find a lot of examples in vimrc files made byothers. Have a look at http://www.vim.org/user.html.USING EX COMMANDS ~The vimrc file can contain anything that can be typed on the Vim command line.The recommended practice is not to include the preceding colon sign ":", thusif one would type> :set numberon the Vim command line, the same can appear in the vimrc file simply as> set numberThe end-of-line character depends on the system. For Unix a single <NL>character is used. For MS-DOS, Windows, OS/2 and the like, <CR><LF> is used.This is important when using mappings that end in a <CR>. See |:source_crnl|.Long lines can be split up by inserting a "\" (backslash) at the start ofthe next line where the line has been broken, see |line-continuation|.Example:> iabbr Http Hyper Text Transfer> \ ProtocolNote that this only works in a Vim script file.WHITE SPACE ~Blank lines are allowed and ignored.Leading whitespace characters (blanks and TABs) are always ignored. Thewhitespaces between parameters (e.g. between the 'set' and the 'number' in theexample above) are reduced to one blank character and plays the role of aseparator, the whitespaces after the last (visible) character may or may notbe ignored depending on the situation, see below.For a ":set" command involving the "=" (equal) sign, such as in> set cpoptions =aABceFstthe whitespace immediately before the "=" sign is ignored. But there can beno whitespace after the "=" sign!To include a whitespace character in the value of an option, it must beescaped by a "\" (backslash) as in the following example:> set tags=my\ nice\ fileThe same example written as> set tags=my nice filewill issue an error, because it is interpreted as:> set tags=my> set nice> set fileCOMMENTS ~The character " (the double quote mark) starts a comment. Everything afterand including this character until the end-of-line is considered a comment andis ignored, except for commands that don't consider comments, as shown inexamples below. A comment can start on any character position on the line.There is a little "catch" with comments for some commands. Examples:> abbrev dev development " shorthand> map <F3> o#include " insert include> execute cmd " do it> !ls *.c " list C filesThe abbreviation 'dev' will be expanded to 'development " shorthand'. Themapping of <F3> will actually be the whole line after the 'o# ....' includingthe '" insert include'. The "execute" command will give an error. The "!"command will send everything after it to the shell, causing an error for anunmatched '"' character.There can be no comment after ":map", ":abbreviate", ":execute" and "!"commands (there are a few more commands with this restriction). For the"map", ":abbreviate" and "execute" commands there is a trick:> abbrev dev development|" shorthand> map <F3> o#include|" insert include> execute cmd |" do itWith the '|' character the command is separated from the next one. And thatnext command is only a comment.Note that there is no white space before the '|' in the abbreviation andmapping. For these commands, any character until the end-of-line or '|' isincluded. As a consequence of this behavior, you don't always see thattrailing whitespace is included:> map <F4> o#include To avoid these problems, you can set the 'list' option when editing vimrcfiles.PITFALLS ~Even bigger problem arises in the following example:> map ,ab o#include> unmap ,abHere the mapping of ,ab will be ',ab', no trailing whitespaces is included.However, the "unmap" does not end directly with the end-of-line, Vim will tryto unmap ',ab ', which does not exist as a mapped sequence. An error will beissued, which is very hard to identify, because the ending whitespacecharacter on the 'unmap ,ab ' are not visible.And this is exactly the same what happens when one uses a comment after an'unmap' command:> unmap ,ab " commentHere the comment part will be ignored. However, Vim will try to unmap',ab ', which does not exist, Deleting the comment as well as all thewhitespaces up the ending 'b' character will cure the problem.Except for the situations as above, it is legal to put a comment on the sameline as the Vim definitions, such as> set number " display line numbersNORMAL MODE COMMANDS ~To execute normal mode commands, the |:normal| command can be used. To avoidtrouble with special characters, use the |:execute| command. Its argument isan expression (more about that further down). Example:> exe "normal mxGOA test.\<Esc>`x"This appends "A test." below the last line in the file and returns the cursorto where it was. Note the use of "\<Esc>", which is translated into theescape character. To use a backslash or double quote here you have to put abackslash before it:> exe "normal \"aYgg\"aP"This yanks the current line and puts it above the first line in the file. Todo this without changing the cursor position and the text displayed in thewindow, this has to be extended a bit:> exe "normal ma\"aYHmbgg\"aP`bzt`a"What this does: ma set mark a at cursor position "aY yank current line into register a Hmb go to top line in window and set mark b there gg go to first line in file "aP put the yanked line above it `b go back to top line in display zt position the text in the window as it was before `a go back to saved cursor positionIF - ELSE - ENDIF ~Sometimes you will want to execute a command only under a certain condition.The |:if| command can be used for this. Example:> if &term == "xterm"> echo "this is an xterm"> endifThe argument for the "if" command is an expression. If this expressionevaluates to true (non-zero), the statements until the "endif" are executed.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -