Christian Brabandt | 6f6c0db | 2025-04-12 18:07:39 +0200 | [diff] [blame] | 1 | *usr_05.txt* For Vim version 9.1. Last change: 2025 Apr 10 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2 | |
| 3 | VIM USER MANUAL - by Bram Moolenaar |
| 4 | |
| 5 | Set your settings |
| 6 | |
| 7 | |
| 8 | Vim can be tuned to work like you want it to. This chapter shows you how to |
| 9 | make Vim start with options set to different values. Add plugins to extend |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 10 | Vim's capabilities. Or define your own macros. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11 | |
| 12 | |05.1| The vimrc file |
| 13 | |05.2| The example vimrc file explained |
Bram Moolenaar | 314dd79 | 2019-02-03 15:27:20 +0100 | [diff] [blame] | 14 | |05.3| The defaults.vim file explained |
| 15 | |05.4| Simple mappings |
| 16 | |05.5| Adding a package |
| 17 | |05.6| Adding a plugin |
| 18 | |05.7| Adding a help file |
| 19 | |05.8| The option window |
| 20 | |05.9| Often used options |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 21 | |
| 22 | Next chapter: |usr_06.txt| Using syntax highlighting |
| 23 | Previous chapter: |usr_04.txt| Making small changes |
| 24 | Table of contents: |usr_toc.txt| |
| 25 | |
| 26 | ============================================================================== |
| 27 | *05.1* The vimrc file *vimrc-intro* |
| 28 | |
| 29 | You probably got tired of typing commands that you use very often. To start |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 30 | Vim with all your favorite option settings and mappings, you write them in |
| 31 | what is called the vimrc file. Vim executes the commands in this file when it |
| 32 | starts up. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 33 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 34 | If you already have a vimrc file (e.g., when your sysadmin has one setup for |
| 35 | you), you can edit it this way: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 36 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 37 | :edit $MYVIMRC |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 38 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 39 | If you don't have a vimrc file yet, see |vimrc| to find out where you can |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 40 | create a vimrc file. Also, the ":version" command mentions the name of the |
| 41 | "user vimrc file" Vim looks for. |
| 42 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 43 | For Unix and Macintosh this file is always used and is recommended: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 44 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 45 | ~/.vimrc ~ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 46 | |
Bram Moolenaar | 5666fcd | 2019-12-26 14:35:26 +0100 | [diff] [blame] | 47 | For MS-Windows you can use one of these: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 48 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 49 | $HOME/_vimrc ~ |
| 50 | $VIM/_vimrc ~ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 51 | |
Bram Moolenaar | 22f1d0e | 2018-02-27 14:53:30 +0100 | [diff] [blame] | 52 | If you are creating the vimrc file for the first time, it is recommended to |
| 53 | put this line at the top: > |
| 54 | |
| 55 | source $VIMRUNTIME/defaults.vim |
| 56 | |
| 57 | This initializes Vim for new users (as opposed to traditional Vi users). See |
| 58 | |defaults.vim| for the details. |
| 59 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 60 | The vimrc file can contain all the commands that you type after a colon. The |
Bram Moolenaar | e7b1ea0 | 2020-08-07 19:54:59 +0200 | [diff] [blame] | 61 | simplest ones are for setting options. For example, if you want Vim to always |
| 62 | start with the 'incsearch' option on, add this line your vimrc file: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 63 | |
| 64 | set incsearch |
| 65 | |
| 66 | For this new line to take effect you need to exit Vim and start it again. |
| 67 | Later you will learn how to do this without exiting Vim. |
| 68 | |
| 69 | This chapter only explains the most basic items. For more information on how |
| 70 | to write a Vim script file: |usr_41.txt|. |
| 71 | |
| 72 | ============================================================================== |
| 73 | *05.2* The example vimrc file explained *vimrc_example.vim* |
| 74 | |
| 75 | In the first chapter was explained how the example vimrc (included in the |
| 76 | Vim distribution) file can be used to make Vim startup in not-compatible mode |
| 77 | (see |not-compatible|). The file can be found here: |
| 78 | |
| 79 | $VIMRUNTIME/vimrc_example.vim ~ |
| 80 | |
| 81 | In this section we will explain the various commands used in this file. This |
| 82 | will give you hints about how to set up your own preferences. Not everything |
| 83 | will be explained though. Use the ":help" command to find out more. |
| 84 | |
| 85 | > |
Bram Moolenaar | 314dd79 | 2019-02-03 15:27:20 +0100 | [diff] [blame] | 86 | " Get the defaults that most users want. |
| 87 | source $VIMRUNTIME/defaults.vim |
Bram Moolenaar | 2696761 | 2019-03-17 17:13:16 +0100 | [diff] [blame] | 88 | |
Bram Moolenaar | 314dd79 | 2019-02-03 15:27:20 +0100 | [diff] [blame] | 89 | This loads the "defaults.vim" file in the $VIMRUNTIME directory. This sets up |
| 90 | Vim for how most users like it. If you are one of the few that don't, then |
| 91 | comment out this line. The commands are explained below: |
| 92 | |defaults.vim-explained| |
| 93 | |
| 94 | > |
| 95 | if has("vms") |
| 96 | set nobackup |
| 97 | else |
| 98 | set backup |
| 99 | if has('persistent_undo') |
| 100 | set undofile |
| 101 | endif |
| 102 | endif |
| 103 | |
| 104 | This tells Vim to keep a backup copy of a file when overwriting it. But not |
| 105 | on the VMS system, since it keeps old versions of files already. The backup |
| 106 | file will have the same name as the original file with "~" added. See |07.4| |
| 107 | |
| 108 | This also sets the 'undofile' option, if available. This will store the |
| 109 | multi-level undo information in a file. The result is that when you change a |
| 110 | file, exit Vim, and then edit the file again, you can undo the changes made |
| 111 | previously. It's a very powerful and useful feature, at the cost of storing a |
| 112 | file. For more information see |undo-persistence|. |
| 113 | |
| 114 | The "if" command is very useful to set options |
| 115 | only when some condition is met. More about that in |usr_41.txt|. |
| 116 | |
| 117 | > |
| 118 | if &t_Co > 2 || has("gui_running") |
| 119 | set hlsearch |
| 120 | endif |
| 121 | |
| 122 | This switches on the 'hlsearch' option, telling Vim to highlight matches with |
| 123 | the last used search pattern. |
| 124 | |
| 125 | > |
| 126 | augroup vimrcEx |
| 127 | au! |
| 128 | autocmd FileType text setlocal textwidth=78 |
| 129 | augroup END |
| 130 | |
| 131 | This makes Vim break text to avoid lines getting longer than 78 characters. |
| 132 | But only for files that have been detected to be plain text. There are |
| 133 | actually two parts here. "autocmd FileType text" is an autocommand. This |
| 134 | defines that when the file type is set to "text" the following command is |
| 135 | automatically executed. "setlocal textwidth=78" sets the 'textwidth' option |
| 136 | to 78, but only locally in one file. |
| 137 | |
| 138 | The wrapper with "augroup vimrcEx" and "augroup END" makes it possible to |
| 139 | delete the autocommand with the "au!" command. See |:augroup|. |
| 140 | |
| 141 | > |
| 142 | if has('syntax') && has('eval') |
| 143 | packadd! matchit |
| 144 | endif |
| 145 | |
| 146 | This loads the "matchit" plugin if the required features are available. |
| 147 | It makes the |%| command more powerful. This is explained at |
| 148 | |matchit-install|. |
| 149 | |
| 150 | |
| 151 | ============================================================================== |
| 152 | *05.3* The defaults.vim file explained *defaults.vim-explained* |
| 153 | |
| 154 | The |defaults.vim| file is loaded when the user has no vimrc file. When you |
| 155 | create a new vimrc file, add this line near the top to keep using it: > |
| 156 | |
| 157 | source $VIMRUNTIME/defaults.vim |
| 158 | |
| 159 | Or use the vimrc_example.vim file, as explained above. |
| 160 | |
| 161 | The following explains what defaults.vim is doing. |
| 162 | |
| 163 | > |
| 164 | if exists('skip_defaults_vim') |
| 165 | finish |
| 166 | endif |
Bram Moolenaar | 2696761 | 2019-03-17 17:13:16 +0100 | [diff] [blame] | 167 | |
Bram Moolenaar | 314dd79 | 2019-02-03 15:27:20 +0100 | [diff] [blame] | 168 | Loading defaults.vim can be disabled with this command: > |
| 169 | let skip_defaults_vim = 1 |
| 170 | This has to be done in the system vimrc file. See |system-vimrc|. If you |
| 171 | have a user vimrc this is not needed, since defaults.vim will not be loaded |
| 172 | automatically. |
Bram Moolenaar | 2696761 | 2019-03-17 17:13:16 +0100 | [diff] [blame] | 173 | |
Bram Moolenaar | 314dd79 | 2019-02-03 15:27:20 +0100 | [diff] [blame] | 174 | > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 175 | set nocompatible |
| 176 | |
| 177 | As mentioned in the first chapter, these manuals explain Vim working in an |
| 178 | improved way, thus not completely Vi compatible. Setting the 'compatible' |
| 179 | option off, thus 'nocompatible' takes care of this. |
| 180 | |
| 181 | > |
| 182 | set backspace=indent,eol,start |
| 183 | |
| 184 | This specifies where in Insert mode the <BS> is allowed to delete the |
| 185 | character in front of the cursor. The three items, separated by commas, tell |
| 186 | Vim to delete the white space at the start of the line, a line break and the |
Bram Moolenaar | 314dd79 | 2019-02-03 15:27:20 +0100 | [diff] [blame] | 187 | character before where Insert mode started. See 'backspace'. |
| 188 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 189 | > |
Bram Moolenaar | 314dd79 | 2019-02-03 15:27:20 +0100 | [diff] [blame] | 190 | set history=200 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 191 | |
Bram Moolenaar | 314dd79 | 2019-02-03 15:27:20 +0100 | [diff] [blame] | 192 | Keep 200 commands and 200 search patterns in the history. Use another number |
| 193 | if you want to remember fewer or more lines. See 'history'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 194 | |
Bram Moolenaar | 2696761 | 2019-03-17 17:13:16 +0100 | [diff] [blame] | 195 | > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 196 | set ruler |
| 197 | |
| 198 | Always display the current cursor position in the lower right corner of the |
Bram Moolenaar | 314dd79 | 2019-02-03 15:27:20 +0100 | [diff] [blame] | 199 | Vim window. See 'ruler'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 200 | |
| 201 | > |
| 202 | set showcmd |
| 203 | |
| 204 | Display an incomplete command in the lower right corner of the Vim window, |
| 205 | left of the ruler. For example, when you type "2f", Vim is waiting for you to |
| 206 | type the character to find and "2f" is displayed. When you press "w" next, |
| 207 | the "2fw" command is executed and the displayed "2f" is removed. |
| 208 | |
| 209 | +-------------------------------------------------+ |
| 210 | |text in the Vim window | |
| 211 | |~ | |
| 212 | |~ | |
| 213 | |-- VISUAL -- 2f 43,8 17% | |
| 214 | +-------------------------------------------------+ |
| 215 | ^^^^^^^^^^^ ^^^^^^^^ ^^^^^^^^^^ |
| 216 | 'showmode' 'showcmd' 'ruler' |
| 217 | |
Bram Moolenaar | 314dd79 | 2019-02-03 15:27:20 +0100 | [diff] [blame] | 218 | |
| 219 | > |
| 220 | set wildmenu |
| 221 | |
| 222 | Display completion matches in a status line. That is when you type <Tab> and |
| 223 | there is more than one match. See 'wildmenu'. |
| 224 | |
| 225 | > |
| 226 | set ttimeout |
| 227 | set ttimeoutlen=100 |
| 228 | |
| 229 | This makes typing Esc take effect more quickly. Normally Vim waits a second |
| 230 | to see if the Esc is the start of an escape sequence. If you have a very slow |
| 231 | remote connection, increase the number. See 'ttimeout'. |
| 232 | |
| 233 | > |
| 234 | set display=truncate |
| 235 | |
| 236 | Show @@@ in the last line if it is truncated, instead of hiding the whole |
Bram Moolenaar | 68e6560 | 2019-05-26 21:33:31 +0200 | [diff] [blame] | 237 | line. See 'display'. |
Bram Moolenaar | 314dd79 | 2019-02-03 15:27:20 +0100 | [diff] [blame] | 238 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 239 | > |
| 240 | set incsearch |
| 241 | |
Bram Moolenaar | 314dd79 | 2019-02-03 15:27:20 +0100 | [diff] [blame] | 242 | Display the match for a search pattern when halfway typing it. See |
| 243 | 'incsearch'. |
| 244 | |
| 245 | > |
| 246 | set nrformats-=octal |
| 247 | |
| 248 | Do not recognize numbers starting with a zero as octal. See 'nrformats'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 249 | |
| 250 | > |
| 251 | map Q gq |
| 252 | |
| 253 | This defines a key mapping. More about that in the next section. This |
| 254 | defines the "Q" command to do formatting with the "gq" operator. This is how |
| 255 | it worked before Vim 5.0. Otherwise the "Q" command starts Ex mode, but you |
| 256 | will not need it. |
| 257 | |
| 258 | > |
Bram Moolenaar | 314dd79 | 2019-02-03 15:27:20 +0100 | [diff] [blame] | 259 | inoremap <C-U> <C-G>u<C-U> |
Bram Moolenaar | 2696761 | 2019-03-17 17:13:16 +0100 | [diff] [blame] | 260 | |
Bram Moolenaar | 314dd79 | 2019-02-03 15:27:20 +0100 | [diff] [blame] | 261 | CTRL-U in insert mode deletes all entered text in the current line. Use |
| 262 | CTRL-G u to first break undo, so that you can undo CTRL-U after inserting a |
| 263 | line break. Revert with ":iunmap <C-U>". |
| 264 | |
| 265 | > |
| 266 | if has('mouse') |
| 267 | set mouse=a |
| 268 | endif |
| 269 | |
| 270 | Enable using the mouse if available. See 'mouse'. |
| 271 | |
| 272 | > |
Bram Moolenaar | c51cf03 | 2022-02-26 12:25:45 +0000 | [diff] [blame] | 273 | vnoremap _g y:exe "grep /" .. escape(@", '\\/') .. "/ *.c *.h"<CR> |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 274 | |
Bram Moolenaar | 8fc061c | 2004-12-29 21:03:02 +0000 | [diff] [blame] | 275 | This mapping yanks the visually selected text and searches for it in C files. |
Bram Moolenaar | 314dd79 | 2019-02-03 15:27:20 +0100 | [diff] [blame] | 276 | You can see that a mapping can be used to do quite complicated things. Still, |
| 277 | it is just a sequence of commands that are executed like you typed them. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 278 | |
| 279 | > |
Bram Moolenaar | 314dd79 | 2019-02-03 15:27:20 +0100 | [diff] [blame] | 280 | syntax on |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 281 | |
Bram Moolenaar | 314dd79 | 2019-02-03 15:27:20 +0100 | [diff] [blame] | 282 | Enable highlighting files in color. See |syntax|. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 283 | |
| 284 | *vimrc-filetype* > |
| 285 | filetype plugin indent on |
| 286 | |
| 287 | This switches on three very clever mechanisms: |
| 288 | 1. Filetype detection. |
| 289 | Whenever you start editing a file, Vim will try to figure out what kind of |
| 290 | file this is. When you edit "main.c", Vim will see the ".c" extension and |
| 291 | recognize this as a "c" filetype. When you edit a file that starts with |
| 292 | "#!/bin/sh", Vim will recognize it as a "sh" filetype. |
| 293 | The filetype detection is used for syntax highlighting and the other two |
| 294 | items below. |
| 295 | See |filetypes|. |
| 296 | |
| 297 | 2. Using filetype plugin files |
| 298 | Many different filetypes are edited with different options. For example, |
| 299 | when you edit a "c" file, it's very useful to set the 'cindent' option to |
| 300 | automatically indent the lines. These commonly useful option settings are |
| 301 | included with Vim in filetype plugins. You can also add your own, see |
| 302 | |write-filetype-plugin|. |
| 303 | |
| 304 | 3. Using indent files |
| 305 | When editing programs, the indent of a line can often be computed |
| 306 | automatically. Vim comes with these indent rules for a number of |
| 307 | filetypes. See |:filetype-indent-on| and 'indentexpr'. |
| 308 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 309 | |
Christian Brabandt | 6f6c0db | 2025-04-12 18:07:39 +0200 | [diff] [blame] | 310 | *restore-cursor* *last-position-jump* >vim |
Dragan Simic' via vim_dev | 81b8bf5 | 2023-08-09 17:23:58 +0200 | [diff] [blame] | 311 | augroup RestoreCursor |
| 312 | autocmd! |
| 313 | autocmd BufReadPost * |
| 314 | \ let line = line("'\"") |
| 315 | \ | if line >= 1 && line <= line("$") && &filetype !~# 'commit' |
| 316 | \ && index(['xxd', 'gitrebase'], &filetype) == -1 |
Christian Brabandt | 6f6c0db | 2025-04-12 18:07:39 +0200 | [diff] [blame] | 317 | \ && !&diff |
Dragan Simic' via vim_dev | 81b8bf5 | 2023-08-09 17:23:58 +0200 | [diff] [blame] | 318 | \ | execute "normal! g`\"" |
| 319 | \ | endif |
| 320 | augroup END |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 321 | |
| 322 | Another autocommand. This time it is used after reading any file. The |
| 323 | complicated stuff after it checks if the '" mark is defined, and jumps to it |
Christian Brabandt | 6f6c0db | 2025-04-12 18:07:39 +0200 | [diff] [blame] | 324 | if so. It doesn't do that when: |
| 325 | - editing a commit or rebase message, which are likely a different one than |
| 326 | last time, |
| 327 | - using xxd(1) to filter and edit binary files, which transforms input files |
| 328 | back and forth, causing them to have dual nature, so to speak (see also |
| 329 | |using-xxd|) and |
| 330 | - Vim is in diff mode |
Dragan Simic' via vim_dev | 81b8bf5 | 2023-08-09 17:23:58 +0200 | [diff] [blame] | 331 | |
| 332 | The backslash at the start of a line is used to continue the command from the |
| 333 | previous line. That avoids a line getting very long. See |line-continuation|. |
| 334 | This only works in a Vim script file, not when typing commands at the |
| 335 | command line. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 336 | |
Bram Moolenaar | 314dd79 | 2019-02-03 15:27:20 +0100 | [diff] [blame] | 337 | > |
| 338 | command DiffOrig vert new | set bt=nofile | r ++edit # | 0d_ | diffthis |
| 339 | \ | wincmd p | diffthis |
| 340 | |
| 341 | This adds the ":DiffOrig" command. Use this in a modified buffer to see the |
Bram Moolenaar | 2696761 | 2019-03-17 17:13:16 +0100 | [diff] [blame] | 342 | differences with the file it was loaded from. See |diff| and |:DiffOrig|. |
Bram Moolenaar | 314dd79 | 2019-02-03 15:27:20 +0100 | [diff] [blame] | 343 | |
| 344 | > |
| 345 | set nolangremap |
| 346 | |
| 347 | Prevent that the langmap option applies to characters that result from a |
| 348 | mapping. If set (default), this may break plugins (but it's backward |
| 349 | compatible). See 'langremap'. |
| 350 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 351 | ============================================================================== |
Bram Moolenaar | 314dd79 | 2019-02-03 15:27:20 +0100 | [diff] [blame] | 352 | *05.4* Simple mappings |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 353 | |
| 354 | A mapping enables you to bind a set of Vim commands to a single key. Suppose, |
| 355 | for example, that you need to surround certain words with curly braces. In |
| 356 | other words, you need to change a word such as "amount" into "{amount}". With |
| 357 | the :map command, you can tell Vim that the F5 key does this job. The command |
| 358 | is as follows: > |
| 359 | |
| 360 | :map <F5> i{<Esc>ea}<Esc> |
| 361 | < |
| 362 | Note: |
| 363 | When entering this command, you must enter <F5> by typing four |
| 364 | characters. Similarly, <Esc> is not entered by pressing the <Esc> |
| 365 | key, but by typing five characters. Watch out for this difference |
| 366 | when reading the manual! |
| 367 | |
| 368 | Let's break this down: |
| 369 | <F5> The F5 function key. This is the trigger key that causes the |
| 370 | command to be executed as the key is pressed. |
| 371 | |
| 372 | i{<Esc> Insert the { character. The <Esc> key ends Insert mode. |
| 373 | |
| 374 | e Move to the end of the word. |
| 375 | |
| 376 | a}<Esc> Append the } to the word. |
| 377 | |
| 378 | After you execute the ":map" command, all you have to do to put {} around a |
| 379 | word is to put the cursor on the first character and press F5. |
| 380 | |
| 381 | In this example, the trigger is a single key; it can be any string. But when |
| 382 | you use an existing Vim command, that command will no longer be available. |
| 383 | You better avoid that. |
| 384 | One key that can be used with mappings is the backslash. Since you |
| 385 | probably want to define more than one mapping, add another character. You |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 386 | could map "\p" to add parentheses around a word, and "\c" to add curly braces, |
| 387 | for example: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 388 | |
| 389 | :map \p i(<Esc>ea)<Esc> |
| 390 | :map \c i{<Esc>ea}<Esc> |
| 391 | |
| 392 | You need to type the \ and the p quickly after another, so that Vim knows they |
| 393 | belong together. |
| 394 | |
| 395 | The ":map" command (with no arguments) lists your current mappings. At |
| 396 | least the ones for Normal mode. More about mappings in section |40.1|. |
| 397 | |
| 398 | ============================================================================== |
Christian Brabandt | 9598a63 | 2025-01-11 10:14:24 +0100 | [diff] [blame] | 399 | *05.5* Adding a package *add-package* *matchit-install* *package-matchit* |
Bram Moolenaar | aedfcbe | 2016-03-25 17:02:51 +0100 | [diff] [blame] | 400 | |
| 401 | A package is a set of files that you can add to Vim. There are two kinds of |
| 402 | packages: optional and automatically loaded on startup. |
| 403 | |
| 404 | The Vim distribution comes with a few packages that you can optionally use. |
| 405 | For example, the matchit plugin. This plugin makes the "%" command jump to |
| 406 | matching HTML tags, if/else/endif in Vim scripts, etc. Very useful, although |
| 407 | it's not backwards compatible (that's why it is not enabled by default). |
| 408 | |
| 409 | To start using the matchit plugin, add one line to your vimrc file: > |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 410 | packadd! matchit |
Bram Moolenaar | aedfcbe | 2016-03-25 17:02:51 +0100 | [diff] [blame] | 411 | |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 412 | That's all! After restarting Vim you can find help about this plugin: > |
Bram Moolenaar | aedfcbe | 2016-03-25 17:02:51 +0100 | [diff] [blame] | 413 | :help matchit |
| 414 | |
| 415 | This works, because when `:packadd` loaded the plugin it also added the |
| 416 | package directory in 'runtimepath', so that the help file can be found. |
| 417 | |
| 418 | You can find packages on the Internet in various places. It usually comes as |
| 419 | an archive or as a repository. For an archive you can follow these steps: |
| 420 | 1. create the package directory: > |
| 421 | mkdir -p ~/.vim/pack/fancy |
| 422 | < "fancy" can be any name of your liking. Use one that describes the |
| 423 | package. |
| 424 | 2. unpack the archive in that directory. This assumes the top |
| 425 | directory in the archive is "start": > |
Bram Moolenaar | 938ae28 | 2023-02-20 20:44:55 +0000 | [diff] [blame] | 426 | cd ~/.vim/pack/fancy |
Bram Moolenaar | aedfcbe | 2016-03-25 17:02:51 +0100 | [diff] [blame] | 427 | unzip /tmp/fancy.zip |
| 428 | < If the archive layout is different make sure that you end up with a |
| 429 | path like this: |
| 430 | ~/.vim/pack/fancy/start/fancytext/plugin/fancy.vim ~ |
| 431 | Here "fancytext" is the name of the package, it can be anything |
| 432 | else. |
| 433 | |
Christian Brabandt | 9598a63 | 2025-01-11 10:14:24 +0100 | [diff] [blame] | 434 | Adding the editorconfig package *editorconfig-install* *package-editorconfig* |
Christian Brabandt | 6ffcc58 | 2023-09-12 21:08:54 +0200 | [diff] [blame] | 435 | |
| 436 | Similar to the matchit package, to load the distributed editorconfig plugin |
| 437 | when Vim starts, add the following line to your vimrc file: > |
| 438 | packadd! editorconfig |
| 439 | |
| 440 | After restarting your Vim, the plugin is active and you can read about it at: > |
| 441 | :h editorconfig.txt |
| 442 | |
Maxim Kim | 5400a5d | 2024-04-26 19:53:13 +0200 | [diff] [blame] | 443 | |
Christian Brabandt | 83d7440 | 2025-03-19 21:55:59 +0100 | [diff] [blame] | 444 | Adding the comment package *comment-install* *package-comment* |
Maxim Kim | 5400a5d | 2024-04-26 19:53:13 +0200 | [diff] [blame] | 445 | |
| 446 | Load the plugin with this command: > |
Maxim Kim | 26de90c | 2024-06-18 19:32:39 +0200 | [diff] [blame] | 447 | packadd comment |
Maxim Kim | 5400a5d | 2024-04-26 19:53:13 +0200 | [diff] [blame] | 448 | < |
| 449 | This way you can use the plugin with the default key bindings `gc` and similar |
| 450 | for commenting (which is a well-established mapping in the Vim community). |
| 451 | |
Christian Brabandt | e595e9c | 2024-05-17 17:29:37 +0200 | [diff] [blame] | 452 | If you add this line to your vimrc file, then you need to restart Vim to have |
h-east | 8ee0e0b | 2024-10-05 16:44:27 +0200 | [diff] [blame] | 453 | the package loaded. Once the package is loaded, read about it at: > |
Maxim Kim | 5400a5d | 2024-04-26 19:53:13 +0200 | [diff] [blame] | 454 | :h comment.txt |
| 455 | |
Maxim Kim | 26de90c | 2024-06-18 19:32:39 +0200 | [diff] [blame] | 456 | |
Christian Brabandt | 83d7440 | 2025-03-19 21:55:59 +0100 | [diff] [blame] | 457 | Adding the nohlsearch package *nohlsearch-install* *package-nohlsearch* |
Maxim Kim | 26de90c | 2024-06-18 19:32:39 +0200 | [diff] [blame] | 458 | |
| 459 | Load the plugin with this command: > |
| 460 | packadd nohlsearch |
| 461 | < |
h-east | 52e7cc2 | 2024-07-28 17:03:29 +0200 | [diff] [blame] | 462 | Automatically execute |:nohlsearch| after 'updatetime' or getting into |
| 463 | |Insert| mode. |
Maxim Kim | 26de90c | 2024-06-18 19:32:39 +0200 | [diff] [blame] | 464 | Thus assuming default updatetime, hlsearch would be suspended/turned off after |
| 465 | 4 seconds of idle time. |
| 466 | |
Maxim Kim | aeca717 | 2024-06-19 19:42:47 +0200 | [diff] [blame] | 467 | To disable the effect of the plugin after it has been loaded: > |
Maxim Kim | 26de90c | 2024-06-18 19:32:39 +0200 | [diff] [blame] | 468 | au! nohlsearch |
| 469 | < |
| 470 | |
Christian Brabandt | 83d7440 | 2025-03-19 21:55:59 +0100 | [diff] [blame] | 471 | Adding the highlight-yank package *hlyank-install* *package-hlyank* |
| 472 | |
| 473 | Load the plugin with this command: > |
| 474 | packadd hlyank |
| 475 | < |
| 476 | This package briefly highlights the affected region of the last |yank| |
Hirohito Higashi | e14aa3c | 2025-03-22 15:58:59 +0100 | [diff] [blame] | 477 | command. See |52.6| for a simplified implementation using the |
| 478 | |getregionpos()| function. |
Christian Brabandt | 83d7440 | 2025-03-19 21:55:59 +0100 | [diff] [blame] | 479 | |
| 480 | The plugin understands the following configuration variables (the settings |
| 481 | show the default values). |
| 482 | |
| 483 | To specify a different highlighting group, use: > |
| 484 | :let g:hlyank_hlgroup = 'IncSearch' |
| 485 | < |
| 486 | To use a different highlighting duration, use: > |
| 487 | :let g:hlyank_duration = 300 |
Hirohito Higashi | e14aa3c | 2025-03-22 15:58:59 +0100 | [diff] [blame] | 488 | The unit is milliseconds, and the upper limit is 3000 ms. If you set a value |
| 489 | higher than this, the highlighting duration will be 3000 ms. |
| 490 | |
Christian Brabandt | 83d7440 | 2025-03-19 21:55:59 +0100 | [diff] [blame] | 491 | To highlight in visual mode, use: > |
| 492 | :let g:hlyank_invisual = v:true |
| 493 | |
| 494 | To disable the effect of the plugin after it has been loaded: > |
| 495 | au! hlyank |
Maxim Kim | 26de90c | 2024-06-18 19:32:39 +0200 | [diff] [blame] | 496 | |
Bram Moolenaar | aedfcbe | 2016-03-25 17:02:51 +0100 | [diff] [blame] | 497 | More information about packages can be found here: |packages|. |
| 498 | |
| 499 | ============================================================================== |
Bram Moolenaar | 314dd79 | 2019-02-03 15:27:20 +0100 | [diff] [blame] | 500 | *05.6* Adding a plugin *add-plugin* *plugin* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 501 | |
| 502 | Vim's functionality can be extended by adding plugins. A plugin is nothing |
| 503 | more than a Vim script file that is loaded automatically when Vim starts. You |
| 504 | can add a plugin very easily by dropping it in your plugin directory. |
| 505 | {not available when Vim was compiled without the |+eval| feature} |
| 506 | |
| 507 | There are two types of plugins: |
| 508 | |
| 509 | global plugin: Used for all kinds of files |
| 510 | filetype plugin: Only used for a specific type of file |
| 511 | |
| 512 | The global plugins will be discussed first, then the filetype ones |
| 513 | |add-filetype-plugin|. |
| 514 | |
| 515 | |
Christian Brabandt | 6ffcc58 | 2023-09-12 21:08:54 +0200 | [diff] [blame] | 516 | GLOBAL PLUGINS *standard-plugin* *distributed-plugins* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 517 | |
| 518 | When you start Vim, it will automatically load a number of global plugins. |
| 519 | You don't have to do anything for this. They add functionality that most |
| 520 | people will want to use, but which was implemented as a Vim script instead of |
| 521 | being compiled into Vim. You can find them listed in the help index |
Christian Brabandt | 6ffcc58 | 2023-09-12 21:08:54 +0200 | [diff] [blame] | 522 | |standard-plugin-list|. |
| 523 | |
| 524 | For locally installed plugins and packages (which come with a separated help |
| 525 | file) a similar list can be found in the help section |local-additions|. |
| 526 | |
| 527 | Also see |load-plugins|. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 528 | |
| 529 | *add-global-plugin* |
| 530 | You can add a global plugin to add functionality that will always be present |
| 531 | when you use Vim. There are only two steps for adding a global plugin: |
| 532 | 1. Get a copy of the plugin. |
| 533 | 2. Drop it in the right directory. |
| 534 | |
| 535 | |
| 536 | GETTING A GLOBAL PLUGIN |
| 537 | |
| 538 | Where can you find plugins? |
Bram Moolenaar | a9604e6 | 2018-07-21 05:56:22 +0200 | [diff] [blame] | 539 | - Some are always loaded, you can see them in the directory $VIMRUNTIME/plugin. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 540 | - Some come with Vim. You can find them in the directory $VIMRUNTIME/macros |
Bram Moolenaar | a9604e6 | 2018-07-21 05:56:22 +0200 | [diff] [blame] | 541 | and its sub-directories and under $VIM/vimfiles/pack/dist/opt/. |
Bram Moolenaar | 76b92b2 | 2006-03-24 22:46:53 +0000 | [diff] [blame] | 542 | - Download from the net. There is a large collection on http://www.vim.org. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 543 | - They are sometimes posted in a Vim |maillist|. |
| 544 | - You could write one yourself, see |write-plugin|. |
| 545 | |
Bram Moolenaar | 76b92b2 | 2006-03-24 22:46:53 +0000 | [diff] [blame] | 546 | Some plugins come as a vimball archive, see |vimball|. |
| 547 | Some plugins can be updated automatically, see |getscript|. |
| 548 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 549 | |
| 550 | USING A GLOBAL PLUGIN |
| 551 | |
| 552 | First read the text in the plugin itself to check for any special conditions. |
| 553 | Then copy the file to your plugin directory: |
| 554 | |
| 555 | system plugin directory ~ |
| 556 | Unix ~/.vim/plugin/ |
Bram Moolenaar | 6f345a1 | 2019-12-17 21:27:18 +0100 | [diff] [blame] | 557 | PC $HOME/vimfiles/plugin or $VIM/vimfiles/plugin |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 558 | Amiga s:vimfiles/plugin |
| 559 | Macintosh $VIM:vimfiles:plugin |
| 560 | Mac OS X ~/.vim/plugin/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 561 | |
| 562 | Example for Unix (assuming you didn't have a plugin directory yet): > |
| 563 | |
| 564 | mkdir ~/.vim |
| 565 | mkdir ~/.vim/plugin |
Bram Moolenaar | 7db8f6f | 2016-03-29 23:12:46 +0200 | [diff] [blame] | 566 | cp /tmp/yourplugin.vim ~/.vim/plugin |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 567 | |
Bram Moolenaar | 7db8f6f | 2016-03-29 23:12:46 +0200 | [diff] [blame] | 568 | That's all! Now you can use the commands defined in this plugin. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 569 | |
Bram Moolenaar | d5cdbeb | 2005-10-10 20:59:28 +0000 | [diff] [blame] | 570 | Instead of putting plugins directly into the plugin/ directory, you may |
| 571 | better organize them by putting them into subdirectories under plugin/. |
| 572 | As an example, consider using "~/.vim/plugin/perl/*.vim" for all your Perl |
| 573 | plugins. |
Bram Moolenaar | 07d4d73 | 2005-10-03 22:04:08 +0000 | [diff] [blame] | 574 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 575 | |
| 576 | FILETYPE PLUGINS *add-filetype-plugin* *ftplugins* |
| 577 | |
| 578 | The Vim distribution comes with a set of plugins for different filetypes that |
| 579 | you can start using with this command: > |
| 580 | |
| 581 | :filetype plugin on |
| 582 | |
| 583 | That's all! See |vimrc-filetype|. |
| 584 | |
| 585 | If you are missing a plugin for a filetype you are using, or you found a |
| 586 | better one, you can add it. There are two steps for adding a filetype plugin: |
| 587 | 1. Get a copy of the plugin. |
| 588 | 2. Drop it in the right directory. |
| 589 | |
| 590 | |
| 591 | GETTING A FILETYPE PLUGIN |
| 592 | |
| 593 | You can find them in the same places as the global plugins. Watch out if the |
| 594 | type of file is mentioned, then you know if the plugin is a global or a |
| 595 | filetype one. The scripts in $VIMRUNTIME/macros are global ones, the filetype |
| 596 | plugins are in $VIMRUNTIME/ftplugin. |
| 597 | |
| 598 | |
| 599 | USING A FILETYPE PLUGIN *ftplugin-name* |
| 600 | |
| 601 | You can add a filetype plugin by dropping it in the right directory. The |
| 602 | name of this directory is in the same directory mentioned above for global |
| 603 | plugins, but the last part is "ftplugin". Suppose you have found a plugin for |
| 604 | the "stuff" filetype, and you are on Unix. Then you can move this file to the |
| 605 | ftplugin directory: > |
| 606 | |
| 607 | mv thefile ~/.vim/ftplugin/stuff.vim |
| 608 | |
| 609 | If that file already exists you already have a plugin for "stuff". You might |
| 610 | want to check if the existing plugin doesn't conflict with the one you are |
| 611 | adding. If it's OK, you can give the new one another name: > |
| 612 | |
| 613 | mv thefile ~/.vim/ftplugin/stuff_too.vim |
| 614 | |
| 615 | The underscore is used to separate the name of the filetype from the rest, |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 616 | which can be anything. If you use "otherstuff.vim" it wouldn't work, it would |
| 617 | be loaded for the "otherstuff" filetype. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 618 | |
Bram Moolenaar | 5666fcd | 2019-12-26 14:35:26 +0100 | [diff] [blame] | 619 | On MS-DOS like filesystems you cannot use long filenames. You would run into |
| 620 | trouble if you add a second plugin and the filetype has more than six |
| 621 | characters. You can use an extra directory to get around this: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 622 | |
| 623 | mkdir $VIM/vimfiles/ftplugin/fortran |
| 624 | copy thefile $VIM/vimfiles/ftplugin/fortran/too.vim |
| 625 | |
| 626 | The generic names for the filetype plugins are: > |
| 627 | |
| 628 | ftplugin/<filetype>.vim |
| 629 | ftplugin/<filetype>_<name>.vim |
| 630 | ftplugin/<filetype>/<name>.vim |
| 631 | |
| 632 | Here "<name>" can be any name that you prefer. |
| 633 | Examples for the "stuff" filetype on Unix: > |
| 634 | |
| 635 | ~/.vim/ftplugin/stuff.vim |
| 636 | ~/.vim/ftplugin/stuff_def.vim |
| 637 | ~/.vim/ftplugin/stuff/header.vim |
| 638 | |
| 639 | The <filetype> part is the name of the filetype the plugin is to be used for. |
| 640 | Only files of this filetype will use the settings from the plugin. The <name> |
| 641 | part of the plugin file doesn't matter, you can use it to have several plugins |
| 642 | for the same filetype. Note that it must end in ".vim". |
| 643 | |
| 644 | |
| 645 | Further reading: |
| 646 | |filetype-plugins| Documentation for the filetype plugins and information |
| 647 | about how to avoid that mappings cause problems. |
| 648 | |load-plugins| When the global plugins are loaded during startup. |
| 649 | |ftplugin-overrule| Overruling the settings from a global plugin. |
| 650 | |write-plugin| How to write a plugin script. |
| 651 | |plugin-details| For more information about using plugins or when your |
| 652 | plugin doesn't work. |
| 653 | |new-filetype| How to detect a new file type. |
| 654 | |
| 655 | ============================================================================== |
Bram Moolenaar | 314dd79 | 2019-02-03 15:27:20 +0100 | [diff] [blame] | 656 | *05.7* Adding a help file *add-local-help* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 657 | |
| 658 | If you are lucky, the plugin you installed also comes with a help file. We |
| 659 | will explain how to install the help file, so that you can easily find help |
| 660 | for your new plugin. |
Bram Moolenaar | aedfcbe | 2016-03-25 17:02:51 +0100 | [diff] [blame] | 661 | Let us use the "doit.vim" plugin as an example. This plugin comes with |
| 662 | documentation: "doit.txt". Let's first copy the plugin to the right |
| 663 | directory. This time we will do it from inside Vim. (You may skip some of |
| 664 | the "mkdir" commands if you already have the directory.) > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 665 | |
| 666 | :!mkdir ~/.vim |
| 667 | :!mkdir ~/.vim/plugin |
Bram Moolenaar | aedfcbe | 2016-03-25 17:02:51 +0100 | [diff] [blame] | 668 | :!cp /tmp/doit.vim ~/.vim/plugin |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 669 | |
Bram Moolenaar | 5666fcd | 2019-12-26 14:35:26 +0100 | [diff] [blame] | 670 | The "cp" command is for Unix, on MS-Windows you can use "copy". |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 671 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 672 | Now create a "doc" directory in one of the directories in 'runtimepath'. > |
| 673 | |
| 674 | :!mkdir ~/.vim/doc |
| 675 | |
| 676 | Copy the help file to the "doc" directory. > |
| 677 | |
Bram Moolenaar | aedfcbe | 2016-03-25 17:02:51 +0100 | [diff] [blame] | 678 | :!cp /tmp/doit.txt ~/.vim/doc |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 679 | |
| 680 | Now comes the trick, which allows you to jump to the subjects in the new help |
| 681 | file: Generate the local tags file with the |:helptags| command. > |
| 682 | |
| 683 | :helptags ~/.vim/doc |
| 684 | |
| 685 | Now you can use the > |
| 686 | |
Bram Moolenaar | aedfcbe | 2016-03-25 17:02:51 +0100 | [diff] [blame] | 687 | :help doit |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 688 | |
Bram Moolenaar | aedfcbe | 2016-03-25 17:02:51 +0100 | [diff] [blame] | 689 | command to find help for "doit" in the help file you just added. You can see |
| 690 | an entry for the local help file when you do: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 691 | |
| 692 | :help local-additions |
| 693 | |
| 694 | The title lines from the local help files are automagically added to this |
| 695 | section. There you can see which local help files have been added and jump to |
| 696 | them through the tag. |
| 697 | |
| 698 | For writing a local help file, see |write-local-help|. |
| 699 | |
| 700 | ============================================================================== |
Bram Moolenaar | 314dd79 | 2019-02-03 15:27:20 +0100 | [diff] [blame] | 701 | *05.8* The option window |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 702 | |
| 703 | If you are looking for an option that does what you want, you can search in |
| 704 | the help files here: |options|. Another way is by using this command: > |
| 705 | |
| 706 | :options |
| 707 | |
| 708 | This opens a new window, with a list of options with a one-line explanation. |
| 709 | The options are grouped by subject. Move the cursor to a subject and press |
| 710 | <Enter> to jump there. Press <Enter> again to jump back. Or use CTRL-O. |
| 711 | |
| 712 | You can change the value of an option. For example, move to the "displaying |
| 713 | text" subject. Then move the cursor down to this line: |
| 714 | |
| 715 | set wrap nowrap ~ |
| 716 | |
| 717 | When you hit <Enter>, the line will change to: |
| 718 | |
| 719 | set nowrap wrap ~ |
| 720 | |
| 721 | The option has now been switched off. |
| 722 | |
| 723 | Just above this line is a short description of the 'wrap' option. Move the |
| 724 | cursor one line up to place it in this line. Now hit <Enter> and you jump to |
| 725 | the full help on the 'wrap' option. |
| 726 | |
| 727 | For options that take a number or string argument you can edit the value. |
| 728 | Then press <Enter> to apply the new value. For example, move the cursor a few |
| 729 | lines up to this line: |
| 730 | |
| 731 | set so=0 ~ |
| 732 | |
| 733 | Position the cursor on the zero with "$". Change it into a five with "r5". |
| 734 | Then press <Enter> to apply the new value. When you now move the cursor |
| 735 | around you will notice that the text starts scrolling before you reach the |
| 736 | border. This is what the 'scrolloff' option does, it specifies an offset |
| 737 | from the window border where scrolling starts. |
| 738 | |
| 739 | ============================================================================== |
Bram Moolenaar | 314dd79 | 2019-02-03 15:27:20 +0100 | [diff] [blame] | 740 | *05.9* Often used options |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 741 | |
| 742 | There are an awful lot of options. Most of them you will hardly ever use. |
| 743 | Some of the more useful ones will be mentioned here. Don't forget you can |
| 744 | find more help on these options with the ":help" command, with single quotes |
| 745 | before and after the option name. For example: > |
| 746 | |
| 747 | :help 'wrap' |
| 748 | |
| 749 | In case you have messed up an option value, you can set it back to the |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 750 | default by putting an ampersand (&) after the option name. Example: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 751 | |
| 752 | :set iskeyword& |
| 753 | |
| 754 | |
| 755 | NOT WRAPPING LINES |
| 756 | |
| 757 | Vim normally wraps long lines, so that you can see all of the text. Sometimes |
| 758 | it's better to let the text continue right of the window. Then you need to |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 759 | scroll the text left-right to see all of a long line. Switch wrapping off |
| 760 | with this command: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 761 | |
| 762 | :set nowrap |
| 763 | |
| 764 | Vim will automatically scroll the text when you move to text that is not |
| 765 | displayed. To see a context of ten characters, do this: > |
| 766 | |
| 767 | :set sidescroll=10 |
| 768 | |
| 769 | This doesn't change the text in the file, only the way it is displayed. |
| 770 | |
| 771 | |
| 772 | WRAPPING MOVEMENT COMMANDS |
| 773 | |
| 774 | Most commands for moving around will stop moving at the start and end of a |
| 775 | line. You can change that with the 'whichwrap' option. This sets it to the |
| 776 | default value: > |
| 777 | |
| 778 | :set whichwrap=b,s |
| 779 | |
| 780 | This allows the <BS> key, when used in the first position of a line, to move |
| 781 | the cursor to the end of the previous line. And the <Space> key moves from |
| 782 | the end of a line to the start of the next one. |
| 783 | |
| 784 | To allow the cursor keys <Left> and <Right> to also wrap, use this command: > |
| 785 | |
| 786 | :set whichwrap=b,s,<,> |
| 787 | |
| 788 | This is still only for Normal mode. To let <Left> and <Right> do this in |
| 789 | Insert mode as well: > |
| 790 | |
| 791 | :set whichwrap=b,s,<,>,[,] |
| 792 | |
| 793 | There are a few other flags that can be added, see 'whichwrap'. |
| 794 | |
| 795 | |
| 796 | VIEWING TABS |
| 797 | |
| 798 | When there are tabs in a file, you cannot see where they are. To make them |
| 799 | visible: > |
| 800 | |
| 801 | :set list |
| 802 | |
Bram Moolenaar | 1b826e5 | 2007-05-12 15:14:36 +0000 | [diff] [blame] | 803 | Now every tab is displayed as ^I. And a $ is displayed at the end of each |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 804 | line, so that you can spot trailing spaces that would otherwise go unnoticed. |
| 805 | A disadvantage is that this looks ugly when there are many Tabs in a file. |
| 806 | If you have a color terminal, or are using the GUI, Vim can show the spaces |
| 807 | and tabs as highlighted characters. Use the 'listchars' option: > |
| 808 | |
| 809 | :set listchars=tab:>-,trail:- |
| 810 | |
| 811 | Now every tab will be displayed as ">---" (with more or less "-") and trailing |
| 812 | white space as "-". Looks a lot better, doesn't it? |
| 813 | |
| 814 | |
| 815 | KEYWORDS |
| 816 | |
| 817 | The 'iskeyword' option specifies which characters can appear in a word: > |
| 818 | |
| 819 | :set iskeyword |
| 820 | < iskeyword=@,48-57,_,192-255 ~ |
| 821 | |
| 822 | The "@" stands for all alphabetic letters. "48-57" stands for ASCII |
| 823 | characters 48 to 57, which are the numbers 0 to 9. "192-255" are the |
| 824 | printable latin characters. |
| 825 | Sometimes you will want to include a dash in keywords, so that commands |
| 826 | like "w" consider "upper-case" to be one word. You can do it like this: > |
| 827 | |
| 828 | :set iskeyword+=- |
| 829 | :set iskeyword |
| 830 | < iskeyword=@,48-57,_,192-255,- ~ |
| 831 | |
| 832 | If you look at the new value, you will see that Vim has added a comma for you. |
| 833 | To remove a character use "-=". For example, to remove the underscore: > |
| 834 | |
| 835 | :set iskeyword-=_ |
| 836 | :set iskeyword |
| 837 | < iskeyword=@,48-57,192-255,- ~ |
| 838 | |
| 839 | This time a comma is automatically deleted. |
| 840 | |
| 841 | |
| 842 | ROOM FOR MESSAGES |
| 843 | |
| 844 | When Vim starts there is one line at the bottom that is used for messages. |
| 845 | When a message is long, it is either truncated, thus you can only see part of |
| 846 | it, or the text scrolls and you have to press <Enter> to continue. |
| 847 | You can set the 'cmdheight' option to the number of lines used for |
| 848 | messages. Example: > |
| 849 | |
| 850 | :set cmdheight=3 |
| 851 | |
| 852 | This does mean there is less room to edit text, thus it's a compromise. |
| 853 | |
| 854 | ============================================================================== |
| 855 | |
| 856 | Next chapter: |usr_06.txt| Using syntax highlighting |
| 857 | |
Bram Moolenaar | d473c8c | 2018-08-11 18:00:22 +0200 | [diff] [blame] | 858 | Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: |