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