usr_08.txt 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. *usr_08.txt* Nvim
  2. VIM USER MANUAL - by Bram Moolenaar
  3. Splitting windows
  4. Display two different files above each other. Or view two locations in the
  5. file at the same time. See the difference between two files by putting them
  6. side by side. All this is possible with split windows.
  7. |08.1| Split a window
  8. |08.2| Split a window on another file
  9. |08.3| Window size
  10. |08.4| Vertical splits
  11. |08.5| Moving windows
  12. |08.6| Commands for all windows
  13. |08.7| Viewing differences with diff mode
  14. |08.8| Various
  15. |08.9| Tab pages
  16. Next chapter: |usr_09.txt| Using the GUI
  17. Previous chapter: |usr_07.txt| Editing more than one file
  18. Table of contents: |usr_toc.txt|
  19. ==============================================================================
  20. *08.1* Split a window
  21. The easiest way to open a new window is to use the following command: >
  22. :split
  23. This command splits the screen into two windows and leaves the cursor in the
  24. top one:
  25. +----------------------------------+
  26. |/* file one.c */ |
  27. |~ |
  28. |~ |
  29. |one.c=============================|
  30. |/* file one.c */ |
  31. |~ |
  32. |one.c=============================|
  33. | |
  34. +----------------------------------+
  35. What you see here is two windows on the same file. The line with "====" is
  36. the status line. It displays information about the window above it. (In
  37. practice the status line will be in reverse video.)
  38. The two windows allow you to view two parts of the same file. For example,
  39. you could make the top window show the variable declarations of a program, and
  40. the bottom one the code that uses these variables.
  41. The CTRL-W w command can be used to jump between the windows. If you are in
  42. the top window, CTRL-W w jumps to the window below it. If you are in the
  43. bottom window it will jump to the first window. (CTRL-W CTRL-W does the same
  44. thing, in case you let go of the CTRL key a bit later.)
  45. CLOSE THE WINDOW
  46. To close a window, use the command: >
  47. :close
  48. Actually, any command that quits editing a file works, like ":quit" and "ZZ".
  49. But ":close" prevents you from accidentally exiting Vim when you close the
  50. last window.
  51. CLOSING ALL OTHER WINDOWS
  52. If you have opened a whole bunch of windows, but now want to concentrate on
  53. one of them, this command will be useful: >
  54. :only
  55. This closes all windows, except for the current one. If any of the other
  56. windows has changes, you will get an error message and that window won't be
  57. closed.
  58. ==============================================================================
  59. *08.2* Split a window on another file
  60. The following command opens a second window and starts editing the given file:
  61. >
  62. :split two.c
  63. If you were editing one.c, then the result looks like this:
  64. +----------------------------------+
  65. |/* file two.c */ |
  66. |~ |
  67. |~ |
  68. |two.c=============================|
  69. |/* file one.c */ |
  70. |~ |
  71. |one.c=============================|
  72. | |
  73. +----------------------------------+
  74. To open a window on a new, empty file, use this: >
  75. :new
  76. You can repeat the ":split" and ":new" commands to create as many windows as
  77. you like.
  78. ==============================================================================
  79. *08.3* Window size
  80. The ":split" command can take a number argument. If specified, this will be
  81. the height of the new window. For example, the following opens a new window
  82. three lines high and starts editing the file alpha.c: >
  83. :3split alpha.c
  84. For existing windows you can change the size in several ways. When you have a
  85. working mouse, it is easy: Move the mouse pointer to the status line that
  86. separates two windows, and drag it up or down.
  87. To increase the size of a window: >
  88. CTRL-W +
  89. To decrease it: >
  90. CTRL-W -
  91. Both of these commands take a count and increase or decrease the window size
  92. by that many lines. Thus "4 CTRL-W +" make the window four lines higher.
  93. To set the window height to a specified number of lines: >
  94. {height}CTRL-W _
  95. That's: a number {height}, CTRL-W and then an underscore (the - key with Shift
  96. on English-US keyboards).
  97. To make a window as high as it can be, use the CTRL-W _ command without a
  98. count.
  99. USING THE MOUSE
  100. In Vim you can do many things very quickly from the keyboard. Unfortunately,
  101. the window resizing commands require quite a bit of typing. In this case,
  102. using the mouse is faster. Position the mouse pointer on a status line. Now
  103. press the left mouse button and drag. The status line will move, thus making
  104. the window on one side higher and the other smaller.
  105. OPTIONS
  106. The 'winheight' option can be set to a minimal desired height of a window and
  107. 'winminheight' to a hard minimum height.
  108. Likewise, there is 'winwidth' for the minimal desired width and
  109. 'winminwidth' for the hard minimum width.
  110. The 'equalalways' option, when set, makes Vim equalize the windows sizes
  111. when a window is closed or opened.
  112. ==============================================================================
  113. *08.4* Vertical splits
  114. The ":split" command creates the new window above the current one. To make
  115. the window appear at the left side, use: >
  116. :vsplit
  117. or: >
  118. :vsplit two.c
  119. The result looks something like this:
  120. +--------------------------------------+
  121. |/* file two.c */ |/* file one.c */ |
  122. |~ |~ |
  123. |~ |~ |
  124. |~ |~ |
  125. |two.c===============one.c=============|
  126. | |
  127. +--------------------------------------+
  128. Actually, the | lines in the middle will be in reverse video. This is called
  129. the vertical separator. It separates the two windows left and right of it.
  130. There is also the ":vnew" command, to open a vertically split window on a new,
  131. empty file. Another way to do this: >
  132. :vertical new
  133. The ":vertical" command can be inserted before another command that splits a
  134. window. This will cause that command to split the window vertically instead
  135. of horizontally. (If the command doesn't split a window, it works
  136. unmodified.)
  137. MOVING BETWEEN WINDOWS
  138. Since you can split windows horizontally and vertically as much as you like,
  139. you can create almost any layout of windows. Then you can use these commands
  140. to move between them:
  141. CTRL-W h move to the window on the left
  142. CTRL-W j move to the window below
  143. CTRL-W k move to the window above
  144. CTRL-W l move to the window on the right
  145. CTRL-W t move to the TOP window
  146. CTRL-W b move to the BOTTOM window
  147. You will notice the same letters as used for moving the cursor. And the
  148. cursor keys can also be used, if you like.
  149. More commands to move to other windows: |Q_wi|.
  150. ==============================================================================
  151. *08.5* Moving windows
  152. You have split a few windows, but now they are in the wrong place. Then you
  153. need a command to move the window somewhere else. For example, you have three
  154. windows like this:
  155. +----------------------------------+
  156. |/* file two.c */ |
  157. |~ |
  158. |~ |
  159. |two.c=============================|
  160. |/* file three.c */ |
  161. |~ |
  162. |~ |
  163. |three.c===========================|
  164. |/* file one.c */ |
  165. |~ |
  166. |one.c=============================|
  167. | |
  168. +----------------------------------+
  169. Clearly the last one should be at the top. Go to that window (using CTRL-W w)
  170. and then type this command: >
  171. CTRL-W K
  172. This uses the uppercase letter K. What happens is that the window is moved to
  173. the very top. You will notice that K is again used for moving upwards.
  174. When you have vertical splits, CTRL-W K will move the current window to the
  175. top and make it occupy the full width of the Vim window. If this is your
  176. layout:
  177. +-------------------------------------------+
  178. |/* two.c */ |/* three.c */ |/* one.c */ |
  179. |~ |~ |~ |
  180. |~ |~ |~ |
  181. |~ |~ |~ |
  182. |~ |~ |~ |
  183. |~ |~ |~ |
  184. |two.c=========three.c=========one.c========|
  185. | |
  186. +-------------------------------------------+
  187. Then using CTRL-W K in the middle window (three.c) will result in:
  188. +-------------------------------------------+
  189. |/* three.c */ |
  190. |~ |
  191. |~ |
  192. |three.c====================================|
  193. |/* two.c */ |/* one.c */ |
  194. |~ |~ |
  195. |two.c==================one.c===============|
  196. | |
  197. +-------------------------------------------+
  198. The other three similar commands (you can probably guess these now):
  199. CTRL-W H move window to the far left
  200. CTRL-W J move window to the bottom
  201. CTRL-W L move window to the far right
  202. ==============================================================================
  203. *08.6* Commands for all windows
  204. When you have several windows open and you want to quit Vim, you can close
  205. each window separately. A quicker way is using this command: >
  206. :qall
  207. This stands for "quit all". If any of the windows contain changes, Vim will
  208. not exit. The cursor will automatically be positioned in a window with
  209. changes. You can then either use ":write" to save the changes, or ":quit!" to
  210. throw them away.
  211. If you know there are windows with changes, and you want to save all these
  212. changes, use this command: >
  213. :wall
  214. This stands for "write all". But actually, it only writes files with
  215. changes. Vim knows it doesn't make sense to write files that were not
  216. changed.
  217. And then there is the combination of ":qall" and ":wall": the "write and
  218. quit all" command: >
  219. :wqall
  220. This writes all modified files and quits Vim.
  221. Finally, there is a command that quits Vim and throws away all changes: >
  222. :qall!
  223. Be careful, there is no way to undo this command!
  224. OPENING A WINDOW FOR ALL ARGUMENTS
  225. To make Vim open a window for each file, start it with the "-o" argument: >
  226. vim -o one.txt two.txt three.txt
  227. This results in:
  228. +-------------------------------+
  229. |file one.txt |
  230. |~ |
  231. |one.txt========================|
  232. |file two.txt |
  233. |~ |
  234. |two.txt========================|
  235. |file three.txt |
  236. |~ |
  237. |three.txt======================|
  238. | |
  239. +-------------------------------+
  240. The "-O" argument is used to get vertically split windows.
  241. When Vim is already running, the ":all" command opens a window for each
  242. file in the argument list. ":vertical all" does it with vertical splits.
  243. ==============================================================================
  244. *08.7* Viewing differences with diff mode
  245. There is a special way to start Nvim, which shows the differences between two
  246. files. Let's take a file "main.c" and insert a few characters in one line.
  247. Write this file with the 'backup' option set, so that the backup file
  248. "main.c~" will contain the previous version of the file.
  249. Type this command in a shell to start Nvim in diff mode: >
  250. nvim -d main.c~ main.c
  251. Vim will start, with two windows side by side. You will only see the line
  252. in which you added characters, and a few lines above and below it.
  253. VV VV
  254. +-----------------------------------------+
  255. |+ +--123 lines: /* a|+ +--123 lines: /* a| <- fold
  256. | text | text |
  257. | text | text |
  258. | text | text |
  259. | text | changed text | <- changed line
  260. | text | text |
  261. | text | ------------------| <- deleted line
  262. | text | text |
  263. | text | text |
  264. | text | text |
  265. |+ +--432 lines: text|+ +--432 lines: text| <- fold
  266. | ~ | ~ |
  267. | ~ | ~ |
  268. |main.c~==============main.c==============|
  269. | |
  270. +-----------------------------------------+
  271. (This picture doesn't show the highlighting, use "nvim -d" for that.)
  272. The lines that were not modified have been collapsed into one line. This is
  273. called a closed fold. They are indicated in the picture with "<- fold". Thus
  274. the single fold line at the top stands for 123 text lines. These lines are
  275. equal in both files.
  276. The line marked with "<- changed line" is highlighted, and the inserted
  277. text is displayed with another color. This clearly shows what the difference
  278. is between the two files.
  279. The line that was deleted is displayed with "---" in the main.c window.
  280. See the "<- deleted line" marker in the picture. These characters are not
  281. really there. They just fill up main.c, so that it displays the same number
  282. of lines as the other window.
  283. THE FOLD COLUMN
  284. Each window has a column on the left with a slightly different background. In
  285. the picture above these are indicated with "VV". You notice there is a plus
  286. character there, in front of each closed fold. Move the mouse pointer to that
  287. plus and click the left button. The fold will open, and you can see the text
  288. that it contains.
  289. The fold column contains a minus sign for an open fold. If you click on
  290. this -, the fold will close.
  291. Obviously, this only works when you have a working mouse. You can also use
  292. "zo" to open a fold and "zc" to close it.
  293. DIFFING IN VIM
  294. Another way to start in diff mode can be done from inside Vim. Edit the
  295. "main.c" file, then make a split and show the differences: >
  296. :edit main.c
  297. :vertical diffsplit main.c~
  298. The ":vertical" command is used to make the window split vertically. If you
  299. omit this, you will get a horizontal split.
  300. If you have a patch or diff file, you can use the third way to start diff
  301. mode. First edit the file to which the patch applies. Then tell Vim the name
  302. of the patch file: >
  303. :edit main.c
  304. :vertical diffpatch main.c.diff
  305. WARNING: The patch file must contain only one patch, for the file you are
  306. editing. Otherwise you will get a lot of error messages, and some files might
  307. be patched unexpectedly.
  308. The patching will only be done to the copy of the file in Vim. The file on
  309. your harddisk will remain unmodified (until you decide to write the file).
  310. SCROLL BINDING
  311. When the files have more changes, you can scroll in the usual way. Vim will
  312. try to keep both the windows start at the same position, so you can easily see
  313. the differences side by side.
  314. When you don't want this for a moment, use this command: >
  315. :set noscrollbind
  316. JUMPING TO CHANGES
  317. When you have disabled folding in some way, it may be difficult to find the
  318. changes. Use this command to jump forward to the next change: >
  319. ]c
  320. To go the other way use: >
  321. [c
  322. Prepended a count to jump further away.
  323. REMOVING CHANGES
  324. You can move text from one window to the other. This either removes
  325. differences or adds new ones. Vim doesn't keep the highlighting updated in
  326. all situations. To update it use this command: >
  327. :diffupdate
  328. To remove a difference, you can move the text in a highlighted block from one
  329. window to another. Take the "main.c" and "main.c~" example above. Move the
  330. cursor to the left window, on the line that was deleted in the other window.
  331. Now type this command: >
  332. dp
  333. The change will be removed by putting the text of the current window in the
  334. other window. "dp" stands for "diff put".
  335. You can also do it the other way around. Move the cursor to the right
  336. window, to the line where "changed" was inserted. Now type this command: >
  337. do
  338. The change will now be removed by getting the text from the other window.
  339. Since there are no changes left now, Vim puts all text in a closed fold.
  340. "do" stands for "diff obtain". "dg" would have been better, but that already
  341. has a different meaning ("dgg" deletes from the cursor until the first line).
  342. For details about diff mode, see |diff-mode|.
  343. ==============================================================================
  344. *08.8* Various
  345. The 'laststatus' option can be used to specify when the last window has a
  346. statusline:
  347. 0 never
  348. 1 only when there are split windows (the default)
  349. 2 always
  350. 3 have a global statusline at the bottom instead of one for each
  351. window
  352. Many commands that edit another file have a variant that splits the window.
  353. For Command-line commands this is done by prepending an "s". For example:
  354. ":tag" jumps to a tag, ":stag" splits the window and jumps to a
  355. tag.
  356. For Normal mode commands a CTRL-W is prepended. CTRL-^ jumps to the
  357. alternate file, CTRL-W CTRL-^ splits the window and edits the alternate file.
  358. The 'splitbelow' option can be set to make a new window appear below the
  359. current window. The 'splitright' option can be set to make a vertically split
  360. window appear right of the current window.
  361. When splitting a window you can prepend a modifier command to tell where the
  362. window is to appear:
  363. :leftabove {cmd} left or above the current window
  364. :aboveleft {cmd} idem
  365. :rightbelow {cmd} right or below the current window
  366. :belowright {cmd} idem
  367. :topleft {cmd} at the top or left of the Vim window
  368. :botright {cmd} at the bottom or right of the Vim window
  369. ==============================================================================
  370. *08.9* Tab pages
  371. You will have noticed that windows never overlap. That means you quickly run
  372. out of screen space. The solution for this is called Tab pages.
  373. Assume you are editing "thisfile". To create a new tab page use this command: >
  374. :tabedit thatfile
  375. This will edit the file "thatfile" in a window that occupies the whole Vim
  376. window. And you will notice a bar at the top with the two file names:
  377. +----------------------------------+
  378. | thisfile | /thatfile/ __________X| (thatfile is bold)
  379. |/* thatfile */ |
  380. |that |
  381. |that |
  382. |~ |
  383. |~ |
  384. |~ |
  385. | |
  386. +----------------------------------+
  387. You now have two tab pages. The first one has a window for "thisfile" and the
  388. second one a window for "thatfile". It's like two pages that are on top of
  389. each other, with a tab sticking out of each page showing the file name.
  390. Now use the mouse to click on "thisfile" in the top line. The result is
  391. +----------------------------------+
  392. | /thisfile/ | thatfile __________X| (thisfile is bold)
  393. |/* thisfile */ |
  394. |this |
  395. |this |
  396. |~ |
  397. |~ |
  398. |~ |
  399. | |
  400. +----------------------------------+
  401. Thus you can switch between tab pages by clicking on the label in the top
  402. line. If you don't have a mouse or don't want to use it, you can use the "gt"
  403. command. Mnemonic: Goto Tab.
  404. Now let's create another tab page with the command: >
  405. :tab split
  406. This makes a new tab page with one window that is editing the same buffer as
  407. the window we were in:
  408. +-------------------------------------+
  409. | thisfile | /thisfile/ | thatfile __X| (thisfile is bold)
  410. |/* thisfile */ |
  411. |this |
  412. |this |
  413. |~ |
  414. |~ |
  415. |~ |
  416. | |
  417. +-------------------------------------+
  418. You can put ":tab" before any Ex command that opens a window. The window will
  419. be opened in a new tab page. Another example: >
  420. :tab help gt
  421. Will show the help text for "gt" in a new tab page.
  422. A few more things you can do with tab pages:
  423. - click with the mouse in the space after the last label
  424. The next tab page will be selected, like with "gt".
  425. - click with the mouse on the "X" in the top right corner
  426. The current tab page will be closed. Unless there are unsaved
  427. changes in the current tab page.
  428. - double click with the mouse in the top line
  429. A new tab page will be created.
  430. - the "tabonly" command
  431. Closes all tab pages except the current one. Unless there are unsaved
  432. changes in other tab pages.
  433. For more information about tab pages see |tab-page|.
  434. ==============================================================================
  435. Next chapter: |usr_09.txt| Using the GUI
  436. Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: