robin/laufey

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

Eisuke Kawashimadocs: adjust modelines for HTML rendering #396497e778205ab

master
17.6 KiB483 linesraw
1*usr_21.txt*	Nvim
2
3
4		     VIM USER MANUAL	by Bram Moolenaar
5
6
7			   Go away and come back
8
9
10This chapter goes into mixing the use of other programs with Vim.  Either by
11executing program from inside Vim or by leaving Vim and coming back later.
12Furthermore, this is about the ways to remember the state of Vim and restore
13it later.
14
15|21.1|	Suspend and resume
16|21.2|	Executing shell commands
17|21.3|	Remembering information; ShaDa
18|21.4|	Sessions
19|21.5|	Views
20|21.6|	Modelines
21
22     Next chapter: |usr_22.txt|  Finding the file to edit
23 Previous chapter: |usr_20.txt|  Typing command-line commands quickly
24Table of contents: |usr_toc.txt|
25
26==============================================================================
27*21.1*	Suspend and resume
28
29Like most Unix programs Vim can be suspended by pressing CTRL-Z.  This stops
30Vim and takes you back to the shell it was started in.  You can then do any
31other commands until you are bored with them.  Then bring back Vim with the
32"fg" command. >
33
34	CTRL-Z
35	{any sequence of shell commands}
36	fg
37
38You are right back where you left Vim, nothing has changed.
39   In case pressing CTRL-Z doesn't work, you can also use ":suspend".
40Don't forget to bring Vim back to the foreground, you would lose any changes
41that you made!
42
43Only Unix has support for this.  On other systems Vim will start a shell for
44you.  This also has the functionality of being able to execute shell commands.
45But it's a new shell, not the one that you started Vim from.
46   When you are running the GUI you can't go back to the shell where Vim was
47started.  CTRL-Z will minimize the Vim window instead.
48
49==============================================================================
50*21.2*	Executing shell commands
51
52To execute a single shell command from Vim use ":!{command}".  For example, to
53see a directory listing: >
54
55	:!ls
56	:!dir
57
58The first one is for Unix, the second one for MS-Windows.
59   Vim will execute the program.  When it ends you will get a prompt to hit
60<Enter>.  This allows you to have a look at the output from the command before
61returning to the text you were editing.
62   The "!" is also used in other places where a program is run.  Let's take
63a look at an overview:
64
65	:!{program}		execute {program}
66	:r !{program}		execute {program} and read its output
67	:w !{program}		execute {program} and send text to its input
68	:[range]!{program}	filter text through {program}
69
70Notice that the presence of a range before "!{program}" makes a big
71difference.  Without it executes the program normally, with the range a number
72of text lines is filtered through the program.
73
74Executing a whole row of programs this way is possible.  But a shell is much
75better at it.  You can start a new shell with |:terminal|.
76
77This is similar to using CTRL-Z to suspend Vim.  The difference is that a new
78shell is started.
79
80==============================================================================
81*21.3*	Remembering information; ShaDa
82
83After editing for a while you will have text in registers, marks in various
84files, a command line history filled with carefully crafted commands.  When
85you exit Vim all of this is lost.  But you can get it back!
86
87The ShaDa (abbreviation of SHAred DAta) file is designed to store status
88information:
89
90	Command-line and Search pattern history
91	Text in registers
92	Marks for various files
93	The buffer list
94	Global variables
95
96Each time you exit Vim it will store this information in a file, the ShaDa
97file.  When Vim starts again, the ShaDa file is read and the information
98restored.
99
100The 'shada' option is set by default to restore a limited number of items.
101You might want to set it to remember more information.  This is done through
102the following command: >
103
104	:set shada=string
105
106The string specifies what to save.  The syntax of this string is an option
107character followed by an argument.  The option/argument pairs are separated by
108commas.
109   Take a look at how you can build up your own shada string.  First, the '
110option is used to specify how many files for which you save marks (a-z).  Pick
111a nice even number for this option (1000, for instance).  Your command now
112looks like this: >
113
114	:set shada='1000
115
116The f option controls whether global marks (A-Z and 0-9) are stored.  If this
117option is 0, none are stored.  If it is 1 or you do not specify an f option,
118the marks are stored.  You want this feature, so now you have this: >
119
120	:set shada='1000,f1
121
122The < option controls how many lines are saved for each of the registers.  By
123default, all the lines are saved.  If 0, nothing is saved.  To avoid adding
124thousands of lines to your ShaDa file (which might never get used and makes
125starting Vim slower) you use a maximum of 500 lines: >
126
127	:set shada='1000,f1,<500
128<
129Other options you might want to use:
130	:	number of lines to save from the command line history
131	@	number of lines to save from the input line history
132	/	number of lines to save from the search history
133	r	removable media, for which no marks will be stored (can be
134		used several times)
135	!	global variables that start with an uppercase letter and
136		don't contain lowercase letters
137	h	disable 'hlsearch' highlighting when starting
138	%	the buffer list (only restored when starting Vim without file
139		arguments)
140	c	convert the text using 'encoding'
141	n	name used for the ShaDa file (must be the last option)
142
143See the 'shada' option and |shada-file| for more information.
144
145When you run Vim multiple times, the last one exiting will store its
146information.  This may cause information that previously exiting Vims stored
147to be lost.  Each item can be remembered only once.
148
149
150GETTING BACK TO WHERE YOU STOPPED VIM
151
152You are halfway through editing a file and it's time to leave for holidays.
153You exit Vim and go enjoy yourselves, forgetting all about your work.  After a
154couple of weeks you start Vim, and type:
155>
156	'0
157
158And you are right back where you left Vim.  So you can get on with your work.
159   Vim creates a mark each time you exit Vim.  The last one is '0.  The
160position that '0 pointed to is made '1.  And '1 is made to '2, and so forth.
161Mark '9 is lost.
162   The |:marks| command is useful to find out where '0 to '9 will take you.
163
164
165GETTING BACK TO SOME FILE
166
167If you want to go back to a file that you edited recently, but not when
168exiting Vim, there is a slightly more complicated way.  You can see a list of
169files by typing the command: >
170
171	:oldfiles
172<	1: ~/.config/nvim/init.vim ~
173	2: ~/text/resume.txt ~
174	3: /tmp/draft ~
175
176Now you would like to edit the second file, which is in the list preceded by
177"2:".  You type: >
178
179	:e #<2
180
181Instead of ":e" you can use any command that has a file name argument, the
182"#<2" item works in the same place as "%" (current file name) and "#"
183(alternate file name).  So you can also split the window to edit the third
184file: >
185
186	:split #<3
187
188That #<123 thing is a bit complicated when you just want to edit a file.
189Fortunately there is a simpler way: >
190
191	:browse oldfiles
192<	1: ~/.config/nvim/init.vim ~
193	2: ~/text/resume.txt ~
194	3: /tmp/draft ~
195	-- More --
196
197You get the same list of files as with |:oldfiles|.  If you want to edit
198"resume.txt" first press "q" to stop the listing.  You will get a prompt:
199
200	Type number and <Enter> (empty cancels): ~
201
202Type "2" and press <Enter> to edit the second file.
203
204More info at |:oldfiles|, |v:oldfiles| and |c_#<|.
205
206
207MOVE INFO FROM ONE VIM TO ANOTHER
208
209You can use the ":wshada" and ":rshada" commands to save and restore the
210information while still running Vim.  This is useful for exchanging register
211contents between two instances of Vim, for example.  In the first Vim do: >
212
213	:wshada! ~/tmp/shada
214
215And in the second Vim do: >
216
217	:rshada! ~/tmp/shada
218
219Obviously, the "w" stands for "write" and the "r" for "read".
220   The ! character is used by ":wshada" to forcefully overwrite an existing
221file.  When it is omitted, and the file exists, the information is merged into
222the file.
223   The ! character used for ":rshada" means that all the information in ShaDa
224file has priority over existing information, this may overwrite it.  Without
225the ! only information that wasn't set is used.
226   These commands can also be used to store info and use it again later.  You
227could make a directory full of ShaDa files, each containing info for a
228different purpose.
229
230==============================================================================
231*21.4*	Sessions
232
233Suppose you are editing along, and it is the end of the day.  You want to quit
234work and pick up where you left off the next day.  You can do this by saving
235your editing session and restoring it the next day.
236   A Vim session contains all the information about what you are editing.
237This includes things such as the file list, window layout, global variables,
238options and other information.  (Exactly what is remembered is controlled by
239the 'sessionoptions' option, described below.)
240   The following command creates a session file: >
241
242	:mksession vimbook.vim
243
244Later if you want to restore this session, you can use this command: >
245
246	:source vimbook.vim
247
248If you want to start Vim and restore a specific session, you can use the
249following command: >
250
251	vim -S vimbook.vim
252
253This tells Vim to read a specific file on startup.  The 'S' stands for
254session (actually, you can source any Vim script with -S, thus it might as
255well stand for "source").
256
257The windows that were open are restored, with the same position and size as
258before.  Mappings and option values are like before.
259   What exactly is restored depends on the 'sessionoptions' option.  The
260default value is:
261"blank,buffers,curdir,folds,help,options,tabpages,winsize,terminal".
262
263	blank		keep empty windows
264	buffers		all buffers, not only the ones in a window
265	curdir		the current directory
266	folds		folds, also manually created ones
267	help		the help window
268	options		all options and mappings
269	tabpages	all tab pages
270	winsize		window sizes
271	terminal	include terminal windows
272
273Change this to your liking.  To also restore the size of the Vim window, for
274example, use: >
275
276	:set sessionoptions+=resize
277
278
279SESSION HERE, SESSION THERE
280
281The obvious way to use sessions is when working on different projects.
282Suppose you store your session files in the directory "~/.config/nvim".  You
283are currently working on the "secret" project and have to switch to the
284"boring" project: >
285
286	:wall
287	:mksession! ~/.config/nvim/secret.vim
288	:source ~/.config/nvim/boring.vim
289
290This first uses ":wall" to write all modified files.  Then the current session
291is saved, using ":mksession!".  This overwrites the previous session.  The
292next time you load the secret session you can continue where you were at this
293point.  And finally you load the new "boring" session.
294
295If you open help windows, split and close various windows, and generally mess
296up the window layout, you can go back to the last saved session: >
297
298	:source ~/.config/nvim/boring.vim
299
300Thus you have complete control over whether you want to continue next time
301where you are now, by saving the current setup in a session, or keep the
302session file as a starting point.
303   Another way of using sessions is to create a window layout that you like to
304use, and save this in a session.  Then you can go back to this layout whenever
305you want.
306   For example, this is a nice layout to use:
307>
308        +----------------------------------------+
309        |                  VIM - main help file  |
310        |                                        |
311        |Move around:  Use the cursor keys, or "h|
312        |help.txt================================|
313        |explorer   |                            |
314        |dir        |~                           |
315        |dir        |~                           |
316        |file       |~                           |
317        |file       |~                           |
318        |file       |~                           |
319        |file       |~                           |
320        |~/=========|[No File]===================|
321        |                                        |
322        +----------------------------------------+
323<
324This has a help window at the top, so that you can read this text.  The narrow
325vertical window on the left contains a file explorer.  This is a Vim plugin
326that lists the contents of a directory.  You can select files to edit there.
327More about this in the next chapter.
328   Create this from a just started Vim with: >
329
330	:help
331	CTRL-W w
332	:vertical split ~/
333
334You can resize the windows a bit to your liking.  Then save the session with:
335>
336	:mksession ~/.config/nvim/mine.vim
337
338Now you can start Vim with this layout: >
339
340	vim -S ~/.config/nvim/mine.vim
341
342Hint: To open a file you see listed in the explorer window in the empty
343window, move the cursor to the filename and press "O".  Double clicking with
344the mouse will also do this.
345
346
347SESSIONS AND SHADA
348
349Sessions store many things, but not the position of marks, contents of
350registers and the command line history.  You need to use the shada feature
351for these things.
352   In most situations you will want to use sessions separately from shada.
353This can be used to switch to another session, but keep the command line
354history.  And yank text into registers in one session, and paste it back in
355another session.
356   You might prefer to keep the info with the session.  You will have to do
357this yourself then.  Example: >
358
359	:mksession! ~/.config/nvim/secret.vim
360	:wshada! ~/.local/state/nvim/shada/secret.shada
361
362And to restore this again: >
363
364	:source ~/.config/nvim/secret.vim
365	:rshada! ~/.local/state/nvim/shada/secret.shada
366
367==============================================================================
368*21.5*	Views
369
370A session stores the looks of the whole of Vim.  When you want to store the
371properties for one window only, use a view.
372   The use of a view is for when you want to edit a file in a specific way.
373For example, you have line numbers enabled with the 'number' option and
374defined a few folds.  Just like with sessions, you can remember this view on
375the file and restore it later.  Actually, when you store a session, it stores
376the view of each window.
377   There are two basic ways to use views.  The first is to let Vim pick a name
378for the view file.  You can restore the view when you later edit the same
379file.  To store the view for the current window: >
380
381	:mkview
382
383Vim will decide where to store the view.  When you later edit the same file
384you get the view back with this command: >
385
386	:loadview
387
388That's easy, isn't it?
389   Now you want to view the file without the 'number' option on, or with all
390folds open, you can set the options to make the window look that way.  Then
391store this view with: >
392
393	:mkview 1
394
395Obviously, you can get this back with: >
396
397	:loadview 1
398
399Now you can switch between the two views on the file by using ":loadview" with
400and without the "1" argument.
401   You can store up to ten views for the same file this way, one unnumbered
402and nine numbered 1 to 9.
403
404
405A VIEW WITH A NAME
406
407The second basic way to use views is by storing the view in a file with a name
408you choose.  This view can be loaded while editing another file.  Vim will
409then switch to editing the file specified in the view.  Thus you can use this
410to quickly switch to editing another file, with all its options set as you
411saved them.
412   For example, to save the view of the current file: >
413
414	:mkview ~/.config/nvim/main.vim
415
416You can restore it with: >
417
418	:source ~/.config/nvim/main.vim
419
420==============================================================================
421*21.6*	Modelines
422
423When editing a specific file, you might set options specifically for that
424file.  Typing these commands each time is boring.  Using a session or view for
425editing a file doesn't work when sharing the file between several people.
426   The solution for this situation is adding a modeline to the file.  This is
427a line of text that tells Vim the values of options, to be used in this file
428only.
429   A typical example is a C program where you make indents by a multiple of 4
430spaces.  This requires setting the 'shiftwidth' option to 4.  This modeline
431will do that: >
432
433	/* vim:set shiftwidth=4: */ ~
434
435Put this line as one of the first or last five lines in the file.  When
436editing the file, you will notice that 'shiftwidth' will have been set to
437four.  When editing another file, it's set back to the default value of eight.
438   For some files the modeline fits well in the header, thus it can be put at
439the top of the file.  For text files and other files where the modeline gets
440in the way of the normal contents, put it at the end of the file.
441
442The 'modelines' option specifies how many lines at the start and end of the
443file are inspected for containing a modeline.  To inspect ten lines: >
444
445	:set modelines=10
446
447The 'modeline' option can be used to switch this off.  Do this when you are
448working as root on Unix or Administrator on MS-Windows, or when you don't
449trust the files you are editing: >
450
451	:set nomodeline
452
453Use this format for the modeline: >
454
455	any-text vim:set {option}={value} ... : any-text
456
457The "any-text" indicates that you can put any text before and after the part
458that Vim will use.  This allows making it look like a comment, like what was
459done above with "/*" and "*/".
460   The " vim:" part is what makes Vim recognize this line.  There must be
461white space before "vim", or "vim" must be at the start of the line.  Thus
462using something like "gvim:" will not work.
463   The part between the colons is a ":set" command.  It works the same way as
464typing the ":set" command, except that you need to insert a backslash before a
465colon (otherwise it would be seen as the end of the modeline).
466
467Another example: >
468
469	// vim:set textwidth=72 dir=c\:\tmp:  use c:\tmp here
470
471There is an extra backslash before the first colon, so that it's included in
472the ":set" command.  The text after the second colon is ignored, thus a remark
473can be placed there.
474
475For more details see |modeline|.
476
477==============================================================================
478
479Next chapter: |usr_22.txt|  Finding the file to edit
480
481Copyright: see |manual-copyright|
482
483vim:tw=78:ts=8:noet:ft=help:norl: