Vim 7.2b ready for testing.
[MacVim.git] / runtime / doc / usr_08.txt
blob5c3a93e548a3e9d83578fc43f13e1ed7ae6ecd54
1 *usr_08.txt*    For Vim version 7.2b.  Last change: 2006 Jul 18
3                      VIM USER MANUAL - by Bram Moolenaar
5                               Splitting windows
8 Display two different files above each other.  Or view two locations in the
9 file at the same time.  See the difference between two files by putting them
10 side by side.  All this is possible with split windows.
12 |08.1|  Split a window
13 |08.2|  Split a window on another file
14 |08.3|  Window size
15 |08.4|  Vertical splits
16 |08.5|  Moving windows
17 |08.6|  Commands for all windows
18 |08.7|  Viewing differences with vimdiff
19 |08.8|  Various
20 |08.9|  Tab pages
22      Next chapter: |usr_09.txt|  Using the GUI
23  Previous chapter: |usr_07.txt|  Editing more than one file
24 Table of contents: |usr_toc.txt|
26 ==============================================================================
27 *08.1*  Split a window
29 The easiest way to open a new window is to use the following command: >
31         :split
33 This command splits the screen into two windows and leaves the cursor in the
34 top one:
36         +----------------------------------+
37         |/* file one.c */                  |
38         |~                                 |
39         |~                                 |
40         |one.c=============================|
41         |/* file one.c */                  |
42         |~                                 |
43         |one.c=============================|
44         |                                  |
45         +----------------------------------+
47 What you see here is two windows on the same file.  The line with "====" is
48 that status line.  It displays information about the window above it.  (In
49 practice the status line will be in reverse video.)
50    The two windows allow you to view two parts of the same file.  For example,
51 you could make the top window show the variable declarations of a program, and
52 the bottom one the code that uses these variables.
54 The CTRL-W w command can be used to jump between the windows.  If you are in
55 the top window, CTRL-W w jumps to the window below it.  If you are in the
56 bottom window it will jump to the first window.  (CTRL-W CTRL-W does the same
57 thing, in case you let go of the CTRL key a bit later.)
60 CLOSE THE WINDOW
62 To close a window, use the command: >
64         :close
66 Actually, any command that quits editing a file works, like ":quit" and "ZZ".
67 But ":close" prevents you from accidentally exiting Vim when you close the
68 last window.
71 CLOSING ALL OTHER WINDOWS
73 If you have opened a whole bunch of windows, but now want to concentrate on
74 one of them, this command will be useful: >
76         :only
78 This closes all windows, except for the current one.  If any of the other
79 windows has changes, you will get an error message and that window won't be
80 closed.
82 ==============================================================================
83 *08.2*  Split a window on another file
85 The following command opens a second window and starts editing the given file:
87         :split two.c
89 If you were editing one.c, then the result looks like this:
91         +----------------------------------+
92         |/* file two.c */                  |
93         |~                                 |
94         |~                                 |
95         |two.c=============================|
96         |/* file one.c */                  |
97         |~                                 |
98         |one.c=============================|
99         |                                  |
100         +----------------------------------+
102 To open a window on a new, empty file, use this: >
104         :new
106 You can repeat the ":split" and ":new" commands to create as many windows as
107 you like.
109 ==============================================================================
110 *08.3*  Window size
112 The ":split" command can take a number argument.  If specified, this will be
113 the height of the new window.  For example, the following opens a new window
114 three lines high and starts editing the file alpha.c: >
116         :3split alpha.c
118 For existing windows you can change the size in several ways.  When you have a
119 working mouse, it is easy: Move the mouse pointer to the status line that
120 separates two windows, and drag it up or down.
122 To increase the size of a window: >
124         CTRL-W +
126 To decrease it: >
128         CTRL-W -
130 Both of these commands take a count and increase or decrease the window size
131 by that many lines.  Thus "4 CTRL-W +" make the window four lines higher.
133 To set the window height to a specified number of lines: >
135         {height}CTRL-W _
137 That's: a number {height}, CTRL-W and then an underscore (the - key with Shift
138 on English-US keyboards).
139    To make a window as high as it can be, use the CTRL-W _ command without a
140 count.
143 USING THE MOUSE
145 In Vim you can do many things very quickly from the keyboard.  Unfortunately,
146 the window resizing commands require quite a bit of typing.  In this case,
147 using the mouse is faster.  Position the mouse pointer on a status line.  Now
148 press the left mouse button and drag.  The status line will move, thus making
149 the window on one side higher and the other smaller.
152 OPTIONS
154 The 'winheight' option can be set to a minimal desired height of a window and
155 'winminheight' to a hard minimum height.
156    Likewise, there is 'winwidth' for the minimal desired width and
157 'winminwidth' for the hard minimum width.
158    The 'equalalways' option, when set, makes Vim equalize the windows sizes
159 when a window is closed or opened.
161 ==============================================================================
162 *08.4*  Vertical splits
164 The ":split" command creates the new window above the current one.  To make
165 the window appear at the left side, use: >
167         :vsplit
169 or: >
170         :vsplit two.c
172 The result looks something like this:
174         +--------------------------------------+
175         |/* file two.c */   |/* file one.c */  |
176         |~                  |~                 |
177         |~                  |~                 |
178         |~                  |~                 |
179         |two.c===============one.c=============|
180         |                                      |
181         +--------------------------------------+
183 Actually, the | lines in the middle will be in reverse video.  This is called
184 the vertical separator.  It separates the two windows left and right of it.
186 There is also the ":vnew" command, to open a vertically split window on a new,
187 empty file.  Another way to do this: >
189         :vertical new
191 The ":vertical" command can be inserted before another command that splits a
192 window.  This will cause that command to split the window vertically instead
193 of horizontally.  (If the command doesn't split a window, it works
194 unmodified.)
197 MOVING BETWEEN WINDOWS
199 Since you can split windows horizontally and vertically as much as you like,
200 you can create almost any layout of windows.  Then you can use these commands
201 to move between them:
203         CTRL-W h        move to the window on the left
204         CTRL-W j        move to the window below
205         CTRL-W k        move to the window above
206         CTRL-W l        move to the window on the right
208         CTRL-W t        move to the TOP window
209         CTRL-W b        move to the BOTTOM window
211 You will notice the same letters as used for moving the cursor.  And the
212 cursor keys can also be used, if you like.
213    More commands to move to other windows: |Q_wi|.
215 ==============================================================================
216 *08.5*  Moving windows
218 You have split a few windows, but now they are in the wrong place.  Then you
219 need a command to move the window somewhere else.  For example, you have three
220 windows like this:
222         +----------------------------------+
223         |/* file two.c */                  |
224         |~                                 |
225         |~                                 |
226         |two.c=============================|
227         |/* file three.c */                |
228         |~                                 |
229         |~                                 |
230         |three.c===========================|
231         |/* file one.c */                  |
232         |~                                 |
233         |one.c=============================|
234         |                                  |
235         +----------------------------------+
237 Clearly the last one should be at the top.  Go to that window (using CTRL-W w)
238 and the type this command: >
240         CTRL-W K
242 This uses the uppercase letter K.  What happens is that the window is moved to
243 the very top.  You will notice that K is again used for moving upwards.
244    When you have vertical splits, CTRL-W K will move the current window to the
245 top and make it occupy the full width of the Vim window.  If this is your
246 layout:
248         +-------------------------------------------+
249         |/* two.c */  |/* three.c */  |/* one.c */  |
250         |~            |~              |~            |
251         |~            |~              |~            |
252         |~            |~              |~            |
253         |~            |~              |~            |
254         |~            |~              |~            |
255         |two.c=========three.c=========one.c========|
256         |                                           |
257         +-------------------------------------------+
259 Then using CTRL-W K in the middle window (three.c) will result in:
261         +-------------------------------------------+
262         |/* three.c */                              |
263         |~                                          |
264         |~                                          |
265         |three.c====================================|
266         |/* two.c */           |/* one.c */         |
267         |~                     |~                   |
268         |two.c==================one.c===============|
269         |                                           |
270         +-------------------------------------------+
272 The other three similar commands (you can probably guess these now):
274         CTRL-W H        move window to the far left
275         CTRL-W J        move window to the bottom
276         CTRL-W L        move window to the far right
278 ==============================================================================
279 *08.6*  Commands for all windows
281 When you have several windows open and you want to quit Vim, you can close
282 each window separately.  A quicker way is using this command: >
284         :qall
286 This stands for "quit all".  If any of the windows contain changes, Vim will
287 not exit.  The cursor will automatically be positioned in a window with
288 changes.  You can then either use ":write" to save the changes, or ":quit!" to
289 throw them away.
291 If you know there are windows with changes, and you want to save all these
292 changes, use this command: >
294         :wall
296 This stands for "write all".  But actually, it only writes files with
297 changes.  Vim knows it doesn't make sense to write files that were not
298 changed.
299    And then there is the combination of ":qall" and ":wall": the "write and
300 quit all" command: >
302         :wqall
304 This writes all modified files and quits Vim.
305    Finally, there is a command that quits Vim and throws away all changes: >
307         :qall!
309 Be careful, there is no way to undo this command!
312 OPENING A WINDOW FOR ALL ARGUMENTS
314 To make Vim open a window for each file, start it with the "-o" argument: >
316         vim -o one.txt two.txt three.txt
318 This results in:
320         +-------------------------------+
321         |file one.txt                   |
322         |~                              |
323         |one.txt========================|
324         |file two.txt                   |
325         |~                              |
326         |two.txt========================|
327         |file three.txt                 |
328         |~                              |
329         |three.txt======================|
330         |                               |
331         +-------------------------------+
333 The "-O" argument is used to get vertically split windows.
334    When Vim is already running, the ":all" command opens a window for each
335 file in the argument list.  ":vertical all" does it with vertical splits.
337 ==============================================================================
338 *08.7*  Viewing differences with vimdiff
340 There is a special way to start Vim, which shows the differences between two
341 files.  Let's take a file "main.c" and insert a few characters in one line.
342 Write this file with the 'backup' option set, so that the backup file
343 "main.c~" will contain the previous version of the file.
344    Type this command in a shell (not in Vim): >
346         vimdiff main.c~ main.c
348 Vim will start, with two windows side by side.  You will only see the line
349 in which you added characters, and a few lines above and below it.
351          VV                   VV
352         +-----------------------------------------+
353         |+ +--123 lines: /* a|+ +--123 lines: /* a|  <- fold
354         |  text              |  text              |
355         |  text              |  text              |
356         |  text              |  text              |
357         |  text              |  changed text      |  <- changed line
358         |  text              |  text              |
359         |  text              |  ------------------|  <- deleted line
360         |  text              |  text              |
361         |  text              |  text              |
362         |  text              |  text              |
363         |+ +--432 lines: text|+ +--432 lines: text|  <- fold
364         |  ~                 |  ~                 |
365         |  ~                 |  ~                 |
366         |main.c~==============main.c==============|
367         |                                         |
368         +-----------------------------------------+
370 (This picture doesn't show the highlighting, use the vimdiff command for a
371 better look.)
373 The lines that were not modified have been collapsed into one line.  This is
374 called a closed fold.  They are indicated in the picture with "<- fold".  Thus
375 the single fold line at the top stands for 123 text lines.  These lines are
376 equal in both files.
377    The line marked with "<- changed line" is highlighted, and the inserted
378 text is displayed with another color.  This clearly shows what the difference
379 is between the two files.
380    The line that was deleted is displayed with "---" in the main.c window.
381 See the "<- deleted line" marker in the picture.  These characters are not
382 really there.  They just fill up main.c, so that it displays the same number
383 of lines as the other window.
386 THE FOLD COLUMN
388 Each window has a column on the left with a slightly different background.  In
389 the picture above these are indicated with "VV".  You notice there is a plus
390 character there, in front of each closed fold.  Move the mouse pointer to that
391 plus and click the left button.  The fold will open, and you can see the text
392 that it contains.
393    The fold column contains a minus sign for an open fold.  If you click on
394 this -, the fold will close.
395    Obviously, this only works when you have a working mouse.  You can also use
396 "zo" to open a fold and "zc" to close it.
399 DIFFING IN VIM
401 Another way to start in diff mode can be done from inside Vim.  Edit the
402 "main.c" file, then make a split and show the differences: >
404         :edit main.c
405         :vertical diffsplit main.c~ 
407 The ":vertical" command is used to make the window split vertically.  If you
408 omit this, you will get a horizontal split.
410 If you have a patch or diff file, you can use the third way to start diff
411 mode.  First edit the file to which the patch applies.  Then tell Vim the name
412 of the patch file: >
414         :edit main.c
415         :vertical diffpatch main.c.diff
417 WARNING: The patch file must contain only one patch, for the file you are
418 editing.  Otherwise you will get a lot of error messages, and some files might
419 be patched unexpectedly.
420    The patching will only be done to the copy of the file in Vim.  The file on
421 your harddisk will remain unmodified (until you decide to write the file).
424 SCROLL BINDING
426 When the files have more changes, you can scroll in the usual way.  Vim will
427 try to keep both the windows start at the same position, so you can easily see
428 the differences side by side.
429    When you don't want this for a moment, use this command: >
431         :set noscrollbind
434 JUMPING TO CHANGES
436 When you have disabled folding in some way, it may be difficult to find the
437 changes.  Use this command to jump forward to the next change: >
439         ]c
441 To go the other way use: >
443         [c
445 Prepended a count to jump further away.
448 REMOVING CHANGES
450 You can move text from one window to the other.  This either removes
451 differences or adds new ones.  Vim doesn't keep the highlighting updated in
452 all situations.  To update it use this command: >
454         :diffupdate
456 To remove a difference, you can move the text in a highlighted block from one
457 window to another.  Take the "main.c" and "main.c~" example above.  Move the
458 cursor to the left window, on the line that was deleted in the other window.
459 Now type this command: >
461         dp
463 The change will be removed by putting the text of the current window in the
464 other window.  "dp" stands for "diff put".
465    You can also do it the other way around.  Move the cursor to the right
466 window, to the line where "changed" was inserted.  Now type this command: >
468         do
470 The change will now be removed by getting the text from the other window.
471 Since there are no changes left now, Vim puts all text in a closed fold.
472 "do" stands for "diff obtain".  "dg" would have been better, but that already
473 has a different meaning ("dgg" deletes from the cursor until the first line).
475 For details about diff mode, see |vimdiff|.
477 ==============================================================================
478 *08.8*  Various
480 The 'laststatus' option can be used to specify when the last window has a
481 statusline:
483         0       never
484         1       only when there are split windows (the default)
485         2       always
487 Many commands that edit another file have a variant that splits the window.
488 For Command-line commands this is done by prepending an "s".  For example:
489 ":tag" jumps to a tag, ":stag" splits the window and jumps to a
490 tag.
491    For Normal mode commands a CTRL-W is prepended.  CTRL-^ jumps to the
492 alternate file, CTRL-W CTRL-^ splits the window and edits the alternate file.
494 The 'splitbelow' option can be set to make a new window appear below the
495 current window.  The 'splitright' option can be set to make a vertically split
496 window appear right of the current window.
498 When splitting a window you can prepend a modifier command to tell where the
499 window is to appear:
501         :leftabove {cmd}        left or above the current window
502         :aboveleft {cmd}        idem
503         :rightbelow {cmd}       right or below the current window
504         :belowright {cmd}       idem
505         :topleft {cmd}          at the top or left of the Vim window
506         :botright {cmd}         at the bottom or right of the Vim window
509 ==============================================================================
510 *08.9*  Tab pages
512 You will have noticed that windows never overlap.  That means you quickly run
513 out of screen space.  The solution for this is called Tab pages.
515 Assume you are editing "thisfile".  To create a new tab page use this command: >
517         :tabedit thatfile
519 This will edit the file "thatfile" in a window that occupies the whole Vim
520 window.  And you will notice a bar at the top with the two file names:
522         +----------------------------------+
523         | thisfile | /thatfile/ __________X|    (thatfile is bold)
524         |/* thatfile */                    |
525         |that                              |
526         |that                              |
527         |~                                 |
528         |~                                 |
529         |~                                 |
530         |                                  |
531         +----------------------------------+
533 You now have two tab pages.  The first one has a window for "thisfile" and the
534 second one a window for "thatfile".  It's like two pages that are on top of
535 eachother, with a tab sticking out of each page showing the file name.
537 Now use the mouse to click on "thisfile" in the top line.  The result is
539         +----------------------------------+
540         | /thisfile/ | thatfile __________X|    (thisfile is bold)
541         |/* thisfile */                    |
542         |this                              |
543         |this                              |
544         |~                                 |
545         |~                                 |
546         |~                                 |
547         |                                  |
548         +----------------------------------+
550 Thus you can switch between tab pages by clicking on the label in the top
551 line.  If you don't have a mouse or don't want to use it, you can use the "gt"
552 command.  Mnemonic: Goto Tab.
554 Now let's create another tab page with the command: >
556         :tab split
558 This makes a new tab page with one window that is editing the same buffer as
559 the window we were in:
561         +-------------------------------------+
562         | thisfile | /thisfile/ | thatfile __X|   (thisfile is bold)
563         |/* thisfile */                       |
564         |this                                 |
565         |this                                 |
566         |~                                    |
567         |~                                    |
568         |~                                    |
569         |                                     |
570         +-------------------------------------+
572 You can put ":tab" before any Ex command that opens a window.  The window will
573 be opened in a new tab page.  Another example: >
575         :tab help gt
577 Will show the help text for "gt" in a new tab page.
579 A few more things you can do with tab pages:
581 - click with the mouse in the space after the last label
582         The next tab page will be selected, like with "gt".
584 - click with the mouse on the "X" in the top right corner
585         The current tab page will be closed.  Unless there are unsaved
586         changes in the current tab page.
588 - double click with the mouse in the top line
589         A new tab page will be created.
591 - the "tabonly" command
592         Closes all tab pages except the current one.  Unless there are unsaved
593         changes in other tab pages.
595 For more information about tab pages see |tab-page|.
597 ==============================================================================
599 Next chapter: |usr_09.txt|  Using the GUI
601 Copyright: see |manual-copyright|  vim:tw=78:ts=8:ft=help:norl: