robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 9a29622b3b00ddb601

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