*starting.txt* For Vim version 5.6. Last change: 1999 Sep 07 VIM REFERENCE MANUAL by Bram Moolenaar Starting Vim *starting* 1. Vim arguments |vim-arguments| 2. Vim on the Amiga |starting-amiga| 3. Running Vim from xargs |xargs| 4. Initialization |initialization| 5. Suspending |suspend| 6. The vimrc file |vimrc-intro| 7. The viminfo file |viminfo-file| ============================================================================== 1. Vim arguments *vim-arguments* Most often, Vim is started to edit a single file with the command vim filename *-vim* More generally, Vim is started with: vim [option | filename] .. Option arguments and file name arguments can be mixed, and any number of them can be given. However, watch out for options that take an argument. For compatibility with various Vi versions, see |cmdline-arguments|. Exactly one out of the following five items may be used to choose how to start editing: *-file* *---* filename One or more file names. The first one will be the current file and read into the buffer. The cursor will be positioned on the first line of the buffer. To avoid a file name starting with a '-' being interpreted as an option, precede the arglist with "--", e.g.: Vim -- -filename All arguments after the "--" will be interpreted as file names, no other options or "+command" argument can follow. *--* - This argument can mean two things, depending on whether Ex mode is to be used. Starting in Normal mode: > vim - or ex -v - Start editing a new buffer, which is filled with text that is read from stdin. The commands that would normally be read from stdin will now be read from stderr. Example: > find . -name "*.c" -print | vim - The buffer will be marked modified, because it contains text that needs to be saved. Except when in readonly mode, then the buffer is not marked modified. Example: > ls | view - Starting in Ex mode: > ex - or vim -e - Start editing in silent mode. See |-s-ex|. *-t* *-tag* -t {tag} A tag. "tag" is looked up in the tags file, the associated file becomes the current file, and the associated command is executed. Mostly this is used for C programs, in which case "tag" often is a function name. The effect is that the file containing that function becomes the current file and the cursor is positioned on the start of the function (see |tags|). *-q* *-qf* -q [errorfile] QuickFix mode. The file with the name [errorfile] is read and the first error is displayed. See |quickfix|. If [errorfile] is not given, the 'errorfile' option is used for the file name. See 'errorfile' for the default value. {not in Vi} (nothing) Without one of the four items above, Vim will start editing a new buffer. It's empty and doesn't have a file name. The startup mode can be changed by using another name instead of "vim", which is equal to giving options: ex vim -e Start in Ex mode (see |Ex-mode|). *ex* view vim -R Start in read-only mode (see |-R|). *view* gvim vim -g Start the GUI (see |gui|). *gvim* gex vim -eg Start the GUI in Ex mode. *gex* gview vim -Rg Start the GUI in read-only mode. *gview* rvim vim -Z Like "vim", but in restricted mode (see |-Z|) *rvim* rview vim -RZ Like "view", but in restricted mode. *rview* rgvim vim -gZ Like "gvim", but in restricted mode. *rgvim* rgview vim -RgZ Like "gview", but in restricted mode. *rgview* Additional characters may follow, they are ignored. For example, you can have "gvim-5" to start the GUI. You must have an executable by that name then, of course. On Unix, you would normally have one executable called Vim, and links from the different startup-names to that executable. If your system does not support links and you do not want to have several copies of the executable, you could use an alias instead. For example: > alias view vim -R > alias gvim vim -g *startup-options* The option arguments may be given in any order. Single-letter options can be combined after one dash. There can be no option arguments after the "--" argument. --help *-h* *--help* -h Give usage (help) message and exit. {not in Vi} *--version* --version Print version information and exit. Same output as for |:version| command. {not in Vi} *-+* +[num] The cursor will be positioned on line "num" for the first file being edited. If "num" is missing, the cursor will be positioned on the last line. *-+/* +/{pat} The cursor will be positioned on the first line containing "pat" in the first file being edited (see |pattern| for the available search patterns). +{command} *-+c* *-c* -c {command} "command" will be executed after the first file has been read (and after autocommands and modelines for that file have been processed). "command" is interpreted as an Ex command. If the "command" contains spaces, it must be enclosed in double quotes (this depends on the shell that is used). Example: > vim "+set si" main.c > vim -c "set ff=dos" -c wq mine.mak Note: You can use up to 10 "+" or "-c" arguments in a Vim command. They are executed in the order given. {Vi only allows one command} *-r* -r Recovery mode. Without a file name argument, a list of existing swap files is given. With a file name, a swap file is read to recover a crashed editing session. See |crash-recovery|. *-L* -L Same as -r. {only in some versions of Vi: "List recoverable edit sessions"} *-R* -R Readonly mode. The 'readonly' option will be set for all the files being edited. You can still edit the buffer, but will be prevented from accidentally overwriting a file. If you forgot that you are in View mode and did make some changes, you can overwrite a file by adding an exclamation mark to the Ex command, as in ":w!". The 'readonly' option can be reset with ":set noro" (see the options chapter, |options|). Subsequent edits will not be done in readonly mode. Calling the executable "view" has the same effect as the -R argument. The 'updatecount' option will be set to 10000, meaning that the swap file will not be updated automatically very often. *-m* -m Modifications not allowed. The 'write' option will be reset, so that writing files is disabled. The 'write' option can be set to enable writing again. *-Z* -Z Restricted mode. All commands that make use of an external shell are disabled. This includes suspending with CTRL-Z, ":sh", filtering, etc. *-g* -g Start Vim in GUI mode. See |gui|. *-v* -v Start Ex in Vi mode. Only makes a difference when the executable is called "ex" or "gvim". For gvim the GUI is not started if possible. *-e* -e Start Vim in Ex mode. Only makes a difference when the executable is not called "ex". *-s-ex* -s Silent or batch mode. Only when Vim was started as "ex" or when preceded with the "-e" argument. Otherwise see |-s|. To be used when Vim is used to execute Ex commands from a file instead of a terminal. Switches off most prompts and informative messages. But not warning and error messages, and the output from commands that print text lines, like ":print" and ":list". Initializations are skipped (except the ones given with the "-u" argument). *-b* -b Binary mode. File I/O will only recognize to separate lines. The 'expandtab' option will be reset. The 'textwidth' option is set to 0. 'modeline' is reset. The 'binary' option is set. This is done after reading the vimrc/exrc files but before reading any file in the arglist. See also |edit-binary|. {not in Vi} *-l* -l Lisp mode. Sets the 'lisp' and 'showmatch' options on. *-F* -F Farsi mode. Sets the 'fkmap' and 'rightleft' options on. (Only when compiled with |+rightleft| and |+farsi| features, otherwise Vim gives an error message and exits). {not in Vi} *-H* -H Hebrew mode. Sets the 'hkmap' and 'rightleft' options on. (Only when compiled with the |+rightleft| feature, otherwise Vim gives an error message and exits). {not in Vi} *-V* *verbose* -V[n] Verbose. Sets the 'verbose' option to [n][ (default: 10). Messages will be given for each file that is ":source"d and for reading or writing a viminfo file. Can be used to find out what is happening upon startup and exit. {not in Vi} *-C* -C Compatible mode. Sets the 'compatible' option. You can use this to get 'compatible', even though there is a .vimrc file. Also see |compatible-default|. {not in Vi} *-N* -N Not compatible mode. Resets the 'compatible' option. You can use this to get 'nocompatible', when there is no .vimrc file. Also see |compatible-default|. {not in Vi} *-n* -n No swap file will be used. Recovery after a crash will be impossible. Handy if you want to view or edit a file on a very slow medium (e.g., a floppy). Can also be done with ":set updatecount=0". You can switch it on again by setting the 'updatecount' option to some value, e.g., ":set uc=100". 'updatecount' is set to 0 AFTER executing commands from a vimrc file, but before the GUI initializations. Thus it overrides a setting for 'updatecount' in a vimrc file, but not in a gvimrc file. See |startup|. {not in Vi} When you want to reduce accesses to the disk (e.g., for a laptop), don't use "-n", but set 'updatetime' and 'udpatecount' to very big numbers, and type ":preserve" when you want to save your work. This way you keep the possibility for crash recovery. *-o* -o[N] Open N windows. If [N] is not given, one window is opened for every file given as argument. If there is not enough room, only the first few files get a window. If there are more windows than arguments, the last few windows will be editing an empty file. {not in Vi} *-T* -T {terminal} Set the terminal type to "terminal". This influences the codes that Vim will send to your terminal. This is normally not needed, because Vim will be able to find out what type of terminal you are using (See |terminal-info|). {not in Vi} *-d* -d {device} Amiga only: The "device" is opened to be used for editing. Normally you would use this to set the window position and size: "-d con:x/y/width/height", e.g., "-d con:30/10/600/150". But you can also use it to start editing on another device, e.g., AUX:. {not in Vi} *-f* -f Amiga: Do not restart Vim to open a new window. This option should be used when Vim is started by a program that will wait for the edit session to finish (e.g., mail or readnews). See |amiga-window|. {not in Vi} GUI: Do not disconnect from the program that started Vim. 'f' stands for "foreground". If omitted, the GUI forks a new process and exits the current one. "-f" should be used when gvim is started by a program that will wait for the edit session to finish (e.g., mail or readnews). If you want gvim never to fork, include 'f' in 'guioptions' in your .gvimrc. Careful: You can use "-gf" to start the GUI in the foreground, but "-fg" is used to specify the foreground color. {not in Vi} |gui-fork| *-u* -u {vimrc} The file "vimrc" is read for initializations. Other initializations are skipped; see |initialization|. This can be used to start Vim in a special mode, with special mappings and settings. A shell alias can be used to make this easy to use. For example: > alias vimc vim -u ~/.c_vimrc !* Also consider using autocommands; see |autocommand|. When {vimrc} is equal to "NONE" (all uppercase), all initializations from files and environment variables are skipped, including reading the .gvimrc file when the GUI starts. Using the "-u" argument also means that the 'compatible' option will be on by default. This can have unexpected side effects. See |'compatible'|. {not in Vi} *-U* -U {gvimrc} The file "gvimrc" is read for initializations when the GUI starts. Other GUI initializations are skipped. When {gvimrc} is equal to "NONE", no file is read for initializations at all. Exception: Reading the system-wide menu file is always done. *-i* -i {viminfo} The file "viminfo" is used instead of the default viminfo file. If the name "NONE" is used (all uppercase), no viminfo file is read or written, even if 'viminfo' is set or when ":rv" or ":wv" are used. See also |viminfo-file|. {not in Vi} *-x* -x Use encryption to read/write files. Will prompt for a key, which is then stored in the 'key' option. All writes will then use this key to encrypt the text. The '-x' argument is not needed when reading a file, because there is a check if the file that is being read has been encrypted, and Vim asks for a key automatically. |encryption| *-s* -s {scriptin} The script file "scriptin" is read. The characters in the file are interpreted as if you had typed them. The same can be done with the command ":source! {scriptin}". If the end of the file is reached before the editor exits, further characters are read from the keyboard. Only works when not started in Ex mode, see |-s-ex|. See also |complex-repeat|. {not in Vi} *-w* -w {scriptout} All the characters that you type are recorded in the file "scriptout", until you exit Vim. This is useful if you want to create a script file to be used with "vim -s" or ":source!". When the "scriptout" file already exists, new characters are appended. See also |complex-repeat|. {not in Vi} *-W* -W {scriptout} Like -w, but do not append, overwrite an existing file. {not in Vi} *-w_nr* -w{number} Does nothing. This was included for Vi-compatibility. In Vi it sets the 'window' option, which is not implemented in Vim. Example for using a script file to change a name in several files: Create a file "subs.vi" containing substitute commands and a :wq command: > :%s/Jones/Smith/g > :%s/Allen/Peter/g > :wq Execute Vim on all files you want to change: > foreach i ( *.let ) vim -s subs.vi $i If the executable is called "view", Vim will start in Readonly mode. This is useful if you can make a hard or symbolic link from "view" to "vim". Starting in Readonly mode can also be done with "vim -R". If the executable is called "ex", Vim will start in "Ex" mode. This means it will accept only ":" commands. But when the "-v" argument is given, Vim will start in in Normal mode anyway. ============================================================================== 2. Vim on the Amiga *starting-amiga* Starting Vim from the Workbench *workbench* ------------------------------- Vim can be started from the Workbench by clicking on its icon twice. It will then start with an empty buffer. Vim can be started to edit one or more files by using a "Project" icon. The "Default Tool" of the icon must be the full pathname of the Vim executable. The name of the ".info" file must be the same as the name of the text file. By clicking on this icon twice, Vim will be started with the file name as current file name, which will be read into the buffer (if it exists). You can edit multiple files by pressing the shift key while clicking on icons, and clicking twice on the last one. The "Default Tool" for all these icons must be the same. It is not possible to give arguments to Vim, other than file names, from the workbench. Vim window *amiga-window* ---------- Vim will run in the CLI window where it was started. If Vim was started with the "run" or "runback" command, or if Vim was started from the workbench, it will open a window of its own. Technical detail: To open the new window a little trick is used. As soon as Vim recognizes that it does not run in a normal CLI window, it will create a script file in "t:". This script file contains the same command as the one Vim was started with, and an "endcli" command. This script file is then executed with a "newcli" command (the "c:run" and "c:newcli" commands are required for this to work). The script file will hang around until reboot, or until you delete it. This method is required to get the ":sh" and ":!" commands to work correctly. But when Vim was started with the -f option (foreground mode), this method is not used. The reason for this is that when a program starts Vim with the -f option it will wait for Vim to exit. With the script trick, the calling program does not know when Vim exits. The -f option can be used when Vim is started by a mail program which also waits for the edit session to finish. As a consequence, the ":sh" and ":!" commands are not available when the -f option is used. Vim will automatically recognize the window size and react to window resizing. Under Amiga DOS 1.3, it is advised to use the fastfonts program, "FF", to speed up display redrawing. ============================================================================== 3. Running Vim from xargs *xargs* The xargs program can be used to start a program with arguments that come from stdin. A typical example: > find . -name "*.c" -print | xargs vim A problem is that stdin for the program run by xargs is not setup properly, which causes problems for Vim. That is because Vim expects commands to come from stdin. The symptoms are that typed characters are echoed and don't take effect until is hit. This is really a problem in xargs. To work around it, you can use this small program: > #include > #include > > main(argc, argv) > int argc; > char **argv; > { > close(0); /* close stdin */ > dup(2); /* duplicate stdin from stderr */ > execvp(argv[1], argv + 1); > perror("could not execute program"); > } Store this in "start.c", compile it to "start" and put it somewhere in your search path. You might have to make a few adjustments for your system. An example of using this: > find . -name "*.c" -print | xargs start vim ============================================================================== 4. Initialization *initialization* *startup* This section is about the non-GUI version of Vim. See |gui-fork| for additional initialization when starting the GUI. At startup, Vim checks environment variables and files and sets values accordingly. Vim proceeds in this order: 1. Set the 'shell' option *SHELL* *COMSPEC* The environment variable SHELL, if it exists, is used to set the 'shell' option. On MS-DOS and Win32, the COMPSPEC variable is used if SHELL is not set. 2. Set the 'term' option *TERM* The environment variable TERM, if it exists, is used to set the 'term' option. 3. Execute Ex commands, from environment variables and/or files An environment variable is read as one Ex command line, where multiple commands must be separated with '|' or "". *vimrc* *exrc* A file that contains initialization commands is called a "vimrc" file. Each line in a vimrc file is executed as an Ex command line. It is sometimes also referred to as "exrc" file. They are the same type of file, but "exrc" is what Vi always used, "vimrc" is a Vim specific name. Also see |vimrc-intro|. Recommended place for your personal initializations: Unix $HOME/.vimrc OS/2 $HOME/.vimrc or $VIM/.vimrc (or _vimrc) MS-DOS and Win32 $HOME/_vimrc or $VIM/_vimrc Amiga s:.vimrc or $VIM/.vimrc If Vim was started with "-u filename", the file "filename" is used. All following initializations until 5. are skipped. "vim -u NONE" can be used to skip these initializations. |-u| If Vim was started in Ex mode with the "-s" argument, all following initializations until 4. are skipped. Only the "-u" option is interpreted. *system-vimrc* a. For Unix, OS/2, VMS, Macintosh, RISC-OS and Amiga the system vimrc file is read for initializations. The path of this file is shown with the ":version" command. Note that this file is ALWAYS read in 'compatible' mode, since the automatic resetting of 'compatible' is only done later. Add a ":set nocp" command if you like. *VIMINIT* *.vimrc* *_vimrc* *EXINIT* *.exrc* *_exrc* b. Four places are searched for initializations. The first that exists is used, the others are ignored. - The environment variable VIMINIT (see also |compatible-default|) (*) - The user vimrc file(s): "$HOME/.vimrc" (for Unix and OS/2) (*) "s:.vimrc" (for Amiga) (*) "home:.vimrc" (for Amiga) (*) "$VIM/.vimrc" (for OS/2 and Amiga) (*) "$HOME/_vimrc" (for MS-DOS and Win32) (*) "$VIM\_vimrc" (for MS-DOS and Win32) (*) Note: For Unix, OS/2 and Amiga, when ".vimrc" does not exist, "_vimrc" is also tried, in case an MS-DOS compatible file system is used. For MS-DOS and Win32 ".vimrc" is checked after "_vimrc", in case long file names are used. Note: For MS-DOS and Win32, "$HOME" is checked first. If no "_vimrc" or ".vimrc" is found there, "$VIM" is tried. See |$VIM| for when $VIM is not set. - The environment variable EXINIT - The user exrc file(s). Same as for the user vimrc file, but with "vimrc" replaced by "exrc". But without the (*)! c. If the 'exrc' option is on (which is not the default), the current directory is searched for four files. The first that exists is used, the others are ignored. - The file ".vimrc" (for Unix, Amiga and OS/2) (*) "_vimrc" (for MS-DOS and Win32) (*) - The file "_vimrc" (for Unix, Amiga and OS/2) (*) ".vimrc" (for MS-DOS and Win32) (*) - The file ".exrc" (for Unix, Amiga and OS/2) "_exrc" (for MS-DOS and Win32) - The file "_exrc" (for Unix, Amiga and OS/2) ".exrc" (for MS-DOS and Win32) (*) Using this file or environment variable will cause 'compatible' to be off by default. See |compatible-default|. 4. Load the default file type detection script. This script is used to set autocommands for the FileType event. See |FileType| and |'filetype'|. This is only done if Vim was compiled with the |+autocmd| feature. If Vim was started with "-u filename", this is skipped. |-u| The name of this file depends on the system: Amiga $VIMRUNTIME/filetype.vim Mac $VIMRUNTIME:filetype.vim MS-DOS $VIMRUNTIME\filetype.vim RiscOS Vim:Filetype Unix $VIMRUNTIME/filetype.vim VMS $VIMRUNTIME/filetype.vim 5. Set 'shellpipe' and 'shellredir' The 'shellpipe' and 'shellredir' options are set according to the value of the 'shell' option, unless they have been set before. This means that Vim will figure out the values of 'shellpipe' and 'shellredir' for you, unless you have set them yourself. 6. Set 'updatecount' to zero, if "-n" command argument used 7. Set binary options If the "-b" flag was given to Vim, the options for binary editing will be set now. See |-b|. 8. Perform GUI initializations Only when starting "gvim", the GUI initializations will be done. See |gui-init|. 9. Read the viminfo file If the 'viminfo' option is not empty, the viminfo file is read. The default is empty, so 'viminfo' must have been set by one of the previous initializations. See |viminfo-file|. 10. Read the quickfix file If the "-q" flag was given to Vim, the quickfix file is read. If this fails, Vim exits. 11. Open all windows When the |-o| flag was given, windows will be opened (but not displayed yet). When switching screens, it happens now. Redrawing starts. If the "-q" flag was given to Vim, the first error is jumped to. Buffers for all windows will be loaded. 12. Execute startup commands If a "-t" flag was given to Vim, the tag is jumped to. The commands given with the |-c| and |+cmd| arguments are executed. If the 'insertmode' option is set, Insert mode is entered. The |VimEnter| autocommands are executed. Some hints on using initializations: Standard setup: Create a vimrc file to set the default settings and mappings for all your edit sessions. 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 off by default. See |compatible-default|. Local setup: Put all commands that you need for editing a specific directory only into a vimrc 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 you have 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 and want to set the defaults for all users. Create a vimrc file with commands for default settings and mappings and put it in the place that is given with the ":version" command. Saving the current state of Vim to a file: Whenever you have changed values of options or when you have created a mapping, 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 to interfere 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 the DOS 1.3 (or later) setenv command are recognized. See the AmigaDos 1.3 manual. The environment variables set with the old Manx Set command (before version 5.0) are not recognized. MS-DOS line separators: On MS-DOS-like systems (MS-DOS itself, Win32, and OS/2), Vim assumes that all the vimrc files have pairs as line separators. This will give problems if you have a file with only 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 Vim starts its initializations. But as soon as a user vimrc file is found, or a vimrc file in the current directory, or the "VIMINIT" environment variable is set, it will be set to 'nocompatible'. This has the side effect of setting or resetting other options (see 'compatible'). But only the options that have not been set or reset will be changed. This has the same effect like the value of 'compatible' had this value when starting Vim. Note that this doesn't happen for the system-wide vimrc file. But there is a side effect of setting or resetting 'compatible' at the moment a .vimrc file is found: Mappings are interpreted the moment they are encountered. This makes a difference when using things like "". If the mappings depend on a certain value of 'compatible', set or reset it before giving 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, some commands can be disabled for security reasons by setting the 'secure' option. This is always done when executing the command from a tags file. Otherwise it would be possible that you accidentally use a vimrc or tags file that somebody else created and contains nasty commands. The disabled commands are the ones that 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, you can reset the 'secure' option in the EXINIT or VIMINIT environment variable or in 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 the vimrc file. Warning: If you unpack an archive that contains a vimrc or exrc file, it will be owned by you. You won't have the security protection. Check the 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 makes it 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 last part of the line in the tags file) is always done in secure mode. This works just 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 (for those who don't know what Vim is). It is removed as soon as the display is redrawn 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 only possible in Normal and Visual mode (see next chapter, |vim-modes|). Vim will continue if you make it the foreground job again. On other systems, CTRL-Z will start a new shell. This is the same as the ":sh" command. Vim will continue 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 every of your Vim sessions. Normally the file is called $HOME/.vimrc, but other files can also be used, see |vimrc|. Vim will read it (them) when starting and interpret the commands in them. You can also use Vim script files for other purposes. The files used for syntax highlighting are an example. There are enough commands in Vim to accomplish 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 own vimrc file. You can also find a lot of examples in vimrc files made by others. 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 ":", thus if one would type > :set number on the Vim command line, the same can appear in the vimrc file simply as > set number The end-of-line character depends on the system. For Unix a single character is used. For MS-DOS, Windows, OS/2 and the like, is used. This is important when using mappings that end in a . See |:source_crnl|. Long lines can be split up by inserting a "\" (backslash) at the start of the next line where the line has been broken, see |line-continuation|. Example: > iabbr Http Hyper Text Transfer > \ Protocol Note 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. The whitespaces between parameters (e.g. between the 'set' and the 'number' in the example above) are reduced to one blank character and plays the role of a separator, the whitespaces after the last (visible) character may or may not be ignored depending on the situation, see below. For a ":set" command involving the "=" (equal) sign, such as in > set cpoptions =aABceFst the whitespace immediately before the "=" sign is ignored. But there can be no whitespace after the "=" sign! To include a whitespace character in the value of an option, it must be escaped by a "\" (backslash) as in the following example: > set tags=my\ nice\ file The same example written as > set tags=my nice file will issue an error, because it is interpreted as: > set tags=my > set nice > set file COMMENTS ~ The character " (the double quote mark) starts a comment. Everything after and including this character until the end-of-line is considered a comment and is ignored, except for commands that don't consider comments, as shown in examples 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 o#include " insert include > execute cmd " do it > !ls *.c " list C files The abbreviation 'dev' will be expanded to 'development " shorthand'. The mapping of will actually be the whole line after the 'o# ....' including the '" insert include'. The "execute" command will give an error. The "!" command will send everything after it to the shell, causing an error for an unmatched '"' 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 o#include|" insert include > execute cmd |" do it With the '|' character the command is separated from the next one. And that next command is only a comment. Note that there is no white space before the '|' in the abbreviation and mapping. For these commands, any character until the end-of-line or '|' is included. As a consequence of this behavior, you don't always see that trailing whitespace is included: > map o#include To avoid these problems, you can set the 'list' option when editing vimrc files. PITFALLS ~ Even bigger problem arises in the following example: > map ,ab o#include > unmap ,ab Here 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 try to unmap ',ab ', which does not exist as a mapped sequence. An error will be issued, which is very hard to identify, because the ending whitespace character 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 " comment Here the comment part will be ignored. However, Vim will try to unmap ',ab ', which does not exist, Deleting the comment as well as all the whitespaces up the ending 'b' character will cure the problem. Except for the situations as above, it is legal to put a comment on the same line as the Vim definitions, such as > set number " display line numbers NORMAL MODE COMMANDS ~ To execute normal mode commands, the |:normal| command can be used. To avoid trouble with special characters, use the |:execute| command. Its argument is an expression (more about that further down). Example: > exe "normal mxGOA test.\`x" This appends "A test." below the last line in the file and returns the cursor to where it was. Note the use of "\", which is translated into the escape character. To use a backslash or double quote here you have to put a backslash before it: > exe "normal \"aYgg\"aP" This yanks the current line and puts it above the first line in the file. To do this without changing the cursor position and the text displayed in the window, 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 position IF - 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" > endif The argument for the "if" command is an expression. If this expression evaluates to true (non-zero), the statements until the "endif" are executed. If the expression evaluates to zero, these commands are skipped. The |:echo| command prints its argument, which is also an expression. Many constructions can be used in an expression. In the example we see this: &term The value of the 'term' option. A name prepended with '&' is the value of the option by that name. == "equal", compares the strings or numbers on each side, and evaluates to true when they are equal. "xterm" A literal string. For more information about expressions, see |expression|. You can also use "else": > if &tabstop != 8 > echo "tabstop is " . &tabstop > else > echo "tabstop is OK" > endif Note that a dot is used in the first echo command, to concatenate the literal string with the value of the 'tabstop' option. It is automatically converted from a number to a string, so that it can be concatenated. WHILE - ENDWHILE ~ To repeat a sequence of commands the |:while| command can be used: > let i = 1 > while i < 10 > echo i > let i = i + 1 > endwhile This uses the internal variable "i". An internal variable is used just like in many programming languages. The |:let| command is used to assign a value to a variable. The variable need not be declared and can hold a number or a string. The statements between the "while" and the "endwhile" are executed nine times. Note that the variable "i" is incremented with the "let i = i + 1" command. If you would forget this, the loop would run forever! Fortunately you can interrupt it with a CTRL-C (CTRL-Break on DOS). BUILTIN FUNCTIONS ~ Vim has builtin functions that you can use. Example: > echo exists("did_my_inits") This will print a one when the variable "did_my_inits" exists, and a zero if it doesn't exist. > if has("gui_running") > syntax on > endif This will switch on syntax highlighting if the GUI is started. The has() function can be used to check if a certain feature is supported. > call append(0, hostname() . " - " . strftime("%Y %b %d")) This inserts a line with the name of the computer and the current date at the start of the current buffer. There are many more functions, which can be used to accomplish most things you would want to do in Vim, which are too complicated to do with normal commands. See |functions|. USER DEFINED FUNCTIONS ~ You will often have a number of commands that you use in several places. You can define a function for these, and call it where you want. Example: > function My_Func(name) > if a:name ==? "john" > let s = "Hello " > else > let s = "I don't know " > endif > echo s . a:name . "." > endfun Explanation: The function "My_Func" is defined with one argument. "a:name" is used to refer to that argument. The "==?" operator is used to compare the argument with "john" while ignoring case. You could call it like this: > call My_Func("John") > Hello John. > call My_Func("James") > I don't know James. Note that the name of a user function always starts with a capital letter. There can be any number of arguments. See |:function|. PACKAGING ~ To avoid your function names to interfere with functions that you get from others, use this scheme: - Prepend a unique string before each function name. I often use an abbreviation. For example, "OW_" is used for the option window functions. - Put the definition of your functions together in a file. Set a global variable to indicate that the functions have been loaded. When sourcing the file again, first unload the functions. Example: > " This is the XXX package > > if exists("XXX_loaded") > delfun XXX_one > delfun XXX_two > endif > > function XXX_one(a) > ... body of function ... > endfun > > function XXX_two(b) > ... body of function ... > endfun > > let XXX_loaded = 1 AUTOCOMMANDS ~ You might want to set options or define mappings when editing a certain kind of file. Fortunatly, Vim already recognizes a lot of file types. This is used for syntax highlighting, but you can use it for other things too. You need to have syntax enabled |:syn-on|, or use this command: > filetype on For example, this autocommand sets the 'cindent' option in a C program file: > au FileType c set cindent This autocommand consists of three parts: FileType the name of the event which is used c the name of the file type detected set cindent the command executed when the event happens and the file type is "c" See |:autocmd| for more information. This works fine for the 'cindent' option, which is local to a buffer. When this is used for defining a mapping, this will not work properly when there are two windows. For example, if you are editing foo.txt in one window and foo.c in another, a mapping defined for foo.c will also be used in foo.txt. To make the mapping only work in the foo.c file, it needs to be removed as soon as the foo.c file is no longer the current buffer. That can be done in this way: > au BufEnter * if &ft == "c" | call CM_map() | endif > au BufLeave * if &ft == "c" | call CM_unmap() | endif > fun CM_map() > set cindent > imap { { } > endfun > fun CM_unmap() > iunmap { > endfun One function is used here to define the mapping and another to remove it. This makes it possible to add more mappings, without changing the autocommands. The use of functions makes it possible to keep the autocommands short. With the '|' character commands are separated. In the ":imap" command the <> codes are used to avoid the need for control characters. This is easy to type and to read back. The " is used to avoid that the indent is removed when is typed. ============================================================================== 7. The viminfo file *viminfo-file* If you exit Vim and later start it again, you would normally lose a lot of information. The viminfo file can be used to remember that information, which enables you to continue where you left off. The viminfo file is used to store: - The command line history. - The search string history. - The input-line history. - Contents of registers. - Marks for several files. - File marks, pointing to locations in files. - Last search/substitute pattern (for 'n' and '&'). - The buffer list. - Global variables. The viminfo file is not supported when the |+viminfo| feature has been disabled at compile time. You could also use a session file. The difference is that the viminfo file does not depend on what you are working on. There normally is only one viminfo file. Session files are used to save the state of a specific editing session. You could have several session files, one for each project you are working on. Viminfo and session files together can be used to effectively enter Vim and directly start working in your desired setup. |session-file| *viminfo-read* When Vim is started and the 'viminfo' option is non-empty, the contents of the viminfo file are read and the info can be used in the appropriate places. The marks are not read in at startup (but file marks are). See |initialization| for how to set the 'viminfo' option upon startup. *viminfo-write* When Vim exits and 'viminfo' is non-empty, the info is stored in the viminfo file (it's actually merged with the existing one, if one exists). The 'viminfo' option is a string containing information about what info should be stored, and contains limits on how much should be stored (see 'viminfo'). Notes for Unix: - The file protection for the viminfo file will be set to prevent other users from being able to read it, because it may contain any text or commands that you have worked with. - If you want to share the viminfo file with other users (e.g. when you "su" to another user), you can make the file writable for the group or everybody. Vim will preserve this when writing new viminfo files. Be careful, don't allow just anybody to read and write your viminfo file! - Vim will not overwrite a viminfo file that is not writable by the current "real" user. This helps for when you did "su" to become root, but your $HOME is still set to a normal user's home directory. Otherwise Vim would create a viminfo file owned by root that nobody else can read. Marks are stored for each file separately. When a file is read and 'viminfo' is non-empty, the marks for that file are read from the viminfo file. NOTE: The marks are only written when exiting Vim, which is fine because marks are remembered for all the files you have opened in the current editing session, unless ":bdel" is used. If you want to save the marks for a file that you are about to abandon with ":bdel", use ":wv". The '[' and ']' marks are not stored, but the '"' mark is. The '"' mark is very useful for jumping to the cursor position when the file was last exited. No marks are saved for files that start with any string given with the "r" flag in 'viminfo'. This can be used to avoid saving marks for files on removable media (for MS-DOS you would use "ra:,rb:", for Amiga "rdf0:,rdf1:,rdf2:"). *viminfo-file-marks* Uppercase marks ('A to 'Z) are stored when writing the viminfo file. The numbered marks ('0 to '9) are a bit special. When the viminfo file is written (when exiting or with the ":wviminfo" command), '0 is set to the current cursor position and file. The old '0 is moved to '1, '1 to '2, etc. This resembles what happens with the "1 to "9 delete registers. If the current cursor position is already present in '0 to '9, it is moved to '0, to avoid having the same position twice. The result is that with "'0", you can jump back to the file and line where you exited Vim. To do that right away, try using this command: > vim -c "normal '0" In (c)sh, you could make an alias for it: > alias lvim vim -c '"'normal "'"0'"' Viminfo file name: *viminfo-file-name* - The default name of the viminfo file is "$HOME/.viminfo" for Unix and OS/2, "s:.viminfo" for Amiga, "$HOME\_viminfo" for MS-DOS and Win32. For the last two, when $HOME is not set, "$VIM\_viminfo" is used. When $VIM is also not set, "c:\_viminfo" is used. For OS/2 "$VIM/.viminfo" is used when $HOME is not set and $VIM is set. - The 'n' flag in the 'viminfo' option can be used to specify another viminfo file name |'viminfo'|. - The "-i" Vim argument can be used to set another file name, |-i|. When the file name given is "NONE" (all uppercase), no viminfo file is ever read or written. Also not for the commands below! - For the commands below, another file name can be given, overriding the default and the name given with 'viminfo' or "-i" (unless it's NONE). Two commands can be used to read and write the viminfo file manually. This can be used to exchange registers between two running Vim programs: First type ":wv" in one and then ":rv" in the other. Note that if the register already contained something, then ":rv!" would be required. Also note however that this means everything will be overwritten with information from the first Vim, including the command line history, etc. The viminfo file itself can be edited by hand too, although we suggest you start with an existing one to get the format right. It is reasonably self-explanatory once you're in there. This can be useful in order to create a second file, say "~/.my_viminfo" which could contain certain settings that you always want when you first start Vim. For example, you can preload registers with particular data, or put certain commands in the command line history. A line in your .vimrc file like > rviminfo! ~/.my_viminfo can be used to load this information. You could even have different viminfos for different types of files (e.g., C code) and load them based on the file name, using the ":autocmd" command (see |:autocmd|). *viminfo-errors* When Vim detects an error while reading a viminfo file, it will not overwrite that file. If there are more than 10 errors, Vim stops reading the viminfo file. This was done to avoid accidentally destroying a file when the file name of the viminfo file is wrong. This could happen when accidentally typing "vim -i file" when you wanted "vim -R file" (yes, somebody accidentally did that!). If you want to overwrite a viminfo file with an error in it, you will either have to fix the error, or delete the file (while Vim is running, so most of the information will be restored). *:rv* *:rviminfo* :rv[iminfo][!] [file] Read from viminfo file [file] (default: see above). If [!] is given, then any information that is already set (registers, marks, etc.) will be overwritten. {not in Vi} *:wv* *:wviminfo* :wv[iminfo][!] [file] Write to viminfo file [file] (default: see above). The information in the file is first read in to make a merge between old and new info. When [!] is used, the old information is not read first, only the internal info is written. If 'viminfo' is empty, marks for up to 100 files will be written. {not in Vi} vim:tw=78:ts=8:sw=8: