Softpanorama

May the source be with you, but remember the KISS principle ;-)
Home Switchboard Unix Administration Red Hat TCP/IP Networks Neoliberalism Toxic Managers
(slightly skeptical) Educational society promoting "Back to basics" movement against IT overcomplexity and  bastardization of classic Unix

VIM Multiwindows Support

Old News ;-) See Also Recommended Links Reference .vimrc History Humor Etc

 

Using multiple windows

If you want, you can probably do everything from one vim session! :) Here are some commands to turn one vim session (inside one xterm) into multiple windows.

 :e filename      - edit another file
 
 ctrl-w up arrow  - move cursor up a window
 ctrl-w ctrl-w    - move cursor to another window (cycle)
 ctrl-w_          - maximize current window
 ctrl-w=          - make all equal size
 10 ctrl-w+       - increase window size by 10 lines

 :b 2             - open buffer #2 in this window

Window Management

Splitting horizontal

:split  -- two view on the same file
:split filename  - split window and load another file 
:vsplit file     - vertical split
 :sview file      - same as split, but readonly
 :hide            - close current window
 :only            - keep only this window open
 :ls              - show current buffers

Switching from one to another Ctrl-w,w

Vertical/Horizontal: ctrl-w,v and ctrl-w,h
closeing Ctrl-w,q

Zooming
Open Full Height/Width
ctrl-w,| and ctrl-w,_

Balancing to equal high of wodth
ctrl-w,=

Navigating
ctrl-w, then:

Vim documentation windows

A window is a viewport onto a buffer. You can use multiple windows on one buffer, or several windows on different buffers.

A buffer is a file loaded into memory for editing. The original file remains unchanged until you write the buffer to the file.

A buffer can be in one of three states:

  1. active: The buffer is displayed in a window. If there is a file for this 
      buffer, it has been read into the buffer. The buffer may have been modified.
      
  2. hidden: The buffer is not displayed. If there is a file for this buffer, 
      it has been read into the buffer. The buffer may have been modified. 
  3. inactive: The buffer is not displayed and does not contain anything. 
      Options for the buffer are remembered if the file was once loaded. In a table:
      
state		displayed	loaded		":buffers"  
		in window			shows	    
active		  yes		 yes		  '' ''
hidden		  no		 yes		  'h'
inactive	  no		 no		  '-'

By default, Vim starts with one window, just like Vi. The "-o" argument to Vim can be used to open a window for each file in the argument list: "Vim -o file1 file2 file3" will open three windows.  

"-oN", where N is a decimal number, opens N windows. If there are more file names than windows, only N windows are opened and some files do not get a window. If there are more windows than file names, the last few windows will be editing empty buffers.

If there are many file names, the windows will become very small. You might want to set the 'winheight' option to create a workable situation.

Buf/Win Enter/Leave autocommands are not executed when opening the new windows and reading the files, that's only done when they are really entered.

A status line will be used to separate windows. The 'laststatus' option tells when the last window also has a status line:

You can change the contents of the status line with the 'statusline' option.

Normally, inversion is used to display the status line. This can be changed with the 's' character in the 'highlight' option. For example, "sb" sets it to bold characters. If no highlighting is used for the status line ("sn"), the '^' character is used for the current window, and '=' for other windows. If the mouse is supported and enabled with the 'mouse' option, a status line can be dragged to resize windows.

Note: If you expect your status line to be in reverse video and it isn't, check if the 'highlight' option contains "si". In version 3.0, this meant to invert the status line. Now it should be "sr", reverse the status line, as "si" now stands for italic! If italic is not available on your terminal, the status line is inverted anyway; you will only see this problem on terminals that have termcap codes for italics.

Split window

You can view the same file in multiple windows using split command:

To move between the panes you must press Ctrl+W twice. If you have one file loaded into vim and want to edit a second file in a separate window, you can split the screen horizontally with the other file loaded in the second pane:

:split file2

To set a specific height of the newt window use numberic prefix before the split command:

:10split /etc/fstab

If you wanted to close the split window, you would focus on it by pressing Ctrl+W and then entering

:close

Better yet, after comparing something or getting a filename straight, you can close all the other split windows and just have the current window open by entering the following:

:only

Reference for split command

CTRL-W s						

CTRL-W S					

CTRL-W CTRL-S						

:[N]sp[lit] [+cmd]					
		Split current window in two.  The result is two viewports on
		the same file.  Make new window N high (default is to use half
		the height of the current window).  Reduces the current window
		height to create room (and others, if the 'equalalways' option
		is set).  (Note: CTRL-S does not work on all terminals).  Also
		see |+cmd|.

CTRL-W n					

CTRL-W CTRL_N						

:[N]new [+cmd]						
		Create a new window and start editing an empty file in it.
		Make new window N high (default is to use half the existing
		height).  Reduces the current window height to create room (and
		others, if the 'equalalways' option is set).  Also see
		|+cmd|.  If 'fileformats' is not empty, the first format given
		will be used for the new buffer.  If 'fileformats' is empty,
		the 'fileformat' of the current buffer is used.
		Autocommands are executed in this order:
		1. WinLeave for the current window
		2. WinEnter for the new window
		3. BufLeave for the current buffer
		4. BufEnter for the new buffer
		This behaves like a ":split" first, and then a ":e" command.

:[N]new [+cmd] {file}

:[N]sp[lit] [+cmd] {file}				
		Create a new window and start editing file {file} in it.  If
		[+cmd] is given, execute the command when the file has been
		loaded |+cmd|.  Make new window N high (default is to use half
		the existing height).  Reduces the current window height to
		create room (and others, if the 'equalalways' option is set).


:[N]sv[iew] [+cmd] {file}			*:sv* *:sview* *splitview*
		Same as ":split", but set 'readonly' option for this buffer.


:[N]sf[ind] [+cmd] {file}			
		Same as ":split", but search for {file} in 'path'.  Doesn't
		split if {file} is not found.


CTRL-W CTRL-^					
CTRL-W ^	Does ":split #", split window in two and edit alternate file.
		When a count is given, it becomes ":split #N", split window
		and edit buffer N.

Closing a window

CTRL-W q						

CTRL-W CTRL-Q						
:q[uit]		Quit current window.  When quitting the last window (not
		counting a help window), exit Vim.
		When 'hidden' is set, and there is only one window for the
		current buffer, it becomes hidden.
		When 'hidden' is not set, and there is only one window for the
		current buffer, and the buffer was changed, the command fails.
		(Note: CTRL-Q does not work on all terminals)

:q[uit]!	Quit current window.  If this was the last window for a buffer,
		any changes to that buffer are lost.  When quitting the last
		window (not counting help windows), exit Vim.  The contents of
		the buffer are lost, even when 'hidden' is set.


CTRL-W c					
:clo[se][!]	Close current window.  When the 'hidden' option is set, or
		when the buffer was changed and the [!] is used, the buffer
		becomes hidden (unless there is another window editing it).
		This command fails when:
		- There is only one window on the screen.
		- When 'hidden' is not set, [!] is not used, the buffer has
		  changes, and there is no other window on this buffer.
		Changes to the buffer are not written and won't get lost, so
		this is a "safe" command.


CTRL-W CTRL-C						
		You might have expected that CTRL-W CTRL-C closes the current
		window, but that does not work, because the CTRL-C cancels the
		command.


							
:hid[e]		Quit current window, unless it is the last window on the
		screen.  The buffer becomes hidden (unless there is another
		window editing it).
		The value of 'hidden' is irrelevant for this command.
		Changes to the buffer are not written and won't get lost, so
		this is a "safe" command.


CTRL-W o						

CTRL-W CTRL-O					
:on[ly][!]	Make the current window the only one on the screen.  All other
		windows are closed.
		When the 'hidden' option is set, all buffers in closed windows
		become hidden.
		When 'hidden' is not set, and the 'autowrite' option is set,
		modified buffers are written.  Otherwise, windows that have
		buffers that are modified are not removed, unless the [!] is
		given, then they become hidden.  But modified buffers are
		never abandoned, so changes cannot get lost.

Moving cursor to other windows

CTRL-W <Down>					

CTRL-W CTRL-J					
CTRL-W j	move cursor to Nth window below current one.


CTRL-W <Up>					

CTRL-W CTRL-K					
CTRL-W k	move cursor to Nth window above current one.


CTRL-W w					
CTRL-W CTRL-W	Without count: move cursor to window below current one.  If
		there is no window below, go to top window.
		With count: go to Nth window.


						
CTRL-W W	Without count: move cursor to window above current one.  If
		there is no window above, go to bottom window.
		With count: go to Nth window.


CTRL-W t					
CTRL-W CTRL-T	move cursor to top window.


CTRL-W b					
CTRL-W CTRL-B	move cursor to bottom window.


CTRL-W p					
CTRL-W CTRL-P	go to previous (last accessed) window.

If Visual mode is active and the new window is not for the same buffer, the
Visual mode is ended.



Moving windows around				


CTRL-W r					
CTRL-W CTRL-R	Rotate windows downwards.  The first window becomes the second
		one, the second one becomes the third one, etc.  The last
		window becomes the first window.  The cursor remains in the
		same window.
						
CTRL-W R	Rotate windows upwards.  The second window becomes the first
		one, the third one becomes the second one, etc.  The first
		window becomes the last window.  The cursor remains in the
		same window.

CTRL-W x					
CTRL-W CTRL-X	Without count: Exchange current window with next one.  If there
		is no next window, exchange with previous window.
		With count: Exchange current window with Nth window (first
		window is 1).  The cursor is put in the other window.

Window resizing

CTRL-W =	make all windows (almost) equally high.

:res[ize] -N					
CTRL-W -	decrease current window height by N

:res[ize] +N					
CTRL-W +	increase current window height by N
:res[ize] [N]

CTRL-W CTRL-_					
CTRL-W _	set current window height to N (default: highest possible)

z{nr}<CR>	set current window height to {nr}

You can also resize the window by dragging a status line up or down with the
mouse.  This only works if the version of Vim that is being used supports the
mouse and the 'mouse' option has been set to enable it.

The option 'winheight' ('wh') is used to set the minimal window height of the
current window.  This option is used each time another window becomes the
current window.  If the option is '0', it is disabled.  Set 'winheight' to a
very large value, e.g., '9999', to make the current window always fill all
available space.  Set it to a reasonable value, e.g., '10', to make editing in
the current window comfortable.

When the option 'equalalways' ('ea') is set, all the windows are automatically
made the same size after splitting or closing a window.  If you don't set this
option, splitting a window will reduce the size of the current window and
leave the other windows the same.  When closing a window, the extra lines are
given to the window above it.

The option 'cmdheight' ('ch') is used to set the height of the command-line.
If you are annoyed by the |hit-return| prompt for long messages, set this
option to 2 or 3.

If there is only one window, resizing that window will also change the command
line height.  If there are several windows, resizing the current window will
also change the height of the window below it (and sometimes the window above
it).



Exiting Vim						


							
:qa[ll]		Exit Vim, unless there are some buffers which have been
		changed.  (Use ":bmod" to go to the next modified buffer).

:conf[irm] qa[ll]
		Exit Vim.  Bring up a prompt when some buffers have been
		changed.  See |:confirm|.

:qa[ll]!	Exit Vim.  Any changes to buffers are lost.


:wqa[ll]					
:xa[ll]		Write all changed buffers and exit Vim.  If there are buffers
		without a file name, which are readonly or which cannot be
		written for another reason, Vim is not quit.

:conf[irm] wqa[ll]
:conf[irm] xa[ll]
		Write all changed buffers and exit Vim.  Bring up a prompt
		when some buffers are readonly or cannot be written for
		another reason.  See |:confirm|.

:wqa[ll]!
:xa[ll]!	Write all changed buffers, even the ones that are readonly,
		and exit Vim.  If there are buffers without a file name or
		which cannot be written for another reason, Vim is not quit.


Writing with multiple buffers			


						
:wa[ll]		Write all changed buffers.  Buffers without a file name or
		which are readonly are not written.

:wa[ll]!	Write all changed buffers, even the ones that are readonly.
		Buffers without a file name are not written.



argument and buffer list commands			

      args list		       buffer list	   meaning 
1. :[N]argument [N]	11. :[N]buffer [N]	to arg/buf N
2. :[N]next [file ..]	12. :[N]bnext [N]	to Nth next arg/buf
3. :[N]Next [N]		13. :[N]bNext [N]	to Nth previous arg/buf
4. :[N]previous	[N]	14. :[N]bprevious [N]	to Nth previous arg/buf
5. :rewind		15. :brewind		to first arg/buf
6. :last		16. :blast		to last arg/buf
7. :all			17. :ball		edit all args/buffers
			18. :unhide		edit all loaded buffers
			19. :[N]bmod [N]	to Nth modified buf

  split & args list	  split & buffer list	   meaning 
21. :[N]sargument [N]   31. :[N]sbuffer [N]	split + to arg/buf N
22. :[N]snext [file ..] 32. :[N]sbnext [N]      split + to Nth next arg/buf
23. :[N]sNext [N]       33. :[N]sbNext [N]      split + to Nth previous arg/buf
24. :[N]sprevious [N]   34. :[N]sbprevious [N]  split + to Nth previous arg/buf
25. :srewind		35. :sbrewind		split + to first arg/buf
26. :slast		36. :sblast		split + to last arg/buf
27. :sall		37: :sball		edit all args/buffers
			38. :sunhide		edit all loaded buffers
			39. :[N]sbmod [N]	split + to Nth modified buf

40. :args		list of arguments
41. :buffers		list of buffers

The meaning of [N] depends on the command:
 [N] is number of buffers to go forward/backward on ?2, ?3, and ?4
 [N] is an argument number, defaulting to current argument, for 1 and 21
 [N] is a buffer number, defaulting to current buffer, for 11 and 31
 [N] is a count for 19 and 39

Note: ":next" is an exception, because it must accept a list of file names
for compatibility with Vi.


The argument list and multiple windows

The current position in the argument list can be different for each window.
Remember that when doing ":e file", the position in the argument list stays
the same, but you are not editing the file at that position.  To indicate
this, the file message (and the title, if you have one) shows
"(file (N) of M)", where "(N)" is the current position in the file list, and
"M" the number of files in the file list.

All the entries in the argument list are added to the buffer list.  Thus, you
can also get to them with the buffer list commands, like ":bnext".


:[N]al[l][!] [N]			
:[N]sal[l][!] [N]
		Rearrange the screen to open one window for each argument.
		All other windows are closed.  When a count is given, this is
		the maximum number of windows to open.
		When the 'hidden' option is set, all buffers in closed windows
		become hidden.
		When 'hidden' is not set, and the 'autowrite' option is set,
		modified buffers are written.  Otherwise, windows that have
		buffers that are modified are not removed, unless the [!] is
		given, then they become hidden.  But modified buffers are
		never abandoned, so changes cannot get lost.
		Buf/Win Enter/Leave autocommands are not executed for the new
		windows here, that's only done when they are really entered.


:[N]sa[rgument][!] [N]				
		Short for ":split | argument [N]": split window and go to Nth
		argument.  But when there is no such argument, the window is
		not split.


:[N]sn[ext][!] [file ..]				
		Short for ":split | [N]next": split window and go to Nth next
		argument.  But when there is no next file, the window is not
		split.


:[N]spr[evious][!] [N]				

:[N]sN[ext][!] [N]				
		Short for ":split | [N]Next": split window and go to Nth
		previous argument.  But when there is no previous file, the
		window is not split.

						
:sr[ewind][!]	Short for ":split | rewind": split window and go to first
		argument.  But when there is no argument list, the window is
		not split.

						
:sla[st][!]	Short for ":split | last": split window and go to last
		argument.  But when there is no argument list, the window is
		not split.



Tag or file name under the cursor			


						
:sta[g][!] [tagname]
		Does ":tag[!] [tagname]" and splits the window for the found
		tag.  See also |:tag|.


CTRL-W ]					
CTRL-W CTRL-]	Split current window in two.  Use identifier under cursor as a
		tag and jump to it in the new upper window.  Make new window N
		high.


							
CTRL-W g ]	Split current window in two.  Use identifier under cursor as a
		tag and perform ":tselect" on it in the new upper window.
		Make new window N high.


							
CTRL-W g CTRL-]	Split current window in two.  Use identifier under cursor as a
		tag and perform ":tjump" on it in the new upper window.  Make
		new window N high.


						
:pta[g][!] [tagname]
		Does ":tag[!] [tagname]" and shows the found tag in a
		"Preview" window without changing the current buffer or cursor
		position. If a "Preview" window already exists, it is re-used
		(like a help window is).  If a new one is opened,
		'previewheight' is used for the height of the window.   See
		also |:tag|.

		Example:				
   au! CursorHold *.[ch] nested exe "ptag " . expand("<cword>")
		This will cause a ":ptag" to be executed for the keyword under
		the cursor, when the cursor hasn't moved for the time set with
		'updatetime'.  The "nested" makes other autocommands be
		executed, so that syntax highlighting works in the preview
		window.  Also see |CursorHold|.  Note: this isn't perfect, you
		will get error messages when the cursor rests on a word that
		isn't a tag.


