usr_05.txt 22 KB

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