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