CTRL-W z					*CTRL-W_z*

CTRL-W CTRL-Z					*CTRL-W_CTRL-Z* *:pc* *:pclose*
:pc[lose][!]	Close any "Preview" windows currently open.  When the 'hidden'
		option is set, or when the buffer was changed and the [!] is
		used, the buffer becomes hidden (unless there is another
		window editing it).  The command fails if any "Preview" buffer
		cannot be closed.  See also |:close|.


							*:pp* *:ppop*
:[count]pp[op][!]
		Does ":[count]pop[!]" in the preview window.  See |:pop| and
		|:ptag|.  {not in Vi}


CTRL-W }						*CTRL-W_}*
		Use identifier under cursor as a tag and perform a :ptag on
		it. Make the new Preview window (if required) N high.  If N is
		not given, 'previewheight' is used.


CTRL-W g }						*CTRL-W_g}*
		Use identifier under cursor as a tag and perform a :ptjump on
		it. Make the new Preview window (if required) N high.  If N is
		not given, 'previewheight' is used.


CTRL-W f					*CTRL-W_f* *CTRL-W_CTRL-F*
CTRL-W CTRL-F	Split current window in two.  Edit file name under cursor.
		Like ":split ]f", but window isn't split if the file does not
		exist.
		Uses the 'path' variable as a list of directory names where to
		look for the file.  Also the path for current file is
		used to search for the file name.
		If the name is a hypertext link that looks like
		"type://machine/path", only "/path" is used.
		If a count is given, the count'th matching file is edited.
		Not available when the |+file_in_path| feature was disabled at
		compile time.

Also see |CTRL-W_CTRL-I|: open window for an included file that includes
the keyword under the cursor.

NEWS CONTENTS

Old News

InformIT Basic vi Skills for the Linux LPIC Exams Advanced vi

Several tasks are part of vi that don't fit in any other section. Most of these are quite advanced, such as running external commands, joining lines, and splitting windows. This section covers these in detail.

Running External Commands in vi

A frequent question on the exams is how to run an external command inside vi, such as seeing an ls -l listing of the current directory so you can remember a filename:

:! ls -l

In this, the command ls -l executes, with the command's output displaying onscreen, and you need only press Enter or enter a command to return to the vi session. If the output scrolls more than one screen, it's piped to the more command and all the normal movement keystrokes will apply.

Joining Lines

It's quite irritating in vi to be at the front of a line and want to use the Backspace key to move that line to the end of the previous line. The Backspace key works only on the current line. If you need a line to be joined to the previous line, you can position the cursor in either line and press Shift+J to cause the second to be appended to the end of the first line.

Say you have a file named file1 that contains the following text:

This is line 1
This is a longer line 2
This is an even longer line 3

You want to join line 1 and line 2, so you position your cursor somewhere on line 1 and press the J key. The file then looks like the following:

This is line 1 This is a longer line 2
This is an even longer line 3

Putting the cursor on line 2 and pressing J joins line 2 and line 3 in a similar fashion.

Split Windows

Last, but not least, is splitting windows in vi, specifically the vim version of vi. When you're editing a particular file and want to see either another section of that same file or even another file altogether, you can use the following:

Moving between the panes is somewhat counterintuitive because you must press Ctrl+W twice to move between the windows.

To edit a completely different file, you should edit the first one in vi; then to split the screen horizontally with the other file loaded in the second pane, you can enter

:split file2

To set the height of the newly split window from the split, you could enter the following:

:10split /etc/fstab

This command splits the top 10 lines of the screen and displays the contents of the /etc/fstab file therein.

If you wanted to close the split window, you would focus on it by pressing Ctrl+W and then entering

:close

Better yet, after comparing something or getting a filename straight, you can close all the other split windows and just have the current window open by entering the following:

:only

NOTE

Many times I've opened a couple of split windows to see the difference between two files or used the diff command. The easiest way to compare two files and make edits to them is to use the vimdiff command, such as

:vimdiff file1 file2

This loads the two files into a vertically split vim session and uses color and other symbols to show you what is similar or different between the two files. This is useful for comparing a

y default, Vim opens only one window for a session, even if you specify more than one file. While we don't know for sure why Vim would not open multiple windows for multiple files, it may be because using just a single window is consistent with vi behavior. Multiple files occupy multiple buffers, with each file in its own buffer. (Buffers are discussed shortly.)

To open multiple windows from the command line, use Vim's -o option. For example:

$ vim -o file1 file2

This opens the edit session with the display horizontally split into two equal-sized windows, one for each file (see Figure 11-1). For each file named on the command line, Vim tries to open a window for editing. If Vim cannot split the screen into enough windows for the files, the first files listed in the command-line arguments get windows, while the remaining files are loaded into buffers not visible (but still available) to the user.

Another form of the command line preallocates the windows by appending a number n to -o:

$ Vim -o5 file1 file2

This opens a session with the display horizontally split into five equal-sized windows, the topmost of which contains file1 and the second of which contains file2

Tip

When Vim creates more than one window, its default behavior is to create a status line for each window (whereas the default behavior for a single window is not to display any status line). You can control this behavior with Vim's laststatus option, e.g.:

:set laststatus=1

Set laststatus to 2 to always see a status line for each window, even in single window mode. (It is best to set this in your .vimrc file.)

Because window size affects readability and usability, you may want to control Vim's limits for window sizes. Use Vim's winheight and winwidth options to define reasonable limits for the current window (other windows may be resized to accommodate it).

Split and vsplit commands

You can initiate and modify the window configuration from within Vim. Create a new window with the :split command. This breaks the current window in half, showing the same buffer in both halves. Now you can navigate independently in each window on the same file.

Similarly, you can create a new, vertically separated edit window with the :vsplit command

If no file was specified on the :split command line, you end up editing the same file with two views or windows.

Use Crtl-w,w to move to next windows in circular order.

Note

There are convenience key sequences for many of the commands in this chapter. In this case, for instance, ^Ws splits a window. (All Vim window-related commands begin with ^W, with the "W" being mnemonic for "window.") For the purposes of discussion, we show only the command-line methods because they provide the added power of optional parameters that customize the default behavior.

If you find yourself using commands routinely, you can easily find the corresponding key sequence in the Vim documentation, as described in Built-in Help.

For each of these methods, Vim splits the window (horizontally or vertically), and

Tip

Don't believe you're editing the same file at the same time? Split the edit window and scroll each window so that each shows the same area of the file. Make changes. Watch the other window. Magic.

Why or how is this useful? One common use by this author, when writing shell scripts or C programs, is to code a block of text that describes the program's usage. (Typically, the program will display the block when passed a --help option.) I split the display so that one window displays the usage text, and I use this as a template to edit the code in the other window that parses all the options and command-line arguments described in the usage text. Often (almost always) this code is complex and ends up far enough from the usage text that I can't display everything I want in a single window.

If you want to edit or browse another file without losing your place in your current file, provide the new file as an argument to your :split command. For instance:

:split otherfile

New Windows

As discussed previously, the simplest way to open a new window is to issue :split (for a horizontal division) or :vsplit (for a vertical division). A more in-depth discussion of the many commands and variations follows. We also include a command synopsis for quick reference.

Options During Splits

The full :split command to open a new horizontal window is:

:[n]split [++opt] [+cmd] [file]

where:

n
Tells Vim how many lines to display in the new window, which goes at the top.
opt
Passes Vim option information to the new window session (note that it must be preceded by two plus signs).
cmd
Passes a command for execution in the new window (note that it must be preceded by a single plus sign).
file
Specifies a file to edit in the new window.

For example, suppose you are editing a file and want to split the window to edit another file named otherfile. You want to ensure that the session uses a fileformat of unix (which ensures the use of a line feed to end each line instead of a carriage return and line feed combination). Finally, you want the window to be 15 lines tall. Enter:

:15split ++fileformat=unix otherfile

To simply split the screen, showing the same file in both windows and using all the current defaults, you can use the key commands ^Ws, ^WS, or ^W^S.

Tip

If you want windows to always split equally, set the equalalways option, preferably putting it in your .vimrc to make it persistent over sessions. By default, setting equalalways splits both horizontal and vertical windows equally. Add the eadirection option (hor, ver, both, for horizontal, vertical, or both, respectively) to control which direction splits equally.

The following form of the :split command opens a new horizontal window as before, but with a slight nuance:

:[n]new [++opt] [+cmd] [file]

In addition to creating the new window, the WinLeave, WinEnter, BufLeave, and BufEnter autocommands execute. (For more information on autocommands, see the section Autocommands.)

Along with the horizontal split commands, Vim offers analogous vertical ones. So, for example, to split a vertical window, instead of :split or :new, use :vsplit and :vnew respectively. The same optional parameters are available as for the horizontal split commands.

There are two horizontal split commands without vertical cousins:

:sview filename
Splits the screen horizontally to open a new window and sets the readonly for that buffer. :sview requires the filename argument.
:sfind [++ opt ] [+ cmd ] filename
Works like :split, but looks for the filename in the path. If Vim does not find the file, it doesn't split the window.

Conditional Split Commands

Vim lets you specify a command that causes a window to open if a new file is found. :topleft cmd tells Vim to execute cmd and display a new window with the cursor at the top left if cmd opens a new file. The command can produce three different results:

Window Command Summary

ex command vi command Description
:[n]split [++opt] [+cmd] [file]
^Ws

^WS

^W^S

Split the current window into two from side to side, placing the cursor in the new window. The optional file argument places that file in the newly created window. The windows are created as equal in size as possible, determined by free window space.
:[n]new [++opt] [+cmd]
^Wn

^W^N

Same as :split, but start the new window editing an empty file. Note that the buffer will have no name until one is assigned.
:[n]sview [++opt] [+cmd] [file]
Read-only version of :split.
:[n]sfind [++opt] [+cmd] [file]
Split window and open file (if specified) in the new window. Look for file in the path.
:[n]vsplit [++opt] [+cmd] [file]
^Wv

^W^V

Split current window into two from top to bottom and open file (if specified) in the new window.
:[n]vnew [++opt] [+cmd

Moving Around Windows (Getting Your Cursor from Here to There)

It's easy to move from window to window with a mouse in both gvim and Vim. gvim supports clicking with the mouse by default, whereas in Vim you can enable the behavior with the mouse option. A good default setting for Vim is :set mouse=a, to activate the mouse for all uses: command line, input, and navigation.

If you don't have a mouse, or prefer to control your session from the keyboard, Vim provides a full set of navigation commands to move quickly and accurately among session windows. Happily, Vim uses the mnemonic prefix keystroke ^W consistently for window navigation. The keystroke that follows defines the motion or other action, and should be familiar to experienced vi and Vim users because they map closely to the same motion commands for editing.

Rather than describe each command and its behavior, we will consider an example. The command-synopsis table should then be self-explanatory.

To move from the current Vim window to the next one, type CTRL-W j (or CTRL-W <down> or CTRL-W CTRL-J). The CTRL-W is the mnemonic for "window" command, and the j is analogous to Vim's j command, which moves the cursor to the next line.

Note

As with many Vim and vi commands, these can be multiply executed by prefixing them with a count. For example, 3^Wj tells Vim to jump to the third window down from the current window.

Mnemonic Tips

t and b are mnemonic for top and bottom windows.

In keeping with the convention that lowercase and uppercase implement opposites, CTRL-W w moves you through the windows in the opposite direction from CTRL-W W.

The Control characters do not distinguish between uppercase and lowercase; in other words, pressing the Shift key while pressing a CTRL- key itself has no effect. However, an upper/lowercase distinction is recognized for the regular keyboard key you press afterward.

Moving Windows Around

You can move windows two ways in Vim. One way simply swaps the windows on the screen. The other way changes the actual window layouts. In the first case, window sizes remain constant while windows change position on the screen. In the second case, windows not only move but are resized to fill the position to which they've moved.

Moving Windows (Rotate or Exchange)

Three commands move windows without modifying layout. Two of these rotate the windows positionally in one direction (to the right or down) or the other (to the left or up), and the other one exchanges the position of two possibly nonadjacent windows. These commands operate only on the row or column in which the current window lives.

CTRL-Wr rotates windows to the right or down. Its complement is CTRL-WR, which rotates windows in the opposite direction.

An easier way to imagine how these work is to think of a row or column of Vim windows as a one-dimensional array. CTRL-W r would shift each element of the array one position to the right, and move the last element into the vacated first position. CTRL-W R simply moves the elements the other direction.

If there are no windows in a column or row that aligns with the current window, this command does nothing.

After Vim rotates the windows, the cursor remains in the window from which the rotate command executed; thus, the cursor moves with the window.

CTRL-Wx and CTRL-WCTRL-X let you exchange two windows in a row or column of windows. By default, Vim exchanges the current window with the next window, and if there is no next window, Vim tries to exchange with the previous window. You can exchange with the nth next window by prepending a count before the command. For example, to switch the current window with the third next window, use the command 3^Wx.

As with the two previous commands, the cursor stays in the window from which the exchange command executes.

Moving Windows and Changing Their Layout

Five commands move and reflow the windows: two move the current window to a full-width top or bottom window, two move the current window to a full-height left or right window, and the fifth moves the current window to another existing tab. (See the section Tabbed Editing.) The first four commands bear familiar mnemonic relationships to other Vim commands; for instance, CTRL-W K maps to the traditional notion of k as "up." Table 11-2 summarizes these commands.

Commands to move and reflow windows
Command Description
^WK Move the current window to the top of the screen, using the full width of the screen.
^WJ Move the current window to the bottom of the screen, using the full width of the screen.
^WH Move the current window to the left of the screen, using the full height of the screen.
^WL Move the current window to the right of the screen, using the full height of the screen.
^WT Move the current window to a new existing tab.

It is difficult to describe the exact behavior of these layout commands. After the move and expansion of the window to the full height or width of the screen, Vim reflows the windows in a reasonable way. The behavior of the reflow can also be influenced by some of the windows options settings.

Window Move Commands: Synopsis

Commands to rotate window positions

Command Description
^Wr

^W^R

Rotate windows down or to the right.
^WR Rotate windows up or to the left.
^Wx

^W^X

Swap positions with the next window, or if issued with a count n, swap with nth next window.
Table 11-4. Commands to change position and layout
Command Description
^WK Move window to top of screen and use full width. The cursor stays with the moved window.
^WJ Move window to bottom of screen and use full width. The cursor stays with the moved window.
^WH Move window to left of screen and use full height. The cursor stays with the moved window.
^WL Move window to right of screen and use full height. The cursor stays with the moved window.
^WT Move window to new tab. The cursor stays with the moved window. If the current window is the only window in the current tab, no action is taken.

Moving Windows Around

You can move windows two ways in Vim. One way simply swaps the windows on the screen. The other way changes the actual window layouts. In the first case, window sizes remain constant while windows change position on the screen. In the second case, windows not only move but are resized to fill the position to which they've moved.

Moving Windows (Rotate or Exchange)

Three commands move windows without modifying layout. Two of these rotate the windows positionally in one direction (to the right or down) or the other (to the left or up), and the other one exchanges the position of two possibly nonadjacent windows. These commands operate only on the row or column in which the current window lives.

CTRL-Wr rotates windows to the right or down. Its complement is CTRL-W R, which rotates windows in the opposite direction.

An easier way to imagine how these work is to think of a row or column of Vim windows as a one-dimensional array. CTRL-W r would shift each element of the array one position to the right, and move the last element into the vacated first position. CTRL-W R simply moves the elements the other direction.

If there are no windows in a column or row that aligns with the current window, this command does nothing.

After Vim rotates the windows, the cursor remains in the window from which the rotate command executed; thus, the cursor moves with the window.

CTRL-Wx and CTRL-WCTRL-X let you exchange two windows in a row or column of windows. By default, Vim exchanges the current window with the next window, and if there is no next window, Vim tries to exchange with the previous window. You can exchange with the nth next window by prepending a count before the command. For example, to switch the current window with the third next window, use the command 3^Wx.

As with the two previous commands, the cursor stays in the window from which the exchange command executes.

Moving Windows and Changing Their Layout

Five commands move and reflow the windows: two move the current window to a full-width top or bottom window, two move the current window to a full-height left or right window, and the fifth moves the current window to another existing tab. (See the section Tabbed Editing.) The first four commands bear familiar mnemonic relationships to other Vim commands; for instance, CTRL-W K maps to the traditional notion of k as "up." Table 11-2 summarizes these commands.

Commands to move and reflow windows
Command Description
^WK Move the current window to the top of the screen, using the full width of the screen.
^WJ Move the current window to the bottom of the screen, using the full width of the screen.
^WH Move the current window to the left of the screen, using the full height of the screen.
^WL Move the current window to the right of the screen, using the full height of the screen.
^WT Move the current window to a new existing tab.

It is difficult to describe the exact behavior of these layout commands. After the move and expansion of the window to the full height or width of the screen, Vim reflows the windows in a reasonable way. The behavior of the reflow can also be influenced by some of the windows options settings.

Window Move Commands: Synopsis

Commands to rotate window positions

Command Description
^Wr

^W^R

Rotate windows down or to the right.
^WR Rotate windows up or to the left.
^Wx

^W^X

Swap positions with the next window, or if issued with a count n, swap with nth next window.
Table 11-4. Commands to change position and layout
Command Description
^WK Move window to top of screen and use full width. The cursor stays with the moved window.
^WJ Move window to bottom of screen and use full width. The cursor stays with the moved window.
^WH Move window to left of screen and use full height. The cursor stays with the moved window.
^WL Move window to right of screen and use full height. The cursor stays with the moved window.
^WT Move window to new tab. The cursor stays with the moved window. If the current window is the only window in the current tab, no action is taken.

Window Resize Commands

As you'd expect, Vim has vertical and horizontal resize commands. Like the other window commands, these all begin with CTRL-W and map nicely to mnemonic devices, making them easy to learn and remember.

CTRL-W= tries to resize all windows to equal size. (This is also influenced by the current values of winheight and windwidth, discussed in the following section.) If the available screen real estate doesn't divide equally, Vim sizes the windows to be as close to equal as possible.

CTRL-W- decreases the current window height by one line. Vim also has an ex command that lets you decrease the window size explicitly. For example, the command resize -4 decreases the current window by four lines and gives those lines to the window below it.

Note

It's interesting to note that Vim obediently decreases your window size even if you are not in a multiple window edit session. While it may seem counterintuitive at first, the side effect is that Vim decreases the window as requested and the vacated screen real estate is allocated to the command-line window. Typically, the command-line window always uses a single line, but there are reasons to use a command-line window larger than one line high. (The most common reason we know of is to provide enough space to let Vim display complete command-line status and feedback without intermediate prompts.) That said, it's best to use the :resize command to resize your current window, and to use the winheight option to size your command window.

CTRL-W+ increases the current window by one line. The :resize +n command increases the current window size by n lines. Once the window's maximum height is reached, further use of this command has no effect.

Tip

One of the authors' favorite ways to use the CTRL-W + and CTRL-W - commands is by mapping each to keys, both keys adjacent. The + key is a convenient choice. Though it is already the Vim "up" command, that behavior is redundant and little used by veteran Vim users (who use the k command instead). Therefore, this key is a good candidate to map to something else, in this case CTRL-W +. Immediately to that key's left (on most standard keyboards) is the -. But since it is unshifted and the + is shifted, map the shifted key, _, to CTRL-W -. Now you have two convenient side-by-side keys to easily and quickly expand and contract your current window horizontally.

:resizen sets the horizontal size of the current window to n lines. It sets an absolute size, in contrast to the previously described commands that make a relative change.

zn sets the current window height to n lines. Note that n is not optional! Omitting it results in the vi/Vim command z, which moves the cursor to the top of the screen.

CTRL-W< and CTRL-W> decrease and increase the window width, respectively. Think of the mnemonic device of "shift left" (<<) and "shift right" (>>) to associate these commands to their function.

Finally, CTRL-W | resizes the current window to the widest size possible (by default). You can also specify explicitly how to change the window width with vertical resize n. The n defines the window's new width.

Window Sizing Options

Several Vim options influence the behavior of the resize commands described in the previous section.

winheight and winwidth define the minimal window height and width, respectively, when a window becomes active. For example, if the screen accommodates two equal-sized windows of 45 lines, the default Vim behavior is to split them equally. If you were to set winheight to a value larger than 45-say, 60-Vim will resize the window to which you move each time to 60 lines, and will resize the other window to 30. This is handy for editing two files simultaneously; you automatically increase the allocated window size for maximum context when you switch from window to window and from file to file.

equalalways tells Vim to always resize windows equally after splitting or closing a window. This is a good option to set in order to ensure equitable allocation of windows as you add and delete them.

eadirection defines directional jurisdiction for equalalways. The possible values hor, ver, and both tell Vim to make windows of equal size horizontally, vertically, or both, respectively. The resizing applies each time you split or delete a window.

cmdheight sets the command line height. As described previously, decreasing a window's height when there is only one window increases the command-line height. You can keep the command line the height you want using this option.

Finally, winminwidth and winminheight tell Vim the minimum width and height to size windows. Vim considers these to be hard values, meaning that windows will never be allowed to get smaller than these values.

Resizing Command Synopsis

Window resizing commands

Command or option Description
^W= Resize all windows equally. The current window honors the settings of the winheight and winwidth options.
:resize -n
^W-
Decrease the current window size. The default amount is one line.
:resize +n

^W+

Increase the current window size. The default amount is one line.
:resizen

^W^_

^W_

Set the current window height. The default is to maximize window height (unless n is specified).
zn <ENTER> Set the current window height to n.
^W< Increase the current window width. The default amount is one column.
^W> Decrease the current window width. The default amount is one column.
:vertical resizen

^W|

Set the current window width to n. The default is to make window as wide as possible.
winheight option When entering or creating a window, set its height to at least the specified value.
winwidth option When entering or creating a window, set its width to at least the specified value.
equalalways option When the number of windows changes, either by splitting or closing windows, resize them to be the same size.
eadirection option Define whether Vim resizes windows equally vertically, horizontally, or both.
cmdheight option Set the command line height.
winminheight option Define the minimum window height, which applies to all windows created.
winminwidth option Define the minimum window width, which applies to all windows created.

log file before and after a problem or two configuration files to see why one doesn't work.


Top Visited
Switchboard
Latest
Past week
Past month

Recommended Links

Google matched content

Softpanorama Recommended

Top articles

Sites



Etc

Society

Groupthink : Two Party System as Polyarchy : Corruption of Regulators : Bureaucracies : Understanding Micromanagers and Control Freaks : Toxic Managers :   Harvard Mafia : Diplomatic Communication : Surviving a Bad Performance Review : Insufficient Retirement Funds as Immanent Problem of Neoliberal Regime : PseudoScience : Who Rules America : Neoliberalism  : The Iron Law of Oligarchy : Libertarian Philosophy

Quotes

War and Peace : Skeptical Finance : John Kenneth Galbraith :Talleyrand : Oscar Wilde : Otto Von Bismarck : Keynes : George Carlin : Skeptics : Propaganda  : SE quotes : Language Design and Programming Quotes : Random IT-related quotesSomerset Maugham : Marcus Aurelius : Kurt Vonnegut : Eric Hoffer : Winston Churchill : Napoleon Bonaparte : Ambrose BierceBernard Shaw : Mark Twain Quotes

Bulletin:

Vol 25, No.12 (December, 2013) Rational Fools vs. Efficient Crooks The efficient markets hypothesis : Political Skeptic Bulletin, 2013 : Unemployment Bulletin, 2010 :  Vol 23, No.10 (October, 2011) An observation about corporate security departments : Slightly Skeptical Euromaydan Chronicles, June 2014 : Greenspan legacy bulletin, 2008 : Vol 25, No.10 (October, 2013) Cryptolocker Trojan (Win32/Crilock.A) : Vol 25, No.08 (August, 2013) Cloud providers as intelligence collection hubs : Financial Humor Bulletin, 2010 : Inequality Bulletin, 2009 : Financial Humor Bulletin, 2008 : Copyleft Problems Bulletin, 2004 : Financial Humor Bulletin, 2011 : Energy Bulletin, 2010 : Malware Protection Bulletin, 2010 : Vol 26, No.1 (January, 2013) Object-Oriented Cult : Political Skeptic Bulletin, 2011 : Vol 23, No.11 (November, 2011) Softpanorama classification of sysadmin horror stories : Vol 25, No.05 (May, 2013) Corporate bullshit as a communication method  : Vol 25, No.06 (June, 2013) A Note on the Relationship of Brooks Law and Conway Law

History:

Fifty glorious years (1950-2000): the triumph of the US computer engineering : Donald Knuth : TAoCP and its Influence of Computer Science : Richard Stallman : Linus Torvalds  : Larry Wall  : John K. Ousterhout : CTSS : Multix OS Unix History : Unix shell history : VI editor : History of pipes concept : Solaris : MS DOSProgramming Languages History : PL/1 : Simula 67 : C : History of GCC developmentScripting Languages : Perl history   : OS History : Mail : DNS : SSH : CPU Instruction Sets : SPARC systems 1987-2006 : Norton Commander : Norton Utilities : Norton Ghost : Frontpage history : Malware Defense History : GNU Screen : OSS early history

Classic books:

The Peter Principle : Parkinson Law : 1984 : The Mythical Man-MonthHow to Solve It by George Polya : The Art of Computer Programming : The Elements of Programming Style : The Unix Hater’s Handbook : The Jargon file : The True Believer : Programming Pearls : The Good Soldier Svejk : The Power Elite

Most popular humor pages:

Manifest of the Softpanorama IT Slacker Society : Ten Commandments of the IT Slackers Society : Computer Humor Collection : BSD Logo Story : The Cuckoo's Egg : IT Slang : C++ Humor : ARE YOU A BBS ADDICT? : The Perl Purity Test : Object oriented programmers of all nations : Financial Humor : Financial Humor Bulletin, 2008 : Financial Humor Bulletin, 2010 : The Most Comprehensive Collection of Editor-related Humor : Programming Language Humor : Goldman Sachs related humor : Greenspan humor : C Humor : Scripting Humor : Real Programmers Humor : Web Humor : GPL-related Humor : OFM Humor : Politically Incorrect Humor : IDS Humor : "Linux Sucks" Humor : Russian Musical Humor : Best Russian Programmer Humor : Microsoft plans to buy Catholic Church : Richard Stallman Related Humor : Admin Humor : Perl-related Humor : Linus Torvalds Related humor : PseudoScience Related Humor : Networking Humor : Shell Humor : Financial Humor Bulletin, 2011 : Financial Humor Bulletin, 2012 : Financial Humor Bulletin, 2013 : Java Humor : Software Engineering Humor : Sun Solaris Related Humor : Education Humor : IBM Humor : Assembler-related Humor : VIM Humor : Computer Viruses Humor : Bright tomorrow is rescheduled to a day after tomorrow : Classic Computer Humor

The Last but not Least Technology is dominated by two types of people: those who understand what they do not manage and those who manage what they do not understand ~Archibald Putt. Ph.D


Copyright © 1996-2021 by Softpanorama Society. www.softpanorama.org was initially created as a service to the (now defunct) UN Sustainable Development Networking Programme (SDNP) without any remuneration. This document is an industrial compilation designed and created exclusively for educational use and is distributed under the Softpanorama Content License. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

FAIR USE NOTICE This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available to advance understanding of computer science, IT technology, economic, scientific, and social issues. We believe this constitutes a 'fair use' of any such copyrighted material as provided by section 107 of the US Copyright Law according to which such material can be distributed without profit exclusively for research and educational purposes.

This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Grammar and spelling errors should be expected. The site contain some broken links as it develops like a living tree...

You can use PayPal to to buy a cup of coffee for authors of this site

Disclaimer:

The statements, views and opinions presented on this web page are those of the author (or referenced source) and are not endorsed by, nor do they necessarily reflect, the opinions of the Softpanorama society. We do not warrant the correctness of the information provided or its fitness for any purpose. The site uses AdSense so you need to be aware of Google privacy policy. You you do not want to be tracked by Google please disable Javascript for this site. This site is perfectly usable without Javascript.

Last modified: March 13, 2019