Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1 | *usr_08.txt* For Vim version 7.0aa. Last change: 2005 Apr 01 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2 | |
| 3 | VIM USER MANUAL - by Bram Moolenaar |
| 4 | |
| 5 | Splitting windows |
| 6 | |
| 7 | |
| 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. |
| 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 |
| 23 | Table of contents: |usr_toc.txt| |
| 24 | |
| 25 | ============================================================================== |
| 26 | *08.1* Split a window |
| 27 | |
| 28 | The easiest way to open a new window is to use the following command: > |
| 29 | |
| 30 | :split |
| 31 | |
| 32 | This command splits the screen into two windows and leaves the cursor in the |
| 33 | top one: |
| 34 | |
| 35 | +----------------------------------+ |
| 36 | |/* file one.c */ | |
| 37 | |~ | |
| 38 | |~ | |
| 39 | |one.c=============================| |
| 40 | |/* file one.c */ | |
| 41 | |~ | |
| 42 | |one.c=============================| |
| 43 | | | |
| 44 | +----------------------------------+ |
| 45 | |
| 46 | What you see here is two windows on the same file. The line with "====" is |
| 47 | that status line. It displays information about the window above it. (In |
| 48 | practice the status line will be in reverse video.) |
| 49 | The two windows allow you to view two parts of the same file. For example, |
| 50 | you could make the top window show the variable declarations of a program, and |
| 51 | the bottom one the code that uses these variables. |
| 52 | |
| 53 | The CTRL-W w command can be used to jump between the windows. If you are in |
| 54 | the top window, CTRL-W w jumps to the window below it. If you are in the |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 55 | bottom window it will jump to the first window. (CTRL-W CTRL-W does the same |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 56 | thing, in case you let go of the CTRL key a bit later.) |
| 57 | |
| 58 | |
| 59 | CLOSE THE WINDOW |
| 60 | |
| 61 | To close a window, use the command: > |
| 62 | |
| 63 | :close |
| 64 | |
| 65 | Actually, any command that quits editing a file works, like ":quit" and "ZZ". |
| 66 | But ":close" prevents you from accidentally exiting Vim when you close the |
| 67 | last window. |
| 68 | |
| 69 | |
| 70 | CLOSING ALL OTHER WINDOWS |
| 71 | |
| 72 | If you have opened a whole bunch of windows, but now want to concentrate on |
| 73 | one of them, this command will be useful: > |
| 74 | |
| 75 | :only |
| 76 | |
| 77 | This closes all windows, except for the current one. If any of the other |
| 78 | windows has changes, you will get an error message and that window won't be |
| 79 | closed. |
| 80 | |
| 81 | ============================================================================== |
| 82 | *08.2* Split a window on another file |
| 83 | |
| 84 | The following command opens a second window and starts editing the given file: |
| 85 | > |
| 86 | :split two.c |
| 87 | |
| 88 | If 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 | |
| 101 | To open a window on a new, empty file, use this: > |
| 102 | |
| 103 | :new |
| 104 | |
| 105 | You can repeat the ":split" and ":new" commands to create as many windows as |
| 106 | you like. |
| 107 | |
| 108 | ============================================================================== |
| 109 | *08.3* Window size |
| 110 | |
| 111 | The ":split" command can take a number argument. If specified, this will be |
| 112 | the height of the new window. For example, the following opens a new window |
| 113 | three lines high and starts editing the file alpha.c: > |
| 114 | |
| 115 | :3split alpha.c |
| 116 | |
| 117 | For existing windows you can change the size in several ways. When you have a |
| 118 | working mouse, it is easy: Move the mouse pointer to the status line that |
| 119 | separates two windows, and drag it up or down. |
| 120 | |
| 121 | To increase the size of a window: > |
| 122 | |
| 123 | CTRL-W + |
| 124 | |
| 125 | To decrease it: > |
| 126 | |
| 127 | CTRL-W - |
| 128 | |
| 129 | Both of these commands take a count and increase or decrease the window size |
| 130 | by that many lines. Thus "4 CTRL-W +" make the window four lines higher. |
| 131 | |
| 132 | To set the window height to a specified number of lines: > |
| 133 | |
| 134 | {height}CTRL-W _ |
| 135 | |
| 136 | That's: a number {height}, CTRL-W and then an underscore (the - key with Shift |
| 137 | on English-US keyboards). |
| 138 | To make a window as high as it can be, use the CTRL-W _ command without a |
| 139 | count. |
| 140 | |
| 141 | |
| 142 | USING THE MOUSE |
| 143 | |
| 144 | In Vim you can do many things very quickly from the keyboard. Unfortunately, |
| 145 | the window resizing commands require quite a bit of typing. In this case, |
| 146 | using the mouse is faster. Position the mouse pointer on a status line. Now |
| 147 | press the left mouse button and drag. The status line will move, thus making |
| 148 | the window on one side higher and the other smaller. |
| 149 | |
| 150 | |
| 151 | OPTIONS |
| 152 | |
| 153 | The '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 |
| 158 | when a window is closed or opened. |
| 159 | |
| 160 | ============================================================================== |
| 161 | *08.4* Vertical splits |
| 162 | |
| 163 | The ":split" command creates the new window above the current one. To make |
| 164 | the window appear at the left side, use: > |
| 165 | |
| 166 | :vsplit |
| 167 | |
| 168 | or: > |
| 169 | :vsplit two.c |
| 170 | |
| 171 | The 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 | |
| 182 | Actually, the | lines in the middle will be in reverse video. This is called |
| 183 | the vertical separator. It separates the two windows left and right of it. |
| 184 | |
| 185 | There is also the ":vnew" command, to open a vertically split window on a new, |
| 186 | empty file. Another way to do this: > |
| 187 | |
| 188 | :vertical new |
| 189 | |
| 190 | The ":vertical" command can be inserted before another command that splits a |
| 191 | window. This will cause that command to split the window vertically instead |
| 192 | of horizontally. (If the command doesn't split a window, it works |
| 193 | unmodified.) |
| 194 | |
| 195 | |
| 196 | MOVING BETWEEN WINDOWS |
| 197 | |
| 198 | Since you can split windows horizontally and vertically as much as you like, |
| 199 | you can create any layout of windows. Then you can use these commands to move |
| 200 | between 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 | |
| 210 | You will notice the same letters as used for moving the cursor. And the |
| 211 | cursor 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 | |
| 217 | You have split a few windows, but now they are in the wrong place. Then you |
| 218 | need a command to move the window somewhere else. For example, you have three |
| 219 | windows 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 | |
| 236 | Clearly the last one should be at the top. Go to that window (using CTRL-W w) |
| 237 | and the type this command: > |
| 238 | |
| 239 | CTRL-W K |
| 240 | |
| 241 | This uses the uppercase letter K. What happens is that the window is moved to |
| 242 | the 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 |
| 244 | top and make it occupy the full with of the Vim window. If this is your |
| 245 | layout: |
| 246 | |
| 247 | +-------------------------------------------+ |
| 248 | |/* two.c */ |/* three.c */ |/* one.c */ | |
| 249 | |~ |~ |~ | |
| 250 | |~ |~ |~ | |
| 251 | |~ |~ |~ | |
| 252 | |~ |~ |~ | |
| 253 | |~ |~ |~ | |
| 254 | |two.c=========three.c=========one.c========| |
| 255 | | | |
| 256 | +-------------------------------------------+ |
| 257 | |
| 258 | Then 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 | |
| 271 | The 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 | |
| 280 | When you have several windows open and you want to quit Vim, you can close |
| 281 | each window separately. A quicker way is using this command: > |
| 282 | |
| 283 | :qall |
| 284 | |
| 285 | This stands for "quit all". If any of the windows contain changes, Vim will |
| 286 | not exit. The cursor will automatically be positioned in a window with |
| 287 | changes. You can then either use ":write" to save the changes, or ":quit!" to |
| 288 | throw them away. |
| 289 | |
| 290 | If you know there are windows with changes, and you want to save all these |
| 291 | changes, use this command: > |
| 292 | |
| 293 | :wall |
| 294 | |
| 295 | This stands for "write all". But actually, it only writes files with |
| 296 | changes. Vim knows it doesn't make sense to write files that were not |
| 297 | changed. |
| 298 | And then there is the combination of ":qall" and ":wall": the "write and |
| 299 | quit all" command: > |
| 300 | |
| 301 | :wqall |
| 302 | |
| 303 | This 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 | |
| 308 | Be careful, there is no way to undo this command! |
| 309 | |
| 310 | |
| 311 | OPENING A WINDOW FOR ALL ARGUMENTS |
| 312 | |
| 313 | To 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 | |
| 317 | This 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 | |
| 332 | The "-O" argument is used to get vertically split windows. |
| 333 | When Vim is already running, the ":all" command opens a window for each |
| 334 | file in the argument list. ":vertical all" does it with vertical splits. |
| 335 | |
| 336 | ============================================================================== |
| 337 | *08.7* Viewing differences with vimdiff |
| 338 | |
| 339 | There is a special way to start Vim, which shows the differences between two |
| 340 | files. Let's take a file "main.c" and insert a few characters in one line. |
| 341 | Write 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 | |
| 347 | Vim will start, with two windows side by side. You will only see the line |
| 348 | in 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 |
| 370 | better look.) |
| 371 | |
| 372 | The lines that were not modified have been collapsed into one line. This is |
| 373 | called a closed fold. They are indicated in the picture with "<- fold". Thus |
| 374 | the single fold line at the top stands for 123 text lines. These lines are |
| 375 | equal in both files. |
| 376 | The line marked with "<- changed line" is highlighted, and the inserted |
| 377 | text is displayed with another color. This clearly shows what the difference |
| 378 | is between the two files. |
| 379 | The line that was deleted is displayed with "---" in the main.c window. |
| 380 | See the "<- deleted line" marker in the picture. These characters are not |
| 381 | really there. They just fill up main.c, so that it displays the same number |
| 382 | of lines as the other window. |
| 383 | |
| 384 | |
| 385 | THE FOLD COLUMN |
| 386 | |
| 387 | Each window has a column on the left with a slightly different background. In |
| 388 | the picture above these are indicated with "VV". You notice there is a plus |
| 389 | character there, in front of each closed fold. Move the mouse pointer to that |
| 390 | plus and click the left button. The fold will open, and you can see the text |
| 391 | that it contains. |
| 392 | The fold column contains a minus sign for an open fold. If you click on |
| 393 | this -, 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 | |
| 398 | DIFFING IN VIM |
| 399 | |
| 400 | Another 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 | |
| 406 | The ":vertical" command is used to make the window split vertically. If you |
| 407 | omit this, you will get a horizontal split. |
| 408 | |
| 409 | If you have a patch or diff file, you can use the third way to start diff |
| 410 | mode. First edit the file to which the patch applies. Then tell Vim the name |
| 411 | of the patch file: > |
| 412 | |
| 413 | :edit main.c |
| 414 | :vertical diffpatch main.c.diff |
| 415 | |
| 416 | WARNING: The patch file must contain only one patch, for the file you are |
| 417 | editing. Otherwise you will get a lot of error messages, and some files might |
| 418 | be patched unexpectedly. |
| 419 | The patching will only be done to the copy of the file in Vim. The file on |
| 420 | your harddisk will remain unmodified (until you decide to write the file). |
| 421 | |
| 422 | |
| 423 | SCROLL BINDING |
| 424 | |
| 425 | When the files have more changes, you can scroll in the usual way. Vim will |
| 426 | try to keep both the windows start at the same position, so you can easily see |
| 427 | the differences side by side. |
| 428 | When you don't want this for a moment, use this command: > |
| 429 | |
| 430 | :set noscrollbind |
| 431 | |
| 432 | |
| 433 | JUMPING TO CHANGES |
| 434 | |
| 435 | When you have disabled folding in some way, it may be difficult to find the |
| 436 | changes. Use this command to jump forward to the next change: > |
| 437 | |
| 438 | ]c |
| 439 | |
| 440 | To go the other way use: > |
| 441 | |
| 442 | [c |
| 443 | |
| 444 | Prepended a count to jump further away. |
| 445 | |
| 446 | |
| 447 | REMOVING CHANGES |
| 448 | |
| 449 | You can move text from one window to the other. This either removes |
| 450 | differences or adds new ones. Vim doesn't keep the highlighting updated in |
| 451 | all situations. To update it use this command: > |
| 452 | |
| 453 | :diffupdate |
| 454 | |
| 455 | To remove a difference, you can move the text in a highlighted block from one |
| 456 | window to another. Take the "main.c" and "main.c~" example above. Move the |
| 457 | cursor to the left window, on the line that was deleted in the other window. |
| 458 | Now type this command: > |
| 459 | |
| 460 | dp |
| 461 | |
| 462 | The change will be removed by putting the text of the current window in the |
| 463 | other window. "dp" stands for "diff put". |
| 464 | You can also do it the other way around. Move the cursor to the right |
| 465 | window, to the line where "changed" was inserted. Now type this command: > |
| 466 | |
| 467 | do |
| 468 | |
| 469 | The change will now be removed by getting the text from the other window. |
| 470 | Since 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 |
| 472 | has a different meaning ("dgg" deletes from the cursor until the first line). |
| 473 | |
| 474 | For details about diff mode, see |vimdiff|. |
| 475 | |
| 476 | ============================================================================== |
| 477 | *08.8* Various |
| 478 | |
| 479 | The 'laststatus' option can be used to specify when the last window has a |
| 480 | statusline: |
| 481 | |
| 482 | 0 never |
| 483 | 1 only when there are split windows (the default) |
| 484 | 2 always |
| 485 | |
| 486 | Many commands that edit another file have a variant that splits the window. |
| 487 | For 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 |
| 489 | tag. |
| 490 | For Normal mode commands a CTRL-W is prepended. CTRL-^ jumps to the |
| 491 | alternate file, CTRL-W CTRL-^ splits the window and edits the alternate file. |
| 492 | |
| 493 | The 'splitbelow' option can be set to make a new window appear below the |
| 494 | current window. The 'splitright' option can be set to make a vertically split |
| 495 | window appear right of the current window. |
| 496 | |
| 497 | When splitting a window you can prepend a modifier command to tell where the |
| 498 | window 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 | |
| 509 | Next chapter: |usr_09.txt| Using the GUI |
| 510 | |
| 511 | Copyright: see |manual-copyright| vim:tw=78:ts=8:ft=help:norl: |