1*usr_08.txt* For Vim version 7.3. Last change: 2006 Jul 18 2 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|08.9| Tab pages 21 22 Next chapter: |usr_09.txt| Using the GUI 23 Previous chapter: |usr_07.txt| Editing more than one file 24Table of contents: |usr_toc.txt| 25 26============================================================================== 27*08.1* Split a window 28 29The easiest way to open a new window is to use the following command: > 30 31 :split 32 33This command splits the screen into two windows and leaves the cursor in the 34top one: 35 36 +----------------------------------+ 37 |/* file one.c */ | 38 |~ | 39 |~ | 40 |one.c=============================| 41 |/* file one.c */ | 42 |~ | 43 |one.c=============================| 44 | | 45 +----------------------------------+ 46 47What you see here is two windows on the same file. The line with "====" is 48that status line. It displays information about the window above it. (In 49practice the status line will be in reverse video.) 50 The two windows allow you to view two parts of the same file. For example, 51you could make the top window show the variable declarations of a program, and 52the bottom one the code that uses these variables. 53 54The CTRL-W w command can be used to jump between the windows. If you are in 55the top window, CTRL-W w jumps to the window below it. If you are in the 56bottom window it will jump to the first window. (CTRL-W CTRL-W does the same 57thing, in case you let go of the CTRL key a bit later.) 58 59 60CLOSE THE WINDOW 61 62To close a window, use the command: > 63 64 :close 65 66Actually, any command that quits editing a file works, like ":quit" and "ZZ". 67But ":close" prevents you from accidentally exiting Vim when you close the 68last window. 69 70 71CLOSING ALL OTHER WINDOWS 72 73If you have opened a whole bunch of windows, but now want to concentrate on 74one of them, this command will be useful: > 75 76 :only 77 78This closes all windows, except for the current one. If any of the other 79windows has changes, you will get an error message and that window won't be 80closed. 81 82============================================================================== 83*08.2* Split a window on another file 84 85The following command opens a second window and starts editing the given file: 86> 87 :split two.c 88 89If you were editing one.c, then the result looks like this: 90 91 +----------------------------------+ 92 |/* file two.c */ | 93 |~ | 94 |~ | 95 |two.c=============================| 96 |/* file one.c */ | 97 |~ | 98 |one.c=============================| 99 | | 100 +----------------------------------+ 101 102To open a window on a new, empty file, use this: > 103 104 :new 105 106You can repeat the ":split" and ":new" commands to create as many windows as 107you like. 108 109============================================================================== 110*08.3* Window size 111 112The ":split" command can take a number argument. If specified, this will be 113the height of the new window. For example, the following opens a new window 114three lines high and starts editing the file alpha.c: > 115 116 :3split alpha.c 117 118For existing windows you can change the size in several ways. When you have a 119working mouse, it is easy: Move the mouse pointer to the status line that 120separates two windows, and drag it up or down. 121 122To increase the size of a window: > 123 124 CTRL-W + 125 126To decrease it: > 127 128 CTRL-W - 129 130Both of these commands take a count and increase or decrease the window size 131by that many lines. Thus "4 CTRL-W +" make the window four lines higher. 132 133To set the window height to a specified number of lines: > 134 135 {height}CTRL-W _ 136 137That's: a number {height}, CTRL-W and then an underscore (the - key with Shift 138on English-US keyboards). 139 To make a window as high as it can be, use the CTRL-W _ command without a 140count. 141 142 143USING THE MOUSE 144 145In Vim you can do many things very quickly from the keyboard. Unfortunately, 146the window resizing commands require quite a bit of typing. In this case, 147using the mouse is faster. Position the mouse pointer on a status line. Now 148press the left mouse button and drag. The status line will move, thus making 149the window on one side higher and the other smaller. 150 151 152OPTIONS 153 154The '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 159when a window is closed or opened. 160 161============================================================================== 162*08.4* Vertical splits 163 164The ":split" command creates the new window above the current one. To make 165the window appear at the left side, use: > 166 167 :vsplit 168 169or: > 170 :vsplit two.c 171 172The result looks something like this: 173 174 +--------------------------------------+ 175 |/* file two.c */ |/* file one.c */ | 176 |~ |~ | 177 |~ |~ | 178 |~ |~ | 179 |two.c===============one.c=============| 180 | | 181 +--------------------------------------+ 182 183Actually, the | lines in the middle will be in reverse video. This is called 184the vertical separator. It separates the two windows left and right of it. 185 186There is also the ":vnew" command, to open a vertically split window on a new, 187empty file. Another way to do this: > 188 189 :vertical new 190 191The ":vertical" command can be inserted before another command that splits a 192window. This will cause that command to split the window vertically instead 193of horizontally. (If the command doesn't split a window, it works 194unmodified.) 195 196 197MOVING BETWEEN WINDOWS 198 199Since you can split windows horizontally and vertically as much as you like, 200you can create almost any layout of windows. Then you can use these commands 201to move between them: 202 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 207 208 CTRL-W t move to the TOP window 209 CTRL-W b move to the BOTTOM window 210 211You will notice the same letters as used for moving the cursor. And the 212cursor keys can also be used, if you like. 213 More commands to move to other windows: |Q_wi|. 214 215============================================================================== 216*08.5* Moving windows 217 218You have split a few windows, but now they are in the wrong place. Then you 219need a command to move the window somewhere else. For example, you have three 220windows like this: 221 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 +----------------------------------+ 236 237Clearly the last one should be at the top. Go to that window (using CTRL-W w) 238and the type this command: > 239 240 CTRL-W K 241 242This uses the uppercase letter K. What happens is that the window is moved to 243the 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 245top and make it occupy the full width of the Vim window. If this is your 246layout: 247 248 +-------------------------------------------+ 249 |/* two.c */ |/* three.c */ |/* one.c */ | 250 |~ |~ |~ | 251 |~ |~ |~ | 252 |~ |~ |~ | 253 |~ |~ |~ | 254 |~ |~ |~ | 255 |two.c=========three.c=========one.c========| 256 | | 257 +-------------------------------------------+ 258 259Then using CTRL-W K in the middle window (three.c) will result in: 260 261 +-------------------------------------------+ 262 |/* three.c */ | 263 |~ | 264 |~ | 265 |three.c====================================| 266 |/* two.c */ |/* one.c */ | 267 |~ |~ | 268 |two.c==================one.c===============| 269 | | 270 +-------------------------------------------+ 271 272The other three similar commands (you can probably guess these now): 273 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 277 278============================================================================== 279*08.6* Commands for all windows 280 281When you have several windows open and you want to quit Vim, you can close 282each window separately. A quicker way is using this command: > 283 284 :qall 285 286This stands for "quit all". If any of the windows contain changes, Vim will 287not exit. The cursor will automatically be positioned in a window with 288changes. You can then either use ":write" to save the changes, or ":quit!" to 289throw them away. 290 291If you know there are windows with changes, and you want to save all these 292changes, use this command: > 293 294 :wall 295 296This stands for "write all". But actually, it only writes files with 297changes. Vim knows it doesn't make sense to write files that were not 298changed. 299 And then there is the combination of ":qall" and ":wall": the "write and 300quit all" command: > 301 302 :wqall 303 304This writes all modified files and quits Vim. 305 Finally, there is a command that quits Vim and throws away all changes: > 306 307 :qall! 308 309Be careful, there is no way to undo this command! 310 311 312OPENING A WINDOW FOR ALL ARGUMENTS 313 314To make Vim open a window for each file, start it with the "-o" argument: > 315 316 vim -o one.txt two.txt three.txt 317 318This results in: 319 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 +-------------------------------+ 332 333The "-O" argument is used to get vertically split windows. 334 When Vim is already running, the ":all" command opens a window for each 335file in the argument list. ":vertical all" does it with vertical splits. 336 337============================================================================== 338*08.7* Viewing differences with vimdiff 339 340There is a special way to start Vim, which shows the differences between two 341files. Let's take a file "main.c" and insert a few characters in one line. 342Write 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): > 345 346 vimdiff main.c~ main.c 347 348Vim will start, with two windows side by side. You will only see the line 349in which you added characters, and a few lines above and below it. 350 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 +-----------------------------------------+ 369 370(This picture doesn't show the highlighting, use the vimdiff command for a 371better look.) 372 373The lines that were not modified have been collapsed into one line. This is 374called a closed fold. They are indicated in the picture with "<- fold". Thus 375the single fold line at the top stands for 123 text lines. These lines are 376equal in both files. 377 The line marked with "<- changed line" is highlighted, and the inserted 378text is displayed with another color. This clearly shows what the difference 379is between the two files. 380 The line that was deleted is displayed with "---" in the main.c window. 381See the "<- deleted line" marker in the picture. These characters are not 382really there. They just fill up main.c, so that it displays the same number 383of lines as the other window. 384 385 386THE FOLD COLUMN 387 388Each window has a column on the left with a slightly different background. In 389the picture above these are indicated with "VV". You notice there is a plus 390character there, in front of each closed fold. Move the mouse pointer to that 391plus and click the left button. The fold will open, and you can see the text 392that it contains. 393 The fold column contains a minus sign for an open fold. If you click on 394this -, 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. 397 398 399DIFFING IN VIM 400 401Another 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: > 403 404 :edit main.c 405 :vertical diffsplit main.c~ 406 407The ":vertical" command is used to make the window split vertically. If you 408omit this, you will get a horizontal split. 409 410If you have a patch or diff file, you can use the third way to start diff 411mode. First edit the file to which the patch applies. Then tell Vim the name 412of the patch file: > 413 414 :edit main.c 415 :vertical diffpatch main.c.diff 416 417WARNING: The patch file must contain only one patch, for the file you are 418editing. Otherwise you will get a lot of error messages, and some files might 419be patched unexpectedly. 420 The patching will only be done to the copy of the file in Vim. The file on 421your harddisk will remain unmodified (until you decide to write the file). 422 423 424SCROLL BINDING 425 426When the files have more changes, you can scroll in the usual way. Vim will 427try to keep both the windows start at the same position, so you can easily see 428the differences side by side. 429 When you don't want this for a moment, use this command: > 430 431 :set noscrollbind 432 433 434JUMPING TO CHANGES 435 436When you have disabled folding in some way, it may be difficult to find the 437changes. Use this command to jump forward to the next change: > 438 439 ]c 440 441To go the other way use: > 442 443 [c 444 445Prepended a count to jump further away. 446 447 448REMOVING CHANGES 449 450You can move text from one window to the other. This either removes 451differences or adds new ones. Vim doesn't keep the highlighting updated in 452all situations. To update it use this command: > 453 454 :diffupdate 455 456To remove a difference, you can move the text in a highlighted block from one 457window to another. Take the "main.c" and "main.c~" example above. Move the 458cursor to the left window, on the line that was deleted in the other window. 459Now type this command: > 460 461 dp 462 463The change will be removed by putting the text of the current window in the 464other window. "dp" stands for "diff put". 465 You can also do it the other way around. Move the cursor to the right 466window, to the line where "changed" was inserted. Now type this command: > 467 468 do 469 470The change will now be removed by getting the text from the other window. 471Since 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 473has a different meaning ("dgg" deletes from the cursor until the first line). 474 475For details about diff mode, see |vimdiff|. 476 477============================================================================== 478*08.8* Various 479 480The 'laststatus' option can be used to specify when the last window has a 481statusline: 482 483 0 never 484 1 only when there are split windows (the default) 485 2 always 486 487Many commands that edit another file have a variant that splits the window. 488For 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 490tag. 491 For Normal mode commands a CTRL-W is prepended. CTRL-^ jumps to the 492alternate file, CTRL-W CTRL-^ splits the window and edits the alternate file. 493 494The 'splitbelow' option can be set to make a new window appear below the 495current window. The 'splitright' option can be set to make a vertically split 496window appear right of the current window. 497 498When splitting a window you can prepend a modifier command to tell where the 499window is to appear: 500 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 507 508 509============================================================================== 510*08.9* Tab pages 511 512You will have noticed that windows never overlap. That means you quickly run 513out of screen space. The solution for this is called Tab pages. 514 515Assume you are editing "thisfile". To create a new tab page use this command: > 516 517 :tabedit thatfile 518 519This will edit the file "thatfile" in a window that occupies the whole Vim 520window. And you will notice a bar at the top with the two file names: 521 522 +----------------------------------+ 523 | thisfile | /thatfile/ __________X| (thatfile is bold) 524 |/* thatfile */ | 525 |that | 526 |that | 527 |~ | 528 |~ | 529 |~ | 530 | | 531 +----------------------------------+ 532 533You now have two tab pages. The first one has a window for "thisfile" and the 534second one a window for "thatfile". It's like two pages that are on top of 535eachother, with a tab sticking out of each page showing the file name. 536 537Now use the mouse to click on "thisfile" in the top line. The result is 538 539 +----------------------------------+ 540 | /thisfile/ | thatfile __________X| (thisfile is bold) 541 |/* thisfile */ | 542 |this | 543 |this | 544 |~ | 545 |~ | 546 |~ | 547 | | 548 +----------------------------------+ 549 550Thus you can switch between tab pages by clicking on the label in the top 551line. If you don't have a mouse or don't want to use it, you can use the "gt" 552command. Mnemonic: Goto Tab. 553 554Now let's create another tab page with the command: > 555 556 :tab split 557 558This makes a new tab page with one window that is editing the same buffer as 559the window we were in: 560 561 +-------------------------------------+ 562 | thisfile | /thisfile/ | thatfile __X| (thisfile is bold) 563 |/* thisfile */ | 564 |this | 565 |this | 566 |~ | 567 |~ | 568 |~ | 569 | | 570 +-------------------------------------+ 571 572You can put ":tab" before any Ex command that opens a window. The window will 573be opened in a new tab page. Another example: > 574 575 :tab help gt 576 577Will show the help text for "gt" in a new tab page. 578 579A few more things you can do with tab pages: 580 581- click with the mouse in the space after the last label 582 The next tab page will be selected, like with "gt". 583 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. 587 588- double click with the mouse in the top line 589 A new tab page will be created. 590 591- the "tabonly" command 592 Closes all tab pages except the current one. Unless there are unsaved 593 changes in other tab pages. 594 595For more information about tab pages see |tab-page|. 596 597============================================================================== 598 599Next chapter: |usr_09.txt| Using the GUI 600 601Copyright: see |manual-copyright| vim:tw=78:ts=8:ft=help:norl: 602