Learning the vi Editor

Learning the vi EditorSearch this book
Previous: 11.4 InitializationChapter 11
vim -- vi Improved
Next: 11.6 GUI Interfaces
 

11.5 Multiwindow Editing

There are a large number of vi mode commands for manipulating windows, as well as a number of ex mode commands that correspond to most of the vi mode commands.

As in elvis, the :split command will create a new window, and then you can use the ex command :e filename to edit a new file in the new window. Also as in elvis, [CTRL-W][CTRL-W] will let you switch back and forth between windows.

<preface id="VI6-CH-0">
<title>Preface </title>

<para>
Text editing is one of the most common uses of any computer system, and
<command>vi</command> is one of the most useful standard text editors
on your system.
With <command>vi</command> you can create new files, or edit any
existing UNIX text
file.
ch00.sgm                                                                        
# Makefile for vi book
#
# Arnold Robbins

CHAPTERS = ch00_6.sgm ch00_5.sgm ch00.sgm ch01.sgm ch02.sgm ch03.sgm \
	ch04.sgm ch05.sgm ch06.sgm ch07.sgm ch08.sgm
APPENDICES = appa.sgm appb.sgm appc.sgm appd.sgm

POSTSCRIPT = ch00_6.ps ch00_5.ps ch00.ps ch01.ps ch02.ps ch03.ps \
	ch04.ps ch05.ps ch06.ps ch07.ps ch08.ps \
Makefile

The split screen is the result of typing vim ch00.sgm followed by :split Makefile.

Unlike nvi and elvis, all windows share the bottom line of the screen for execution of ex commands. However, the status line for each file displays [+] if that file has been modified. Options control the use of reverse video versus highlighting for the status line, as well as whether the bottom window even has one. By default, the bottom window has a status line when there's more than one window.

Table 11.1 describes the most important vim window management commands.


Table 11.1: vim Window Management Commands
CommandFunction
[N]sp[lit] [position] [file]

Splits the current window into two. N is the height of the new window. position indicates where in the file to position the cursor. If file is given, edit it in the new window, instead of the current file.

[N]new [position] [file]

Creates a new window, editing an empty buffer. If file is given, edit it instead of the empty buffer. N and position are the same as for :split.

[N]sv[iew] [position] [file]

Same as :split, but set the readonly option for the buffer.

q[uit][!]

Quit the current window (exit if given in the last window). If this is the last window on a changed buffer, the command fails, unless the ! is given, in which case the changes are lost. When the hidden option is set, the buffer is not freed, even with !.

clo[se][!]

Close the current window. Setting the hidden option will just hide an unsaved buffer; if not set, the command fails. If supplied, the trailing ! forces the window to be closed, even if it is the last window and the buffer was modified.

hid[e]

Close the current window, if it is not the last one on the screen. The buffer becomes hidden if this was the last window open upon the buffer.

on[ly][!]

Make this window the only one on the screen. Other windows with changes are not closed, unless hidden is set or ! is used. In any case, changes are never lost; the other buffers become hidden, but they are not discarded.

res[ize] [±n]

Increase or decrease the current window height by n.

res[ize] [n]

Set the current window height to n if supplied, otherwise, set it to the largest size possible without hiding the other windows.

qa[ll][!]

Exit vim. The ! forces an exit, even if some buffers have been modified but not saved.

wqa[ll][!]

Write all changed buffers and exit. The ! forces a write of readonly buffers. vim will not exit if any buffer cannot be written.

xa[ll][!]
wa[ll][!]

Write all modified buffers that have filenames. The ! forces a write of readonly buffers.

[N]sn[ext]

Split the window and move to the next file in the argument list, or to the Nth file if a count is supplied.

sta[g] [tagname]

Split the window and then run the :tag command as appropriate in the new window.

There are many more commands for managing the argument list and the list of open buffers. For example, the :all command creates one window for each command line argument. See the vim online help for details. We have included just the most useful of the commands in the above table.

As vim has the most ex commands, so too it has the most vi mode commands, as shown in Table 11.2. As with most vi commands, you can prefix many of the windowing commands with a count.


Table 11.2: vim Window Commands from vi Mode
CommandFunction
^W s

Same as :split without a file argument. ^W ^S may not work on all terminals.

^W S
^W ^S
^W n

Same as :new without a file argument.

^W ^N
^W ^

Perform :split #, split the window and edit the alternate file. With a count, edit the nth buffer.

^W ^^
^W q

Same as the :quit command. ^W ^Q may not work on all terminals.

^W ^Q
^W c

Same as the :close command.

^W o

Like the :only command.

^W ^O
^W <DOWN>

Move cursor to nth window below the current one. n is supplied as a prefix argument.

^W ^J
^W j
^W <UP>

Move cursor to nth window above the current one. n is supplied as a prefix argument.

^W ^K
^W k
^W w

With count, go to nth window. Otherwise, move to the window below the current one. If in the bottom window, move to the top one.

^W ^W
^W W

With count, go to nth window. Otherwise, move to window above the current one. If in the top window, move to the bottom one.

^W t

Move the cursor to the top window.

^W ^T
^W b

Move the cursor to the bottom window.

^W ^B
^W p

Go to the most recently accessed (previous) window.

^W ^P
^W r

Rotate all the windows downwards. The cursor stays in the same window.

^W ^R
^W R

Rotate all the windows upwards. The cursor stays in the same window.

^W x

Without count, exchange the current window with the next one. If there is no next window, exchange with the previous window. With count, exchange the current window with the nth window (first window is 1). The cursor is put in the other window.

^W ^X
^W =

Make all windows the same height.

^W -

Decrease current window height. A preceding count indicates by how much.

^W +

Increase current window height. A preceding count indicates by how much.

^W _

Set the current window size to the value given in a preceding count. This is like :resize with an absolute count. Without a count, the window is made as large as possible.

^W ^_
zN[RETURN]

Set the current window height to N.

^W ]

Split the current window. In the new upper window, use the identifier under the cursor as a tag and go to it. A preceding count indicates the new window's height.

^W ^]
^W f

Split the current window, and edit the file name under the cursor in the new window. Rather complicated file searching is done for this command, see :help ^W_f for details.

^W ^F
^W i

Open a new window and in it, move the cursor to the first line that matches the keyword under the cursor. The search starts at the beginning of the file, and lines that look like comments are ignored. With a preceding count, go to the nth matching line, and don't ignore comments.

^W ^I
^W d

Open a new window, with the cursor on the first macro definition line that contains the keyword under the cursor. The search starts from the beginning of the file. If a count is given, the countth matching line is jumped to.

^W ^D

Miscellaneous remarks: The command ^W ^C does not close the current window, since usually ^C is the interrupt character, which ends up cancelling the command. If mouse support is enabled and you are using the GUI version of vim, you can resize a window by dragging on its status line with the mouse. Finally, vim has many options that control the behavior of the various commands. In particular, check out the hidden, splitbelow, equalalways, winheight, and cmdheight options. See the online help for full details.


Previous: 11.4 InitializationLearning the vi EditorNext: 11.6 GUI Interfaces
11.4 InitializationBook Index11.6 GUI Interfaces

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System