robin/laufey

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

Eisuke Kawashimadocs: adjust modelines for HTML rendering #396497e778205ab

master
23.1 KiB692 linesraw
1*usr_02.txt*	Nvim
2
3
4		     VIM USER MANUAL	by Bram Moolenaar
5
6
7			    The first steps in Vim
8
9
10This chapter provides just enough information to edit a file with Vim.  Not
11well or fast, but you can edit.  Take some time to practice with these
12commands, they form the base for what follows.
13
14|02.1|	Running Vim for the First Time
15|02.2|	Inserting text
16|02.3|	Moving around
17|02.4|	Deleting characters
18|02.5|	Undo and Redo
19|02.6|	Other editing commands
20|02.7|	Getting out
21|02.8|	Finding help
22
23     Next chapter: |usr_03.txt|  Moving around
24 Previous chapter: |usr_01.txt|  About the manuals
25Table of contents: |usr_toc.txt|
26
27==============================================================================
28*02.1*	Running Vim for the First Time
29
30To start Nvim, enter this command: >
31
32	nvim file.txt
33
34On Unix you can type this at any command prompt.  If you are running Microsoft
35Windows, open a Command Prompt and enter the command.  In either case, Vim
36starts editing a file called file.txt.  Because this is a new file, you get a
37blank window.  This is what your screen will look like:
38>
39	+---------------------------------------+
40	|#					|
41	|~					|
42	|~					|
43	|~					|
44	|~					|
45	|"file.txt" [New]			|
46	+---------------------------------------+
47		('#' is the cursor position.)
48<
49The tilde (~) lines indicate lines not in the file.  In other words, when Vim
50runs out of file to display, it displays tilde lines.  At the bottom of the
51screen, a message line indicates the file is named file.txt and shows that you
52are creating a new file.  The message information is temporary and other
53information overwrites it.
54
55==============================================================================
56*02.2*	Inserting text
57
58The Vim editor is a modal editor.  That means that the editor behaves
59differently, depending on which mode you are in.  The two basic modes are
60called Normal mode and Insert mode.  In Normal mode the characters you type
61are commands.  In Insert mode the characters are inserted as text.
62   Since you have just started Vim it will be in Normal mode.  To start Insert
63mode you type the "i" command (i for Insert).  Then you can enter
64the text.  It will be inserted into the file.  Do not worry if you make
65mistakes; you can correct them later.  To enter the following programmer's
66limerick, this is what you type: >
67
68	iA very intelligent turtle
69	Found programming Unix a hurdle
70
71After typing "turtle" you press the <Enter> key to start a new line.  Finally
72you press the <Esc> key to stop Insert mode and go back to Normal mode.  You
73now have two lines of text in your Vim window:
74>
75	+---------------------------------------+
76	|A very intelligent turtle		|
77	|Found programming Unix a hurdle	|
78	|~					|
79	|~					|
80	|					|
81	+---------------------------------------+
82<
83
84WHAT IS THE MODE?
85
86To be able to see what mode you are in, type this command: >
87
88	:set showmode
89
90You will notice that when typing the colon Vim moves the cursor to the last
91line of the window.  That's where you type colon commands (commands that start
92with a colon).  Finish this command by pressing the <Enter> key (all commands
93that start with a colon are finished this way).
94   Now, if you type the "i" command Vim will display --INSERT-- at the bottom
95of the window.  This indicates you are in Insert mode.
96>
97	+---------------------------------------+
98	|A very intelligent turtle		|
99	|Found programming Unix a hurdle	|
100	|~					|
101	|~					|
102	|-- INSERT --				|
103	+---------------------------------------+
104<
105If you press <Esc> to go back to Normal mode the last line will be made blank.
106
107
108GETTING OUT OF TROUBLE
109
110One of the problems for Vim novices is mode confusion, which is caused by
111forgetting which mode you are in or by accidentally typing a command that
112switches modes.  To get back to Normal mode, no matter what mode you are in,
113press the <Esc> key.  Sometimes you have to press it twice.  If Vim beeps back
114at you, you already are in Normal mode.
115
116==============================================================================
117*02.3*	Moving around
118
119After you return to Normal mode, you can move around by using these keys:
120
121	h   left						*hjkl*
122	j   down
123	k   up
124	l   right
125
126At first, it may appear that these commands were chosen at random.  After all,
127who ever heard of using l for right?  But actually, there is a very good
128reason for these choices: Moving the cursor is the most common thing you do in
129an editor, and these keys are on the home row of your right hand.  In other
130words, these commands are placed where you can type them the fastest
131(especially when you type with ten fingers).
132
133	Note:
134	You can also move the cursor by using the arrow keys.  If you do,
135	however, you greatly slow down your editing because to press the arrow
136	keys, you must move your hand from the text keys to the arrow keys.
137	Considering that you might be doing it hundreds of times an hour, this
138	can take a significant amount of time.
139	   Also, there are keyboards which do not have arrow keys, or which
140	locate them in unusual places; therefore, knowing the use of the hjkl
141	keys helps in those situations.
142
143One way to remember these commands is that h is on the left, l is on the
144right and j points down.  In a picture: >
145
146		       k
147		   h     l
148		     j
149
150The best way to learn these commands is by using them.  Use the "i" command to
151insert some more lines of text.  Then use the hjkl keys to move around and
152insert a word somewhere.  Don't forget to press <Esc> to go back to Normal
153mode.  |:Tutor| is also a nice way to learn by doing.
154
155For Japanese users, Hiroshi Iwatani suggested using this:
156
157			Komsomolsk
158			    ^
159			    |
160	   Huan Ho	<--- --->  Los Angeles
161	(Yellow river)	    |
162			    v
163			  Java (the island, not the programming language)
164
165==============================================================================
166*02.4*	Deleting characters
167
168To delete a character, move the cursor over it and type "x".  (This is a
169throwback to the old days of the typewriter, when you deleted things by typing
170xxxx over them.)  Move the cursor to the beginning of the first line, for
171example, and type xxxxxxx (seven x's) to delete "A very ".  The result should
172look like this:
173>
174	+---------------------------------------+
175	|intelligent turtle			|
176	|Found programming Unix a hurdle	|
177	|~					|
178	|~					|
179	|					|
180	+---------------------------------------+
181<
182Now you can insert new text, for example by typing: >
183
184	iA young <Esc>
185
186This begins an insert (the i), inserts the words "A young", and then exits
187insert mode (the final <Esc>).	The result:
188>
189	+---------------------------------------+
190	|A young intelligent turtle		|
191	|Found programming Unix a hurdle	|
192	|~					|
193	|~					|
194	|					|
195	+---------------------------------------+
196<
197
198DELETING A LINE
199
200To delete a whole line use the "dd" command.  The following line will
201then move up to fill the gap:
202>
203	+---------------------------------------+
204	|Found programming Unix a hurdle	|
205	|~					|
206	|~					|
207	|~					|
208	|					|
209	+---------------------------------------+
210<
211
212DELETING A LINE BREAK
213
214In Vim you can join two lines together, which means that the line break
215between them is deleted.  The "J" command does this.
216   Take these two lines:
217
218	A young intelligent ~
219	turtle ~
220
221Move the cursor to the first line and press "J":
222
223	A young intelligent turtle ~
224
225==============================================================================
226*02.5*	Undo and Redo
227
228Suppose you delete too much.  Well, you can type it in again, but an easier
229way exists.  The "u" command undoes the last edit.  Take a look at this in
230action: After using "dd" to delete the first line, "u" brings it back.
231   Another one: Move the cursor to the A in the first line:
232
233	A young intelligent turtle ~
234
235Now type xxxxxxx to delete "A young".  The result is as follows:
236
237	 intelligent turtle ~
238
239Type "u" to undo the last delete.  That delete removed the g, so the undo
240restores the character.
241
242	g intelligent turtle ~
243
244The next "u" command restores the next-to-last character deleted:
245
246	ng intelligent turtle ~
247
248The next "u" command gives you the u, and so on:
249
250	ung intelligent turtle ~
251	oung intelligent turtle ~
252	young intelligent turtle ~
253	 young intelligent turtle ~
254	A young intelligent turtle ~
255
256REDO
257
258If you undo too many times, you can press CTRL-R (redo) to reverse the
259preceding command.  In other words, it undoes the undo.  To see this in
260action, press CTRL-R twice.  The character A and the space after it disappear:
261
262	young intelligent turtle ~
263
264There's a special version of the undo command, the "U" (undo line) command.
265The undo line command undoes all the changes made on the last line that was
266edited.  Typing this command twice cancels the preceding "U".
267
268	A very intelligent turtle ~
269	  xxxx				Delete very
270
271	A intelligent turtle ~
272		      xxxxxx		Delete turtle
273
274	A intelligent ~
275					Restore line with "U"
276	A very intelligent turtle ~
277					Undo "U" with "u"
278	A intelligent ~
279
280The "U" command is a change by itself, which the "u" command undoes and CTRL-R
281redoes.  This might be a bit confusing.  Don't worry, with "u" and CTRL-R you
282can go to any of the situations you had.  More about that in section |32.2|.
283
284==============================================================================
285*02.6*	Other editing commands
286
287Vim has a large number of commands to change the text.  See |Q_in| and below.
288Here are a few often used ones.
289
290
291APPENDING
292
293The "i" command inserts a character before the character under the cursor.
294That works fine; but what happens if you want to add stuff to the end of the
295line?  For that you need to insert text after the cursor.  This is done with
296the "a" (append) command.
297   For example, to change the line
298
299	and that's not saying much for the turtle. ~
300to
301	and that's not saying much for the turtle!!! ~
302
303move the cursor over to the dot at the end of the line.  Then type "x" to
304delete the period.  The cursor is now positioned at the end of the line on the
305e in turtle.  Now type >
306
307	a!!!<Esc>
308
309to append three exclamation points after the e in turtle:
310
311	and that's not saying much for the turtle!!! ~
312
313
314OPENING UP A NEW LINE
315
316The "o" command creates a new, empty line below the cursor and puts Vim in
317Insert mode.  Then you can type the text for the new line.
318   Suppose the cursor is somewhere in the first of these two lines:
319
320	A very intelligent turtle ~
321	Found programming Unix a hurdle ~
322
323If you now use the "o" command and type new text: >
324
325	oThat liked using Vim<Esc>
326
327The result is:
328
329	A very intelligent turtle ~
330	That liked using Vim ~
331	Found programming Unix a hurdle ~
332
333The "O" command (uppercase) opens a line above the cursor.
334
335
336USING A COUNT
337
338Suppose you want to move up nine lines.  You can type "kkkkkkkkk" or you can
339enter the command "9k".  In fact, you can precede many commands with a number.
340Earlier in this chapter, for instance, you added three exclamation points to
341the end of a line by typing "a!!!<Esc>".  Another way to do this is to use the
342command "3a!<Esc>".  The count of 3 tells the command that follows to triple
343its effect.  Similarly, to delete three characters, use the command "3x".  The
344count always comes before the command it applies to.
345
346==============================================================================
347*02.7*	Getting out
348
349To exit, use the "ZZ" command.  This command writes the file and exits.
350
351	Note:
352	Unlike many other editors, Vim does not automatically make a backup
353	file.  If you type "ZZ", your changes are committed and there's no
354	turning back.  You can configure the Vim editor to produce backup
355	files; see |07.4|.
356
357
358DISCARDING CHANGES
359
360Sometimes you will make a sequence of changes and suddenly realize you were
361better off before you started.  Not to worry; Vim has a
362quit-and-throw-things-away command.  It is: >
363
364	:q!
365
366Don't forget to press <Enter> to finish the command.
367
368For those of you interested in the details, the three parts of this command
369are the colon (:), which enters Command-line mode; the q command, which tells
370the editor to quit; and the override command modifier (!).
371   The override command modifier is needed because Vim is reluctant to throw
372away changes.  If you were to just type ":q", Vim would display an error
373message and refuse to exit:
374
375	E37: No write since last change (use ! to override) ~
376
377By specifying the override, you are in effect telling Vim, "I know that what
378I'm doing looks stupid, but I really want to do this."
379
380If you want to continue editing with Vim: The ":e!" command reloads the
381original version of the file.
382
383==============================================================================
384*02.8*	Finding help
385
386Everything you always wanted to know can be found in the Vim help files.
387Don't be afraid to ask!
388
389If you know what you are looking for, it is usually easier to search for it
390using the help system, instead of using Google.  Because the subjects follow
391a certain style guide.
392
393Also the help has the advantage of belonging to your particular Vim version.
394You won't see help for commands added later.  These would not work for you.
395
396To get generic help use this command: >
397
398	:help
399
400You could also use the first function key <F1>.  If your keyboard has a <Help>
401key it might work as well.
402   If you don't supply a subject, ":help" displays the general help window.
403The creators of Vim did something very clever (or very lazy) with the help
404system: They made the help window a normal editing window.  You can use all
405the normal Vim commands to move through the help information.  Therefore h, j,
406k, and l move left, down, up and right.
407   To get out of the help window, use the same command you use to get out of
408the editor: "ZZ".  This will only close the help window, not exit Vim.
409
410As you read the help text, you will notice some text enclosed in vertical bars
411(for example, |help|).  This indicates a hyperlink.  If you position the
412cursor anywhere between the bars and press CTRL-] (jump to tag), the help
413system takes you to the indicated subject.  (For reasons not discussed here,
414the Vim terminology for a hyperlink is tag.  So CTRL-] jumps to the location
415of the tag given by the word under the cursor.)
416   After a few jumps, you might want to go back.  CTRL-T (pop tag) takes you
417back to the preceding position.  CTRL-O (jump to older position) also works
418nicely here.
419   At the top of the help screen, there is the notation "*help.txt*".  This
420name between "*" characters is used by the help system to define a tag
421(hyperlink destination).
422   See |29.1| for details about using tags.
423
424To get help on a given subject, use the following command: >
425
426	:help {subject}
427
428To get help on the "x" command, for example, enter the following: >
429
430	:help x
431
432To find out how to delete text, use this command: >
433
434	:help deleting
435
436To get a complete index of all Vim commands, use the following command: >
437
438	:help index
439
440When you need to get help for a control character command (for example,
441CTRL-A), you need to spell it with the prefix "CTRL-". >
442
443	:help CTRL-A
444
445The Vim editor has many different modes.  By default, the help system displays
446the normal-mode commands.  For example, the following command displays help
447for the normal-mode CTRL-H command: >
448
449	:help CTRL-H
450
451To identify other modes, use a mode prefix.  If you want the help for the
452insert-mode version of a command, use "i_".  For CTRL-H this gives you the
453following command: >
454
455	:help i_CTRL-H
456
457When you start the Vim editor, you can use several command-line arguments.
458These all begin with a dash (-).  To find what the -t argument does, for
459example, use the command: >
460
461	:help -t
462
463The Vim editor has a number of options that enable you to configure and
464customize the editor.  If you want help for an option, you need to enclose it
465in single quotation marks.  To find out what the 'number' option does, for
466example, use the following command: >
467
468	:help 'number'
469
470The table with all mode prefixes can be found below: |help-summary|.
471
472Special keys are enclosed in angle brackets.  To find help on the up-arrow key
473in Insert mode, for instance, use this command: >
474
475	:help i_<Up>
476
477If you see an error message that you don't understand, for example:
478
479	E37: No write since last change (use ! to override) ~
480
481You can use the error ID at the start to find help about it: >
482
483	:help E37
484
485
486Summary:					*help-summary*  >
487
4881) Use Ctrl-D after typing a topic and let Vim show all available topics.
489   Or press Tab to complete: >
490	:help some<Tab>
491<   More information on how to use the help: >
492	:help helphelp
493
4942) Follow the links in bars to related help.  You can go from the detailed
495   help to the user documentation, which describes certain commands more from
496   a user perspective and less detailed.  E.g. after: >
497	:help pattern.txt
498<   You can see the user guide topics |03.9| and |usr_27.txt| in the
499   introduction.
500
5013) Options are enclosed in single apostrophes.  To go to the help topic for
502   the list option: >
503	:help 'list'
504<   If you only know you are looking for a certain option, you can also do: >
505	:help options.txt
506<   to open the help page which describes all option handling and then search
507   using regular expressions, e.g. textwidth.
508   Certain options have their own namespace, e.g.: >
509	:help cpo-<letter>
510<   for the corresponding flag of the 'cpoptions' settings, substitute <letter>
511   by a specific flag, e.g.: >
512	:help cpo-;
513<   And for the 'guioptions' flags: >
514	:help go-<letter>
515
5164) Normal mode commands do not have a prefix.  To go to the help page for the
517   "gt" command: >
518	:help gt
519
5205) Insert mode commands start with i_.  Help for deleting a word: >
521	:help i_CTRL-W
522
5236) Visual mode commands start with v_.  Help for jumping to the other side of
524   the Visual area: >
525	:help v_o
526
5277) Command line editing and arguments start with c_.  Help for using the
528   command argument %: >
529	:help c_%
530
5318) Ex-commands always start with ":", so to go to the ":s" command help: >
532	:help :s
533
5349) Commands specifically for debugging start with ">".  To go to the help
535   for the "cont" debug command: >
536	:help >cont
537
53810) Key combinations.  They usually start with a single letter indicating
539    the mode for which they can be used.  E.g.: >
540	:help i_CTRL-X
541<    takes you to the family of CTRL-X commands for insert mode which can be
542    used to auto-complete different things.  Note, that certain keys will
543    always be written the same, e.g. Control will always be CTRL.
544    For normal mode commands there is no prefix and the topic is available at
545    :h CTRL-<Letter>. E.g.  >
546	:help CTRL-W
547<    In contrast >
548	:help c_CTRL-R
549<    will describe what the CTRL-R does when entering commands in the Command
550    line and >
551	:help v_CTRL-A
552<    talks about incrementing numbers in visual mode and >
553	:help g_CTRL-A
554<    talks about the "g<C-A>" command (e.g. you have to press "g" then
555    <CTRL-A>).  Here the "g" stands for the normal command "g" which always
556    expects a second key before doing something similar to the commands
557    starting with "z".
558
55911) Regexp items always start with /.  So to get help for the "\+" quantifier
560    in Vim regexes: >
561	:help /\+
562<    If you need to know everything about regular expressions, start reading
563    at: >
564	:help pattern.txt
565
56612) Registers always start with "quote".  To find out about the special ":"
567    register: >
568	:help quote:
569
57013) Vim script is available at >
571	:help vimeval.txt
572<    Certain aspects of the language are available at :h expr-X where "X" is a
573    single letter. E.g.  >
574	:help expr-!
575<    will take you to the topic describing the "!" (Not) operator for Vim
576    Script.
577    Also important is >
578	:help function-list
579<    to find a short description of all functions available.  Help topics for
580    Vim script functions always include the "()", so: >
581	:help append()
582<    talks about the append Vim script function rather than how to append text
583    in the current buffer.
584
58514) Mappings are talked about in the help page :h |map.txt|.  Use >
586	:help mapmode-i
587<    to find out about the |:imap| command.  Also use :map-topic
588    to find out about certain subtopics particular for mappings.  e.g: >
589	:help :map-local
590<    for buffer-local mappings or >
591	:help map-bar
592<    for how the '|' is handled in mappings.
593
59415) Command definitions are talked about :h command-topic, so use >
595	:help command-bang
596<    to find out about the '!' argument for custom commands.
597
59816) Window management commands always start with CTRL-W, so you find the
599    corresponding help at :h CTRL-W_letter.  E.g. >
600	:help CTRL-W_p
601<    for moving the previous accessed window.  You can also access >
602	:help windows.txt
603<    and read your way through if you are looking for window handling
604    commands.
605
60617) Use |:helpgrep| to search in all help pages (and also of any installed
607    plugins).  See |:helpgrep| for how to use it.
608    To search for a topic: >
609	:helpgrep topic
610<    This takes you to the first match.  To go to the next one: >
611	:cnext
612<    All matches are available in the quickfix window which can be opened
613    with: >
614	:copen
615<    Move around to the match you like and press Enter to jump to that help.
616
61718) The user manual.  This describes help topics for beginners in a rather
618    friendly way.  Start at |usr_toc.txt| to find the table of content (as you
619    might have guessed): >
620	:help usr_toc.txt
621<    Skim over the contents to find interesting topics.  The "Digraphs" and
622    "Entering special characters" items are in chapter 24, so to go to that
623    particular help page: >
624	:help usr_24.txt
625<    Also if you want to access a certain chapter in the help, the chapter
626    number can be accessed directly like this: >
627	:help 10.1
628<    which goes to chapter 10.1 in |usr_10.txt| and talks about recording
629    macros.
630
63119) Highlighting groups.  Always start with hl-groupname.  E.g. >
632	:help hl-WarningMsg
633<    talks about the WarningMsg highlighting group.
634
63520) Syntax highlighting is namespaced to :syn-topic.  E.g. >
636	:help :syn-conceal
637<    talks about the conceal argument for the ":syn" command.
638
63921) Quickfix commands usually start with :c while location list commands
640    usually start with :l
641
64222) Autocommand events can be found by their name: >
643	:help BufWinLeave
644<    To see all possible events: >
645	:help events
646
64723) Command-line switches always start with "-".  So for the help of the -f
648    command switch of Vim use: >
649	:help -f
650
65124) Lua language and Nvim's Lua standard library are available at >vim
652	:help lua.txt
653<    Guide to using Lua in Nvim is available at >vim
654	:help lua-guide.txt
655<    Lua 5.1 reference manual is available at >vim
656	:help luaref.txt
657<
65825) Documentation for included filetype specific functionality is usually
659    available  in the form ft-<filetype>-<functionality>.  So >
660	:help ft-c-syntax
661<    talks about the C syntax file and the option it provides.  Sometimes,
662    additional sections for omni completion >
663	:help ft-php-omni
664<    or filetype plugins >
665	:help ft-tex-plugin
666<    are available.
667
66826) Error and Warning codes can be looked up directly in the help.  So >
669	:help E297
670<    takes you exactly to the description of the swap error message and >
671	:help W10
672<    talks about the warning "Changing a readonly file".
673    Sometimes, however, those error codes are not described, but rather are
674    listed at the Vim command that usually causes this.  So: >
675	:help E128
676<    takes you to the |:function| command
677
67827) Documentation for packages distributed with Vim have the form
679     package-<name>. So >
680	:help package-termdebug
681<
682    will bring you to the help section for the included termdebug plugin and
683    how to enable it.
684
685
686==============================================================================
687
688Next chapter: |usr_03.txt|  Moving around
689
690Copyright: see |manual-copyright|
691
692vim:tw=78:ts=8:noet:ft=help:norl: