blob: 0ab0301991ffd9eb7c4ad708b16826ab2d6424fd [file] [log] [blame]
Christian Brabandt596ad662023-08-16 00:11:09 +02001*usr_05.txt* For Vim version 9.0. Last change: 2023 Aug 10
Bram Moolenaar071d4272004-06-13 20:20:40 +00002
3 VIM USER MANUAL - by Bram Moolenaar
4
5 Set your settings
6
7
8Vim can be tuned to work like you want it to. This chapter shows you how to
9make Vim start with options set to different values. Add plugins to extend
Bram Moolenaar4399ef42005-02-12 14:29:27 +000010Vim's capabilities. Or define your own macros.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011
12|05.1| The vimrc file
13|05.2| The example vimrc file explained
Bram Moolenaar314dd792019-02-03 15:27:20 +010014|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 Moolenaar071d4272004-06-13 20:20:40 +000021
22 Next chapter: |usr_06.txt| Using syntax highlighting
23 Previous chapter: |usr_04.txt| Making small changes
24Table of contents: |usr_toc.txt|
25
26==============================================================================
27*05.1* The vimrc file *vimrc-intro*
28
29You probably got tired of typing commands that you use very often. To start
Bram Moolenaar910f66f2006-04-05 20:41:53 +000030Vim with all your favorite option settings and mappings, you write them in
31what is called the vimrc file. Vim executes the commands in this file when it
32starts up.
Bram Moolenaar071d4272004-06-13 20:20:40 +000033
Bram Moolenaar910f66f2006-04-05 20:41:53 +000034If you already have a vimrc file (e.g., when your sysadmin has one setup for
35you), you can edit it this way: >
Bram Moolenaar071d4272004-06-13 20:20:40 +000036
Bram Moolenaar910f66f2006-04-05 20:41:53 +000037 :edit $MYVIMRC
Bram Moolenaar071d4272004-06-13 20:20:40 +000038
Bram Moolenaar910f66f2006-04-05 20:41:53 +000039If you don't have a vimrc file yet, see |vimrc| to find out where you can
Bram Moolenaar071d4272004-06-13 20:20:40 +000040create a vimrc file. Also, the ":version" command mentions the name of the
41"user vimrc file" Vim looks for.
42
Bram Moolenaar910f66f2006-04-05 20:41:53 +000043For Unix and Macintosh this file is always used and is recommended:
Bram Moolenaar071d4272004-06-13 20:20:40 +000044
Bram Moolenaar910f66f2006-04-05 20:41:53 +000045 ~/.vimrc ~
Bram Moolenaar071d4272004-06-13 20:20:40 +000046
Bram Moolenaar5666fcd2019-12-26 14:35:26 +010047For MS-Windows you can use one of these:
Bram Moolenaar071d4272004-06-13 20:20:40 +000048
Bram Moolenaar910f66f2006-04-05 20:41:53 +000049 $HOME/_vimrc ~
50 $VIM/_vimrc ~
Bram Moolenaar071d4272004-06-13 20:20:40 +000051
Bram Moolenaar22f1d0e2018-02-27 14:53:30 +010052If you are creating the vimrc file for the first time, it is recommended to
53put this line at the top: >
54
55 source $VIMRUNTIME/defaults.vim
56
57This initializes Vim for new users (as opposed to traditional Vi users). See
58|defaults.vim| for the details.
59
Bram Moolenaar071d4272004-06-13 20:20:40 +000060The vimrc file can contain all the commands that you type after a colon. The
Bram Moolenaare7b1ea02020-08-07 19:54:59 +020061simplest ones are for setting options. For example, if you want Vim to always
62start with the 'incsearch' option on, add this line your vimrc file: >
Bram Moolenaar071d4272004-06-13 20:20:40 +000063
64 set incsearch
65
66For this new line to take effect you need to exit Vim and start it again.
67Later you will learn how to do this without exiting Vim.
68
69This chapter only explains the most basic items. For more information on how
70to write a Vim script file: |usr_41.txt|.
71
72==============================================================================
73*05.2* The example vimrc file explained *vimrc_example.vim*
74
75In the first chapter was explained how the example vimrc (included in the
76Vim 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
81In this section we will explain the various commands used in this file. This
82will give you hints about how to set up your own preferences. Not everything
83will be explained though. Use the ":help" command to find out more.
84
85>
Bram Moolenaar314dd792019-02-03 15:27:20 +010086 " Get the defaults that most users want.
87 source $VIMRUNTIME/defaults.vim
Bram Moolenaar26967612019-03-17 17:13:16 +010088
Bram Moolenaar314dd792019-02-03 15:27:20 +010089This loads the "defaults.vim" file in the $VIMRUNTIME directory. This sets up
90Vim for how most users like it. If you are one of the few that don't, then
91comment 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
104This tells Vim to keep a backup copy of a file when overwriting it. But not
105on the VMS system, since it keeps old versions of files already. The backup
106file will have the same name as the original file with "~" added. See |07.4|
107
108This also sets the 'undofile' option, if available. This will store the
109multi-level undo information in a file. The result is that when you change a
110file, exit Vim, and then edit the file again, you can undo the changes made
111previously. It's a very powerful and useful feature, at the cost of storing a
112file. For more information see |undo-persistence|.
113
114The "if" command is very useful to set options
115only 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
122This switches on the 'hlsearch' option, telling Vim to highlight matches with
123the last used search pattern.
124
125>
126 augroup vimrcEx
127 au!
128 autocmd FileType text setlocal textwidth=78
129 augroup END
130
131This makes Vim break text to avoid lines getting longer than 78 characters.
132But only for files that have been detected to be plain text. There are
133actually two parts here. "autocmd FileType text" is an autocommand. This
134defines that when the file type is set to "text" the following command is
135automatically executed. "setlocal textwidth=78" sets the 'textwidth' option
136to 78, but only locally in one file.
137
138The wrapper with "augroup vimrcEx" and "augroup END" makes it possible to
139delete the autocommand with the "au!" command. See |:augroup|.
140
141>
142 if has('syntax') && has('eval')
143 packadd! matchit
144 endif
145
146This loads the "matchit" plugin if the required features are available.
147It 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
154The |defaults.vim| file is loaded when the user has no vimrc file. When you
155create a new vimrc file, add this line near the top to keep using it: >
156
157 source $VIMRUNTIME/defaults.vim
158
159Or use the vimrc_example.vim file, as explained above.
160
161The following explains what defaults.vim is doing.
162
163>
164 if exists('skip_defaults_vim')
165 finish
166 endif
Bram Moolenaar26967612019-03-17 17:13:16 +0100167
Bram Moolenaar314dd792019-02-03 15:27:20 +0100168Loading defaults.vim can be disabled with this command: >
169 let skip_defaults_vim = 1
170This has to be done in the system vimrc file. See |system-vimrc|. If you
171have a user vimrc this is not needed, since defaults.vim will not be loaded
172automatically.
Bram Moolenaar26967612019-03-17 17:13:16 +0100173
Bram Moolenaar314dd792019-02-03 15:27:20 +0100174>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 set nocompatible
176
177As mentioned in the first chapter, these manuals explain Vim working in an
178improved way, thus not completely Vi compatible. Setting the 'compatible'
179option off, thus 'nocompatible' takes care of this.
180
181>
182 set backspace=indent,eol,start
183
184This specifies where in Insert mode the <BS> is allowed to delete the
185character in front of the cursor. The three items, separated by commas, tell
186Vim to delete the white space at the start of the line, a line break and the
Bram Moolenaar314dd792019-02-03 15:27:20 +0100187character before where Insert mode started. See 'backspace'.
188
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189>
Bram Moolenaar314dd792019-02-03 15:27:20 +0100190 set history=200
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191
Bram Moolenaar314dd792019-02-03 15:27:20 +0100192Keep 200 commands and 200 search patterns in the history. Use another number
193if you want to remember fewer or more lines. See 'history'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194
Bram Moolenaar26967612019-03-17 17:13:16 +0100195>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 set ruler
197
198Always display the current cursor position in the lower right corner of the
Bram Moolenaar314dd792019-02-03 15:27:20 +0100199Vim window. See 'ruler'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200
201>
202 set showcmd
203
204Display an incomplete command in the lower right corner of the Vim window,
205left of the ruler. For example, when you type "2f", Vim is waiting for you to
206type the character to find and "2f" is displayed. When you press "w" next,
207the "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 Moolenaar314dd792019-02-03 15:27:20 +0100218
219>
220 set wildmenu
221
222Display completion matches in a status line. That is when you type <Tab> and
223there is more than one match. See 'wildmenu'.
224
225>
226 set ttimeout
227 set ttimeoutlen=100
228
229This makes typing Esc take effect more quickly. Normally Vim waits a second
230to see if the Esc is the start of an escape sequence. If you have a very slow
231remote connection, increase the number. See 'ttimeout'.
232
233>
234 set display=truncate
235
236Show @@@ in the last line if it is truncated, instead of hiding the whole
Bram Moolenaar68e65602019-05-26 21:33:31 +0200237line. See 'display'.
Bram Moolenaar314dd792019-02-03 15:27:20 +0100238
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239>
240 set incsearch
241
Bram Moolenaar314dd792019-02-03 15:27:20 +0100242Display the match for a search pattern when halfway typing it. See
243'incsearch'.
244
245>
246 set nrformats-=octal
247
248Do not recognize numbers starting with a zero as octal. See 'nrformats'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249
250>
251 map Q gq
252
253This defines a key mapping. More about that in the next section. This
254defines the "Q" command to do formatting with the "gq" operator. This is how
255it worked before Vim 5.0. Otherwise the "Q" command starts Ex mode, but you
256will not need it.
257
258>
Bram Moolenaar314dd792019-02-03 15:27:20 +0100259 inoremap <C-U> <C-G>u<C-U>
Bram Moolenaar26967612019-03-17 17:13:16 +0100260
Bram Moolenaar314dd792019-02-03 15:27:20 +0100261CTRL-U in insert mode deletes all entered text in the current line. Use
262CTRL-G u to first break undo, so that you can undo CTRL-U after inserting a
263line break. Revert with ":iunmap <C-U>".
264
265>
266 if has('mouse')
267 set mouse=a
268 endif
269
270Enable using the mouse if available. See 'mouse'.
271
272>
Bram Moolenaarc51cf032022-02-26 12:25:45 +0000273 vnoremap _g y:exe "grep /" .. escape(@", '\\/') .. "/ *.c *.h"<CR>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274
Bram Moolenaar8fc061c2004-12-29 21:03:02 +0000275This mapping yanks the visually selected text and searches for it in C files.
Bram Moolenaar314dd792019-02-03 15:27:20 +0100276You can see that a mapping can be used to do quite complicated things. Still,
277it is just a sequence of commands that are executed like you typed them.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278
279>
Bram Moolenaar314dd792019-02-03 15:27:20 +0100280 syntax on
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281
Bram Moolenaar314dd792019-02-03 15:27:20 +0100282Enable highlighting files in color. See |syntax|.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283
284 *vimrc-filetype* >
285 filetype plugin indent on
286
287This switches on three very clever mechanisms:
2881. 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
2972. 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
3043. 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 Moolenaar071d4272004-06-13 20:20:40 +0000309
Bram Moolenaar314dd792019-02-03 15:27:20 +0100310 *restore-cursor* *last-position-jump* >
Dragan Simic' via vim_dev81b8bf52023-08-09 17:23:58 +0200311 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
317 \ | execute "normal! g`\""
318 \ | endif
319 augroup END
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320
321Another autocommand. This time it is used after reading any file. The
322complicated stuff after it checks if the '" mark is defined, and jumps to it
Dragan Simic' via vim_dev81b8bf52023-08-09 17:23:58 +0200323if so. It doesn't do that for a commit or rebase message, which are likely
324a different one than last time, and when using xxd(1) to filter and edit
325binary files, which transforms input files back and forth, causing them to
326have dual nature, so to speak. See also |using-xxd|.
327
328The backslash at the start of a line is used to continue the command from the
329previous line. That avoids a line getting very long. See |line-continuation|.
330This only works in a Vim script file, not when typing commands at the
331command line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332
Bram Moolenaar314dd792019-02-03 15:27:20 +0100333>
334 command DiffOrig vert new | set bt=nofile | r ++edit # | 0d_ | diffthis
335 \ | wincmd p | diffthis
336
337This adds the ":DiffOrig" command. Use this in a modified buffer to see the
Bram Moolenaar26967612019-03-17 17:13:16 +0100338differences with the file it was loaded from. See |diff| and |:DiffOrig|.
Bram Moolenaar314dd792019-02-03 15:27:20 +0100339
340>
341 set nolangremap
342
343Prevent that the langmap option applies to characters that result from a
344mapping. If set (default), this may break plugins (but it's backward
345compatible). See 'langremap'.
346
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347==============================================================================
Bram Moolenaar314dd792019-02-03 15:27:20 +0100348*05.4* Simple mappings
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349
350A mapping enables you to bind a set of Vim commands to a single key. Suppose,
351for example, that you need to surround certain words with curly braces. In
352other words, you need to change a word such as "amount" into "{amount}". With
353the :map command, you can tell Vim that the F5 key does this job. The command
354is as follows: >
355
356 :map <F5> i{<Esc>ea}<Esc>
357<
358 Note:
359 When entering this command, you must enter <F5> by typing four
360 characters. Similarly, <Esc> is not entered by pressing the <Esc>
361 key, but by typing five characters. Watch out for this difference
362 when reading the manual!
363
364Let's break this down:
365 <F5> The F5 function key. This is the trigger key that causes the
366 command to be executed as the key is pressed.
367
368 i{<Esc> Insert the { character. The <Esc> key ends Insert mode.
369
370 e Move to the end of the word.
371
372 a}<Esc> Append the } to the word.
373
374After you execute the ":map" command, all you have to do to put {} around a
375word is to put the cursor on the first character and press F5.
376
377In this example, the trigger is a single key; it can be any string. But when
378you use an existing Vim command, that command will no longer be available.
379You better avoid that.
380 One key that can be used with mappings is the backslash. Since you
381probably want to define more than one mapping, add another character. You
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000382could map "\p" to add parentheses around a word, and "\c" to add curly braces,
383for example: >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384
385 :map \p i(<Esc>ea)<Esc>
386 :map \c i{<Esc>ea}<Esc>
387
388You need to type the \ and the p quickly after another, so that Vim knows they
389belong together.
390
391The ":map" command (with no arguments) lists your current mappings. At
392least the ones for Normal mode. More about mappings in section |40.1|.
393
394==============================================================================
Bram Moolenaar314dd792019-02-03 15:27:20 +0100395*05.5* Adding a package *add-package* *matchit-install*
Bram Moolenaaraedfcbe2016-03-25 17:02:51 +0100396
397A package is a set of files that you can add to Vim. There are two kinds of
398packages: optional and automatically loaded on startup.
399
400The Vim distribution comes with a few packages that you can optionally use.
401For example, the matchit plugin. This plugin makes the "%" command jump to
402matching HTML tags, if/else/endif in Vim scripts, etc. Very useful, although
403it's not backwards compatible (that's why it is not enabled by default).
404
405To start using the matchit plugin, add one line to your vimrc file: >
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100406 packadd! matchit
Bram Moolenaaraedfcbe2016-03-25 17:02:51 +0100407
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100408That's all! After restarting Vim you can find help about this plugin: >
Bram Moolenaaraedfcbe2016-03-25 17:02:51 +0100409 :help matchit
410
411This works, because when `:packadd` loaded the plugin it also added the
412package directory in 'runtimepath', so that the help file can be found.
413
414You can find packages on the Internet in various places. It usually comes as
415an archive or as a repository. For an archive you can follow these steps:
416 1. create the package directory: >
417 mkdir -p ~/.vim/pack/fancy
418< "fancy" can be any name of your liking. Use one that describes the
419 package.
420 2. unpack the archive in that directory. This assumes the top
421 directory in the archive is "start": >
Bram Moolenaar938ae282023-02-20 20:44:55 +0000422 cd ~/.vim/pack/fancy
Bram Moolenaaraedfcbe2016-03-25 17:02:51 +0100423 unzip /tmp/fancy.zip
424< If the archive layout is different make sure that you end up with a
425 path like this:
426 ~/.vim/pack/fancy/start/fancytext/plugin/fancy.vim ~
427 Here "fancytext" is the name of the package, it can be anything
428 else.
429
430More information about packages can be found here: |packages|.
431
432==============================================================================
Bram Moolenaar314dd792019-02-03 15:27:20 +0100433*05.6* Adding a plugin *add-plugin* *plugin*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434
435Vim's functionality can be extended by adding plugins. A plugin is nothing
436more than a Vim script file that is loaded automatically when Vim starts. You
437can add a plugin very easily by dropping it in your plugin directory.
438{not available when Vim was compiled without the |+eval| feature}
439
440There are two types of plugins:
441
442 global plugin: Used for all kinds of files
443 filetype plugin: Only used for a specific type of file
444
445The global plugins will be discussed first, then the filetype ones
446|add-filetype-plugin|.
447
448
449GLOBAL PLUGINS *standard-plugin*
450
451When you start Vim, it will automatically load a number of global plugins.
452You don't have to do anything for this. They add functionality that most
453people will want to use, but which was implemented as a Vim script instead of
454being compiled into Vim. You can find them listed in the help index
455|standard-plugin-list|. Also see |load-plugins|.
456
457 *add-global-plugin*
458You can add a global plugin to add functionality that will always be present
459when you use Vim. There are only two steps for adding a global plugin:
4601. Get a copy of the plugin.
4612. Drop it in the right directory.
462
463
464GETTING A GLOBAL PLUGIN
465
466Where can you find plugins?
Bram Moolenaara9604e62018-07-21 05:56:22 +0200467- Some are always loaded, you can see them in the directory $VIMRUNTIME/plugin.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468- Some come with Vim. You can find them in the directory $VIMRUNTIME/macros
Bram Moolenaara9604e62018-07-21 05:56:22 +0200469 and its sub-directories and under $VIM/vimfiles/pack/dist/opt/.
Bram Moolenaar76b92b22006-03-24 22:46:53 +0000470- Download from the net. There is a large collection on http://www.vim.org.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471- They are sometimes posted in a Vim |maillist|.
472- You could write one yourself, see |write-plugin|.
473
Bram Moolenaar76b92b22006-03-24 22:46:53 +0000474Some plugins come as a vimball archive, see |vimball|.
475Some plugins can be updated automatically, see |getscript|.
476
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477
478USING A GLOBAL PLUGIN
479
480First read the text in the plugin itself to check for any special conditions.
481Then copy the file to your plugin directory:
482
483 system plugin directory ~
484 Unix ~/.vim/plugin/
Bram Moolenaar6f345a12019-12-17 21:27:18 +0100485 PC $HOME/vimfiles/plugin or $VIM/vimfiles/plugin
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 Amiga s:vimfiles/plugin
487 Macintosh $VIM:vimfiles:plugin
488 Mac OS X ~/.vim/plugin/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489
490Example for Unix (assuming you didn't have a plugin directory yet): >
491
492 mkdir ~/.vim
493 mkdir ~/.vim/plugin
Bram Moolenaar7db8f6f2016-03-29 23:12:46 +0200494 cp /tmp/yourplugin.vim ~/.vim/plugin
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495
Bram Moolenaar7db8f6f2016-03-29 23:12:46 +0200496That's all! Now you can use the commands defined in this plugin.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000498Instead of putting plugins directly into the plugin/ directory, you may
499better organize them by putting them into subdirectories under plugin/.
500As an example, consider using "~/.vim/plugin/perl/*.vim" for all your Perl
501plugins.
Bram Moolenaar07d4d732005-10-03 22:04:08 +0000502
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503
504FILETYPE PLUGINS *add-filetype-plugin* *ftplugins*
505
506The Vim distribution comes with a set of plugins for different filetypes that
507you can start using with this command: >
508
509 :filetype plugin on
510
511That's all! See |vimrc-filetype|.
512
513If you are missing a plugin for a filetype you are using, or you found a
514better one, you can add it. There are two steps for adding a filetype plugin:
5151. Get a copy of the plugin.
5162. Drop it in the right directory.
517
518
519GETTING A FILETYPE PLUGIN
520
521You can find them in the same places as the global plugins. Watch out if the
522type of file is mentioned, then you know if the plugin is a global or a
523filetype one. The scripts in $VIMRUNTIME/macros are global ones, the filetype
524plugins are in $VIMRUNTIME/ftplugin.
525
526
527USING A FILETYPE PLUGIN *ftplugin-name*
528
529You can add a filetype plugin by dropping it in the right directory. The
530name of this directory is in the same directory mentioned above for global
531plugins, but the last part is "ftplugin". Suppose you have found a plugin for
532the "stuff" filetype, and you are on Unix. Then you can move this file to the
533ftplugin directory: >
534
535 mv thefile ~/.vim/ftplugin/stuff.vim
536
537If that file already exists you already have a plugin for "stuff". You might
538want to check if the existing plugin doesn't conflict with the one you are
539adding. If it's OK, you can give the new one another name: >
540
541 mv thefile ~/.vim/ftplugin/stuff_too.vim
542
543The underscore is used to separate the name of the filetype from the rest,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000544which can be anything. If you use "otherstuff.vim" it wouldn't work, it would
545be loaded for the "otherstuff" filetype.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546
Bram Moolenaar5666fcd2019-12-26 14:35:26 +0100547On MS-DOS like filesystems you cannot use long filenames. You would run into
548trouble if you add a second plugin and the filetype has more than six
549characters. You can use an extra directory to get around this: >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550
551 mkdir $VIM/vimfiles/ftplugin/fortran
552 copy thefile $VIM/vimfiles/ftplugin/fortran/too.vim
553
554The generic names for the filetype plugins are: >
555
556 ftplugin/<filetype>.vim
557 ftplugin/<filetype>_<name>.vim
558 ftplugin/<filetype>/<name>.vim
559
560Here "<name>" can be any name that you prefer.
561Examples for the "stuff" filetype on Unix: >
562
563 ~/.vim/ftplugin/stuff.vim
564 ~/.vim/ftplugin/stuff_def.vim
565 ~/.vim/ftplugin/stuff/header.vim
566
567The <filetype> part is the name of the filetype the plugin is to be used for.
568Only files of this filetype will use the settings from the plugin. The <name>
569part of the plugin file doesn't matter, you can use it to have several plugins
570for the same filetype. Note that it must end in ".vim".
571
572
573Further reading:
574|filetype-plugins| Documentation for the filetype plugins and information
575 about how to avoid that mappings cause problems.
576|load-plugins| When the global plugins are loaded during startup.
577|ftplugin-overrule| Overruling the settings from a global plugin.
578|write-plugin| How to write a plugin script.
579|plugin-details| For more information about using plugins or when your
580 plugin doesn't work.
581|new-filetype| How to detect a new file type.
582
583==============================================================================
Bram Moolenaar314dd792019-02-03 15:27:20 +0100584*05.7* Adding a help file *add-local-help*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585
586If you are lucky, the plugin you installed also comes with a help file. We
587will explain how to install the help file, so that you can easily find help
588for your new plugin.
Bram Moolenaaraedfcbe2016-03-25 17:02:51 +0100589 Let us use the "doit.vim" plugin as an example. This plugin comes with
590documentation: "doit.txt". Let's first copy the plugin to the right
591directory. This time we will do it from inside Vim. (You may skip some of
592the "mkdir" commands if you already have the directory.) >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593
594 :!mkdir ~/.vim
595 :!mkdir ~/.vim/plugin
Bram Moolenaaraedfcbe2016-03-25 17:02:51 +0100596 :!cp /tmp/doit.vim ~/.vim/plugin
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597
Bram Moolenaar5666fcd2019-12-26 14:35:26 +0100598The "cp" command is for Unix, on MS-Windows you can use "copy".
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000599
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600Now create a "doc" directory in one of the directories in 'runtimepath'. >
601
602 :!mkdir ~/.vim/doc
603
604Copy the help file to the "doc" directory. >
605
Bram Moolenaaraedfcbe2016-03-25 17:02:51 +0100606 :!cp /tmp/doit.txt ~/.vim/doc
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607
608Now comes the trick, which allows you to jump to the subjects in the new help
609file: Generate the local tags file with the |:helptags| command. >
610
611 :helptags ~/.vim/doc
612
613Now you can use the >
614
Bram Moolenaaraedfcbe2016-03-25 17:02:51 +0100615 :help doit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616
Bram Moolenaaraedfcbe2016-03-25 17:02:51 +0100617command to find help for "doit" in the help file you just added. You can see
618an entry for the local help file when you do: >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619
620 :help local-additions
621
622The title lines from the local help files are automagically added to this
623section. There you can see which local help files have been added and jump to
624them through the tag.
625
626For writing a local help file, see |write-local-help|.
627
628==============================================================================
Bram Moolenaar314dd792019-02-03 15:27:20 +0100629*05.8* The option window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630
631If you are looking for an option that does what you want, you can search in
632the help files here: |options|. Another way is by using this command: >
633
634 :options
635
636This opens a new window, with a list of options with a one-line explanation.
637The options are grouped by subject. Move the cursor to a subject and press
638<Enter> to jump there. Press <Enter> again to jump back. Or use CTRL-O.
639
640You can change the value of an option. For example, move to the "displaying
641text" subject. Then move the cursor down to this line:
642
643 set wrap nowrap ~
644
645When you hit <Enter>, the line will change to:
646
647 set nowrap wrap ~
648
649The option has now been switched off.
650
651Just above this line is a short description of the 'wrap' option. Move the
652cursor one line up to place it in this line. Now hit <Enter> and you jump to
653the full help on the 'wrap' option.
654
655For options that take a number or string argument you can edit the value.
656Then press <Enter> to apply the new value. For example, move the cursor a few
657lines up to this line:
658
659 set so=0 ~
660
661Position the cursor on the zero with "$". Change it into a five with "r5".
662Then press <Enter> to apply the new value. When you now move the cursor
663around you will notice that the text starts scrolling before you reach the
664border. This is what the 'scrolloff' option does, it specifies an offset
665from the window border where scrolling starts.
666
667==============================================================================
Bram Moolenaar314dd792019-02-03 15:27:20 +0100668*05.9* Often used options
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669
670There are an awful lot of options. Most of them you will hardly ever use.
671Some of the more useful ones will be mentioned here. Don't forget you can
672find more help on these options with the ":help" command, with single quotes
673before and after the option name. For example: >
674
675 :help 'wrap'
676
677In case you have messed up an option value, you can set it back to the
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000678default by putting an ampersand (&) after the option name. Example: >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679
680 :set iskeyword&
681
682
683NOT WRAPPING LINES
684
685Vim normally wraps long lines, so that you can see all of the text. Sometimes
686it's better to let the text continue right of the window. Then you need to
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000687scroll the text left-right to see all of a long line. Switch wrapping off
688with this command: >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689
690 :set nowrap
691
692Vim will automatically scroll the text when you move to text that is not
693displayed. To see a context of ten characters, do this: >
694
695 :set sidescroll=10
696
697This doesn't change the text in the file, only the way it is displayed.
698
699
700WRAPPING MOVEMENT COMMANDS
701
702Most commands for moving around will stop moving at the start and end of a
703line. You can change that with the 'whichwrap' option. This sets it to the
704default value: >
705
706 :set whichwrap=b,s
707
708This allows the <BS> key, when used in the first position of a line, to move
709the cursor to the end of the previous line. And the <Space> key moves from
710the end of a line to the start of the next one.
711
712To allow the cursor keys <Left> and <Right> to also wrap, use this command: >
713
714 :set whichwrap=b,s,<,>
715
716This is still only for Normal mode. To let <Left> and <Right> do this in
717Insert mode as well: >
718
719 :set whichwrap=b,s,<,>,[,]
720
721There are a few other flags that can be added, see 'whichwrap'.
722
723
724VIEWING TABS
725
726When there are tabs in a file, you cannot see where they are. To make them
727visible: >
728
729 :set list
730
Bram Moolenaar1b826e52007-05-12 15:14:36 +0000731Now every tab is displayed as ^I. And a $ is displayed at the end of each
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732line, so that you can spot trailing spaces that would otherwise go unnoticed.
733 A disadvantage is that this looks ugly when there are many Tabs in a file.
734If you have a color terminal, or are using the GUI, Vim can show the spaces
735and tabs as highlighted characters. Use the 'listchars' option: >
736
737 :set listchars=tab:>-,trail:-
738
739Now every tab will be displayed as ">---" (with more or less "-") and trailing
740white space as "-". Looks a lot better, doesn't it?
741
742
743KEYWORDS
744
745The 'iskeyword' option specifies which characters can appear in a word: >
746
747 :set iskeyword
748< iskeyword=@,48-57,_,192-255 ~
749
750The "@" stands for all alphabetic letters. "48-57" stands for ASCII
751characters 48 to 57, which are the numbers 0 to 9. "192-255" are the
752printable latin characters.
753 Sometimes you will want to include a dash in keywords, so that commands
754like "w" consider "upper-case" to be one word. You can do it like this: >
755
756 :set iskeyword+=-
757 :set iskeyword
758< iskeyword=@,48-57,_,192-255,- ~
759
760If you look at the new value, you will see that Vim has added a comma for you.
761 To remove a character use "-=". For example, to remove the underscore: >
762
763 :set iskeyword-=_
764 :set iskeyword
765< iskeyword=@,48-57,192-255,- ~
766
767This time a comma is automatically deleted.
768
769
770ROOM FOR MESSAGES
771
772When Vim starts there is one line at the bottom that is used for messages.
773When a message is long, it is either truncated, thus you can only see part of
774it, or the text scrolls and you have to press <Enter> to continue.
775 You can set the 'cmdheight' option to the number of lines used for
776messages. Example: >
777
778 :set cmdheight=3
779
780This does mean there is less room to edit text, thus it's a compromise.
781
782==============================================================================
783
784Next chapter: |usr_06.txt| Using syntax highlighting
785
Bram Moolenaard473c8c2018-08-11 18:00:22 +0200786Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: