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