1*usr_05.txt* For Vim version 7.0aa. Last change: 2004 Aug 27 2 3 VIM USER MANUAL - by Bram Moolenaar 4 5 Set your settings 6 7 8Vim can be tuned to work like you want it to. This chapter shows you how to 9make Vim start with options set to different values. Add plugins to extend 10Vims capabilities. Or define your own macros. 11 12|05.1| The vimrc file 13|05.2| The example vimrc file explained 14|05.3| Simple mappings 15|05.4| Adding a plugin 16|05.5| Adding a help file 17|05.6| The option window 18|05.7| Often used options 19 20 Next chapter: |usr_06.txt| Using syntax highlighting 21 Previous chapter: |usr_04.txt| Making small changes 22Table of contents: |usr_toc.txt| 23 24============================================================================== 25*05.1* The vimrc file *vimrc-intro* 26 27You probably got tired of typing commands that you use very often. To start 28with all your favorite option settings and mappings, you write them in what is 29called the vimrc file. Vim reads this file when it starts up. 30 31If you have trouble finding your vimrc file, use this command: > 32 33 :scriptnames 34 35One of the first files in the list should be called ".vimrc" or "_vimrc" and 36is located in your home directory. 37 If you don't have a vimrc file yet, see |vimrc| to find out where you can 38create a vimrc file. Also, the ":version" command mentions the name of the 39"user vimrc file" Vim looks for. 40 41For Unix this file is always used: > 42 43 ~/.vimrc 44 45For MS-DOS and MS-Windows it is mostly one of these: > 46 47 $HOME/_vimrc 48 $VIM/_vimrc 49 50The vimrc file can contain all the commands that you type after a colon. The 51most simple ones are for setting options. For example, if you want Vim to 52always start with the 'incsearch' option on, add this line you your vimrc 53file: > 54 55 set incsearch 56 57For this new line to take effect you need to exit Vim and start it again. 58Later you will learn how to do this without exiting Vim. 59 60This chapter only explains the most basic items. For more information on how 61to write a Vim script file: |usr_41.txt|. 62 63============================================================================== 64*05.2* The example vimrc file explained *vimrc_example.vim* 65 66In the first chapter was explained how the example vimrc (included in the 67Vim distribution) file can be used to make Vim startup in not-compatible mode 68(see |not-compatible|). The file can be found here: 69 70 $VIMRUNTIME/vimrc_example.vim ~ 71 72In this section we will explain the various commands used in this file. This 73will give you hints about how to set up your own preferences. Not everything 74will be explained though. Use the ":help" command to find out more. 75 76> 77 set nocompatible 78 79As mentioned in the first chapter, these manuals explain Vim working in an 80improved way, thus not completely Vi compatible. Setting the 'compatible' 81option off, thus 'nocompatible' takes care of this. 82 83> 84 set backspace=indent,eol,start 85 86This specifies where in Insert mode the <BS> is allowed to delete the 87character in front of the cursor. The three items, separated by commas, tell 88Vim to delete the white space at the start of the line, a line break and the 89character before where Insert mode started. 90> 91 92 set autoindent 93 94This makes Vim use the indent of the previous line for a newly created line. 95Thus there is the same amount of white space before the new line. For example 96when pressing <Enter> in Insert mode, and when using the "o" command to open a 97new line. 98> 99 100 if has("vms") 101 set nobackup 102 else 103 set backup 104 endif 105 106This tells Vim to keep a backup copy of a file when overwriting it. But not 107on the VMS system, since it keeps old versions of files already. The backup 108file will have the same name as the original file with "~" added. See |07.4| 109> 110 111 set history=50 112 113Keep 50 commands and 50 search patterns in the history. Use another number if 114you want to remember fewer or more lines. 115> 116 117 set ruler 118 119Always display the current cursor position in the lower right corner of the 120Vim window. 121 122> 123 set showcmd 124 125Display an incomplete command in the lower right corner of the Vim window, 126left of the ruler. For example, when you type "2f", Vim is waiting for you to 127type the character to find and "2f" is displayed. When you press "w" next, 128the "2fw" command is executed and the displayed "2f" is removed. 129 130 +-------------------------------------------------+ 131 |text in the Vim window | 132 |~ | 133 |~ | 134 |-- VISUAL -- 2f 43,8 17% | 135 +-------------------------------------------------+ 136 ^^^^^^^^^^^ ^^^^^^^^ ^^^^^^^^^^ 137 'showmode' 'showcmd' 'ruler' 138 139> 140 set incsearch 141 142Display the match for a search pattern when halfway typing it. 143 144> 145 map Q gq 146 147This defines a key mapping. More about that in the next section. This 148defines the "Q" command to do formatting with the "gq" operator. This is how 149it worked before Vim 5.0. Otherwise the "Q" command starts Ex mode, but you 150will not need it. 151 152> 153 vnoremap p <Esc>:let current_reg = @"<CR>gvs<C-R>=current_reg<CR><Esc> 154 155This is a complicated mapping. It will not be explained how it works here. 156What it does is to make "p" in Visual mode overwrite the selected text with 157the previously yanked text. You can see that mappings can be used to do quite 158complicated things. Still, it is just a sequence of commands that are 159executed like you typed them. 160 161> 162 if &t_Co > 2 || has("gui_running") 163 syntax on 164 set hlsearch 165 endif 166 167This switches on syntax highlighting, but only if colors are available. And 168the 'hlsearch' option tells Vim to highlight matches with the last used search 169pattern. The "if" command is very useful to set options only when some 170condition is met. More about that in |usr_41.txt|. 171 172 *vimrc-filetype* > 173 filetype plugin indent on 174 175This switches on three very clever mechanisms: 1761. Filetype detection. 177 Whenever you start editing a file, Vim will try to figure out what kind of 178 file this is. When you edit "main.c", Vim will see the ".c" extension and 179 recognize this as a "c" filetype. When you edit a file that starts with 180 "#!/bin/sh", Vim will recognize it as a "sh" filetype. 181 The filetype detection is used for syntax highlighting and the other two 182 items below. 183 See |filetypes|. 184 1852. Using filetype plugin files 186 Many different filetypes are edited with different options. For example, 187 when you edit a "c" file, it's very useful to set the 'cindent' option to 188 automatically indent the lines. These commonly useful option settings are 189 included with Vim in filetype plugins. You can also add your own, see 190 |write-filetype-plugin|. 191 1923. Using indent files 193 When editing programs, the indent of a line can often be computed 194 automatically. Vim comes with these indent rules for a number of 195 filetypes. See |:filetype-indent-on| and 'indentexpr'. 196 197> 198 autocmd FileType text setlocal textwidth=78 199 200This makes Vim break text to avoid lines getting longer than 78 characters. 201But only for files that have been detected to be plain text. There are 202actually two parts here. "autocmd FileType text" is an autocommand. This 203defines that when the file type is set to "text" the following command is 204automatically executed. "setlocal textwidth=78" sets the 'textwidth' option 205to 78, but only locally in one file. 206> 207 208 autocmd BufReadPost * 209 \ if line("'\"") > 0 && line("'\"") <= line("$") | 210 \ exe "normal g`\"" | 211 \ endif 212 213Another autocommand. This time it is used after reading any file. The 214complicated stuff after it checks if the '" mark is defined, and jumps to it 215if so. The backslash at the start of a line is used to continue the command 216from the previous line. That avoids a line getting very long. 217See |line-continuation|. This only works in a Vim script file, not when 218typing commands at the command-line. 219 220============================================================================== 221*05.3* Simple mappings 222 223A mapping enables you to bind a set of Vim commands to a single key. Suppose, 224for example, that you need to surround certain words with curly braces. In 225other words, you need to change a word such as "amount" into "{amount}". With 226the :map command, you can tell Vim that the F5 key does this job. The command 227is as follows: > 228 229 :map <F5> i{<Esc>ea}<Esc> 230< 231 Note: 232 When entering this command, you must enter <F5> by typing four 233 characters. Similarly, <Esc> is not entered by pressing the <Esc> 234 key, but by typing five characters. Watch out for this difference 235 when reading the manual! 236 237Let's break this down: 238 <F5> The F5 function key. This is the trigger key that causes the 239 command to be executed as the key is pressed. 240 241 i{<Esc> Insert the { character. The <Esc> key ends Insert mode. 242 243 e Move to the end of the word. 244 245 a}<Esc> Append the } to the word. 246 247After you execute the ":map" command, all you have to do to put {} around a 248word is to put the cursor on the first character and press F5. 249 250In this example, the trigger is a single key; it can be any string. But when 251you use an existing Vim command, that command will no longer be available. 252You better avoid that. 253 One key that can be used with mappings is the backslash. Since you 254probably want to define more than one mapping, add another character. You 255could map "\p" to add parens around a word, and "\c" to add curly braces, for 256example: > 257 258 :map \p i(<Esc>ea)<Esc> 259 :map \c i{<Esc>ea}<Esc> 260 261You need to type the \ and the p quickly after another, so that Vim knows they 262belong together. 263 264The ":map" command (with no arguments) lists your current mappings. At 265least the ones for Normal mode. More about mappings in section |40.1|. 266 267============================================================================== 268*05.4* Adding a plugin *add-plugin* *plugin* 269 270Vim's functionality can be extended by adding plugins. A plugin is nothing 271more than a Vim script file that is loaded automatically when Vim starts. You 272can add a plugin very easily by dropping it in your plugin directory. 273{not available when Vim was compiled without the |+eval| feature} 274 275There are two types of plugins: 276 277 global plugin: Used for all kinds of files 278 filetype plugin: Only used for a specific type of file 279 280The global plugins will be discussed first, then the filetype ones 281|add-filetype-plugin|. 282 283 284GLOBAL PLUGINS *standard-plugin* 285 286When you start Vim, it will automatically load a number of global plugins. 287You don't have to do anything for this. They add functionality that most 288people will want to use, but which was implemented as a Vim script instead of 289being compiled into Vim. You can find them listed in the help index 290|standard-plugin-list|. Also see |load-plugins|. 291 292 *add-global-plugin* 293You can add a global plugin to add functionality that will always be present 294when you use Vim. There are only two steps for adding a global plugin: 2951. Get a copy of the plugin. 2962. Drop it in the right directory. 297 298 299GETTING A GLOBAL PLUGIN 300 301Where can you find plugins? 302- Some come with Vim. You can find them in the directory $VIMRUNTIME/macros 303 and its sub-directories. 304- Download from the net, check out http://vim.sf.net. 305- They are sometimes posted in a Vim |maillist|. 306- You could write one yourself, see |write-plugin|. 307 308 309USING A GLOBAL PLUGIN 310 311First read the text in the plugin itself to check for any special conditions. 312Then copy the file to your plugin directory: 313 314 system plugin directory ~ 315 Unix ~/.vim/plugin/ 316 PC and OS/2 $HOME/vimfiles/plugin or $VIM/vimfiles/plugin 317 Amiga s:vimfiles/plugin 318 Macintosh $VIM:vimfiles:plugin 319 Mac OS X ~/.vim/plugin/ 320 RISC-OS Choices:vimfiles.plugin 321 322Example for Unix (assuming you didn't have a plugin directory yet): > 323 324 mkdir ~/.vim 325 mkdir ~/.vim/plugin 326 cp /usr/local/share/vim/vim60/macros/justify.vim ~/.vim/plugin 327 328That's all! Now you can use the commands defined in this plugin to justify 329text. 330 331 332FILETYPE PLUGINS *add-filetype-plugin* *ftplugins* 333 334The Vim distribution comes with a set of plugins for different filetypes that 335you can start using with this command: > 336 337 :filetype plugin on 338 339That's all! See |vimrc-filetype|. 340 341If you are missing a plugin for a filetype you are using, or you found a 342better one, you can add it. There are two steps for adding a filetype plugin: 3431. Get a copy of the plugin. 3442. Drop it in the right directory. 345 346 347GETTING A FILETYPE PLUGIN 348 349You can find them in the same places as the global plugins. Watch out if the 350type of file is mentioned, then you know if the plugin is a global or a 351filetype one. The scripts in $VIMRUNTIME/macros are global ones, the filetype 352plugins are in $VIMRUNTIME/ftplugin. 353 354 355USING A FILETYPE PLUGIN *ftplugin-name* 356 357You can add a filetype plugin by dropping it in the right directory. The 358name of this directory is in the same directory mentioned above for global 359plugins, but the last part is "ftplugin". Suppose you have found a plugin for 360the "stuff" filetype, and you are on Unix. Then you can move this file to the 361ftplugin directory: > 362 363 mv thefile ~/.vim/ftplugin/stuff.vim 364 365If that file already exists you already have a plugin for "stuff". You might 366want to check if the existing plugin doesn't conflict with the one you are 367adding. If it's OK, you can give the new one another name: > 368 369 mv thefile ~/.vim/ftplugin/stuff_too.vim 370 371The underscore is used to separate the name of the filetype from the rest, 372which can be anything. If you use "otherstuff.vim" it wouldn't work, it would 373be loaded for the "otherstuff" filetype. 374 375On MS-DOS you cannot use long filenames. You would run into trouble if you 376add a second plugin and the filetype has more than six characters. You can 377use an extra directory to get around this: > 378 379 mkdir $VIM/vimfiles/ftplugin/fortran 380 copy thefile $VIM/vimfiles/ftplugin/fortran/too.vim 381 382The generic names for the filetype plugins are: > 383 384 ftplugin/<filetype>.vim 385 ftplugin/<filetype>_<name>.vim 386 ftplugin/<filetype>/<name>.vim 387 388Here "<name>" can be any name that you prefer. 389Examples for the "stuff" filetype on Unix: > 390 391 ~/.vim/ftplugin/stuff.vim 392 ~/.vim/ftplugin/stuff_def.vim 393 ~/.vim/ftplugin/stuff/header.vim 394 395The <filetype> part is the name of the filetype the plugin is to be used for. 396Only files of this filetype will use the settings from the plugin. The <name> 397part of the plugin file doesn't matter, you can use it to have several plugins 398for the same filetype. Note that it must end in ".vim". 399 400 401Further reading: 402|filetype-plugins| Documentation for the filetype plugins and information 403 about how to avoid that mappings cause problems. 404|load-plugins| When the global plugins are loaded during startup. 405|ftplugin-overrule| Overruling the settings from a global plugin. 406|write-plugin| How to write a plugin script. 407|plugin-details| For more information about using plugins or when your 408 plugin doesn't work. 409|new-filetype| How to detect a new file type. 410 411============================================================================== 412*05.5* Adding a help file *add-local-help* *matchit-install* 413 414If you are lucky, the plugin you installed also comes with a help file. We 415will explain how to install the help file, so that you can easily find help 416for your new plugin. 417 Let us use the "matchit.vim" plugin as an example (it is included with 418Vim). This plugin makes the "%" command jump to matching HTML tags, 419if/else/endif in Vim scripts, etc. Very useful, although it's not backwards 420compatible (that's why it is not enabled by default). 421 This plugin comes with documentation: "matchit.txt". Let's first copy the 422plugin to the right directory. This time we will do it from inside Vim, so 423that we can use $VIMRUNTIME. (You may skip some of the "mkdir" commands if 424you already have the directory.) > 425 426 :!mkdir ~/.vim 427 :!mkdir ~/.vim/plugin 428 :!cp $VIMRUNTIME/macros/matchit.vim ~/.vim/plugin 429 430The "cp" command is for Unix, on MS-DOS you can use "copy". 431 432Now create a "doc" directory in one of the directories in 'runtimepath'. > 433 434 :!mkdir ~/.vim/doc 435 436Copy the help file to the "doc" directory. > 437 438 :!cp $VIMRUNTIME/macros/matchit.txt ~/.vim/doc 439 440Now comes the trick, which allows you to jump to the subjects in the new help 441file: Generate the local tags file with the |:helptags| command. > 442 443 :helptags ~/.vim/doc 444 445Now you can use the > 446 447 :help g% 448 449command to find help for "g%" in the help file you just added. You can see an 450entry for the local help file when you do: > 451 452 :help local-additions 453 454The title lines from the local help files are automagically added to this 455section. There you can see which local help files have been added and jump to 456them through the tag. 457 458For writing a local help file, see |write-local-help|. 459 460============================================================================== 461*05.6* The option window 462 463If you are looking for an option that does what you want, you can search in 464the help files here: |options|. Another way is by using this command: > 465 466 :options 467 468This opens a new window, with a list of options with a one-line explanation. 469The options are grouped by subject. Move the cursor to a subject and press 470<Enter> to jump there. Press <Enter> again to jump back. Or use CTRL-O. 471 472You can change the value of an option. For example, move to the "displaying 473text" subject. Then move the cursor down to this line: 474 475 set wrap nowrap ~ 476 477When you hit <Enter>, the line will change to: 478 479 set nowrap wrap ~ 480 481The option has now been switched off. 482 483Just above this line is a short description of the 'wrap' option. Move the 484cursor one line up to place it in this line. Now hit <Enter> and you jump to 485the full help on the 'wrap' option. 486 487For options that take a number or string argument you can edit the value. 488Then press <Enter> to apply the new value. For example, move the cursor a few 489lines up to this line: 490 491 set so=0 ~ 492 493Position the cursor on the zero with "$". Change it into a five with "r5". 494Then press <Enter> to apply the new value. When you now move the cursor 495around you will notice that the text starts scrolling before you reach the 496border. This is what the 'scrolloff' option does, it specifies an offset 497from the window border where scrolling starts. 498 499============================================================================== 500*05.7* Often used options 501 502There are an awful lot of options. Most of them you will hardly ever use. 503Some of the more useful ones will be mentioned here. Don't forget you can 504find more help on these options with the ":help" command, with single quotes 505before and after the option name. For example: > 506 507 :help 'wrap' 508 509In case you have messed up an option value, you can set it back to the 510default by putting an ampersand (&) after the option name. Example: > 511 512 :set iskeyword& 513 514 515NOT WRAPPING LINES 516 517Vim normally wraps long lines, so that you can see all of the text. Sometimes 518it's better to let the text continue right of the window. Then you need to 519scroll the text left-right to see all of a long line. Switch wrapping off 520with this command: > 521 522 :set nowrap 523 524Vim will automatically scroll the text when you move to text that is not 525displayed. To see a context of ten characters, do this: > 526 527 :set sidescroll=10 528 529This doesn't change the text in the file, only the way it is displayed. 530 531 532WRAPPING MOVEMENT COMMANDS 533 534Most commands for moving around will stop moving at the start and end of a 535line. You can change that with the 'whichwrap' option. This sets it to the 536default value: > 537 538 :set whichwrap=b,s 539 540This allows the <BS> key, when used in the first position of a line, to move 541the cursor to the end of the previous line. And the <Space> key moves from 542the end of a line to the start of the next one. 543 544To allow the cursor keys <Left> and <Right> to also wrap, use this command: > 545 546 :set whichwrap=b,s,<,> 547 548This is still only for Normal mode. To let <Left> and <Right> do this in 549Insert mode as well: > 550 551 :set whichwrap=b,s,<,>,[,] 552 553There are a few other flags that can be added, see 'whichwrap'. 554 555 556VIEWING TABS 557 558When there are tabs in a file, you cannot see where they are. To make them 559visible: > 560 561 :set list 562 563Now every Tab is displayed as ^I. And a $ is displayed at the end of each 564line, so that you can spot trailing spaces that would otherwise go unnoticed. 565 A disadvantage is that this looks ugly when there are many Tabs in a file. 566If you have a color terminal, or are using the GUI, Vim can show the spaces 567and tabs as highlighted characters. Use the 'listchars' option: > 568 569 :set listchars=tab:>-,trail:- 570 571Now every tab will be displayed as ">---" (with more or less "-") and trailing 572white space as "-". Looks a lot better, doesn't it? 573 574 575KEYWORDS 576 577The 'iskeyword' option specifies which characters can appear in a word: > 578 579 :set iskeyword 580< iskeyword=@,48-57,_,192-255 ~ 581 582The "@" stands for all alphabetic letters. "48-57" stands for ASCII 583characters 48 to 57, which are the numbers 0 to 9. "192-255" are the 584printable latin characters. 585 Sometimes you will want to include a dash in keywords, so that commands 586like "w" consider "upper-case" to be one word. You can do it like this: > 587 588 :set iskeyword+=- 589 :set iskeyword 590< iskeyword=@,48-57,_,192-255,- ~ 591 592If you look at the new value, you will see that Vim has added a comma for you. 593 To remove a character use "-=". For example, to remove the underscore: > 594 595 :set iskeyword-=_ 596 :set iskeyword 597< iskeyword=@,48-57,192-255,- ~ 598 599This time a comma is automatically deleted. 600 601 602ROOM FOR MESSAGES 603 604When Vim starts there is one line at the bottom that is used for messages. 605When a message is long, it is either truncated, thus you can only see part of 606it, or the text scrolls and you have to press <Enter> to continue. 607 You can set the 'cmdheight' option to the number of lines used for 608messages. Example: > 609 610 :set cmdheight=3 611 612This does mean there is less room to edit text, thus it's a compromise. 613 614============================================================================== 615 616Next chapter: |usr_06.txt| Using syntax highlighting 617 618Copyright: see |manual-copyright| vim:tw=78:ts=8:ft=help:norl: 619