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