Project34.Use vim Windows


Project 34. Use vim Windows

"How do I edit multiple files in one vim session?"

This project introduces windows and buffers, and shows you how to edit many files is a single vim session. Project 32 introduces vim, and Projects 33 and 35 cover advanced vim features and configuration.

Edit Multiple Files

You may list any number of files on the vim command line. vim reads all the files, loading each in its own buffer and presenting the first-named file in the main window. You might edit all HTML files in the current directory, for example, by typing

$ vim *.html


When you finish editing the first file, save the edits and move to the next file by typing

:wn


Omit the w if you've already written the changes back to the first file. To discard changes to the current file and move to the next, type

:n!


To move back to a previous file, type

:wprev


Other methods of moving between buffers (open files) include

  • :ls! to list all the buffers. Each buffer is numbered.

  • :bn to move to buffer number n.

  • n Control-^ also moves to buffer number n.

To quit vim before all buffers have been visited, type

:qa


To also discard all changes not currently saved, type

:qa!


Read Files

From within vim, you may load additional files for editing by typing

  • :e filename to read a file into a new buffer

  • :r filename to include a file within the text of the current file, just below the cursor

Tip

vim supports tabbed completion when you enter a command or filename on its command line. Simply press Tab, and the command or filename is completed. If what you have typed so far is ambiguous, either type some more or continue pressing Tab, and vim will cycle through all possible completions.


Browse Directories

You need not type the full pathname of a file you wish to load. Instead, browse the current directory by typing

:edit.


This tells vim to open a new window, from which you choose a file to edit. Use the cursor keys to select a file, and press Return. If you choose a directory, vim will list all the files in that directory. To move to the parent directory choose the .. (dot-dot) entry.

Open Many Windows

To view several files at once, you could start several Terminal sessions and invoke vim in each of them. It's not necessary to do this, however. Sometimes, it's easier to use vim's multi-window feature, whereby the main window is split into two or more subwindows. You saw this in action before, when you typed :help. Recall that vim split the current window and loaded the help pages into the top split.

In vim terminology, a window is a portion of the Terminal screen where a file is displayed. A buffer, you'll recall, is a copy of a file being edited. There's no hard limit to the number of windows and buffers you can use at any time.

Split the Screen

To open a new window, thus splitting the Terminal screen into two parts, type

:split


The cursor is placed in the new window (the top split), as shown in Figure 4.4. You'll notice that each window now sports a status bar showing a filename. Both windows are editing the same file, enabling you to view one part of the file while editing another. If you wish to edit a different file in the new window, either use :e as described above or give a filename argument to split.

Figure 4.4. There's no limit to the number of windows vim can keep open at any time, and its split-window option is great for comparing files.


:split filename


To flip between two (or more) windows, type one of the following:

  • Control-w Control-w to rotate through the windows

  • Control-w j to move to the bottom window

  • Control-w k to move to the top window

vim does not limit you to two windows. You may split any number of times to create more windows.

Close a window by typing

:close


The buffer that was displayed in the window is not lost; you can view it again by loading it into this or another window by using the techniques discussed in "Edit Multiple Files" earlier in this project.

To open a new, blank window, type

:new


Tip

Type :only to close all but the active window. Other windows must not contain unsaved modifications. Type :only! to override this. The other buffers are still loaded but not displayed in any window.


Change Window Sizes

When you open a new window, vim adjusts the new and existing windows to be of equal size. To override this feature, you may specify a size in lines for the new window. To create a 20-line window, for example, type

:20 split new-filename


It's also possible to adjust the size of existing windows up or down by typing, for example, 7 Control-w + to increase the window size by seven lines and 3 Control-w (that's a minus sign) to decrease the window size by three lines.

Tip

If you use X11 and xterm, enable the mouse in vim by typing

:set mouse=a


Now you are able to change windows by clicking in the desired window and to adjust the size of windows by dragging the status bar. Very GUI-like!


Compare Files

If you wish to compare two files side by side, create a vertical split by typing

:vsplit file2


Alternatively, get vim to do the all the hard work of comparing files and marking the differences. Type either of the following:

  • :diffsplit file2 to compare files one of top of the other

  • :vert diffsplit file2 to compare files vertically, side by side

A similar effect can be achieved by invoking vim as vimdiff and giving it the names of two files to compare.

$ vimdiff file1 file2


More

There's a lot more to windows in vim. Type

:help windows


for a detailed description of all the available features and commands.




Mac OS X UNIX 101 Byte-Sized Projects
Mac OS X Unix 101 Byte-Sized Projects
ISBN: 0321374118
EAN: 2147483647
Year: 2003
Pages: 153
Authors: Adrian Mayo

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net