usr_02.txt 23 KB

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