Vim by default is to edit all files in one window, and only one buffer is displayed when moving between files or moving to different parts of a file. However, Vim also provides multi-window editing to simplify composite editing tasks. This article will learn how to use multiple windows in a running vim process, including initialization and startup of multi-window editing work, moving the cursor between windows, and moving windows in the display area.
To start multi-window editing:
You can start multi-window editing when Vim is turned on, or you can split the window in an edit session.
To start multiple windows from the command line (shell):
by default, Vim opens only one window for a session, even if multiple files have been specified when it is opened. If you want to open multiple windows from the command line, you need to use Vim's "-o" option, at which time Vim will try to open a window for each file listed on the command line. You can also specify the number of open windows by attaching a value after "-O". The "-o" option is already mentioned in the Learning VI and Vim Editor (page): Vim's major improvements to VI .
When VIM creates more than one window, it creates a status line by default for each window (if it is just a window, no state rows are rendered by default). This behavior can be controlled by using Vim's laststatus option, which is the default Laststatus=1. If you set Laststatus to 2, you can see the status line for each window, even if only one window is open.
Vim's Multi-window editing:
After you start vim, you can use ": Split" to create a new window. This command divides the current window into two halves, displaying the contents of the same buffer, so you can browse the same file in two windows. Similarly, with the ": Vsplit" command, you can create a completely new vertical splitter window.
Many of the commands mentioned in this chapter have a more convenient sequence of commands. For example, "^ws" (press Ctrl+w and press the S key) is equivalent to the ": Split" command. All Vim's window commands start with "^w" (W stands for window's meaning). but using the form of the EX command allows us to provide optional parameters that allow you to customize the default functionality.
Vim separates the window in the above manner (horizontally or vertically), and when the ": Split" command does not specify a file, we will edit the same file in two windows. If you want to edit or browse another file, you can pass the new file as a parameter to the ": Split" command, such as ": Split Otherfile".
Open the window:
The next step is to describe in detail how to get more precise behavior when splitting windows.
Options for separating Windows:
complete the ": Split" command to open the new horizontal window as follows: " : [N]split [++opt] [+cmd] [file] ". in the command:
N: Specifies the number of rows displayed in the new window for Vim, and the new window is at the top of the screen;
Opt: Pass VIM option information to a new window session;
CMD: Incoming command to execute in a new window;
File: Specifies the files that you want to edit in a new window;
": [N]new [++opt] [+cmd] [file]" command can also open a horizontal window as described above, but with a slight difference: In addition to creating a new window, the automatic command Winleave,winenter,bufleave, Bufenter was also carried out.
In addition to the horizontal split command, Vim also provides similar vertical split commands: ": Vsplit" or "vnew". However, there are two horizontal split commands that do not have a corresponding vertical split command:
": Sview filename": Split the screen horizontally to open a new window and set the buffer to ReadOnly. The filename in ": Sview" is the necessary parameter.
": sfind [++opt][+cnd] filename": Similar to the ": Split" command, but looks for filename in path and does not split the window if it is not found.
Conditional Split command:
VIM allows us to execute a command that opens a new window when a new file is found. " : TopLeft cmd" tells vim: executes CMD and displays a new window, if CMD successfully opens the new file, the cursor needs to be in the upper left corner. The three possible results of this command:
CMD horizontally splits the window, and the new window occupies the top half of the Vim window;
CMD splits the window vertically, and the new window occupies the left half of the Vim window;
CMD does not split the window, but moves the cursor to the upper-left corner of the current window.
Walk between windows (move the cursor between windows):
when using Gvim and Vim, it is convenient to move between windows. Gvim has the default support for mouse clicks, and Vim can use the ": Set mouse =a" command to turn on the mouse option to activate mouse usage for all purposes. Vim also provides a full set of navigation commands that can be moved quickly and accurately between session windows. The move commands between these windows closely correspond to the action commands at the time of editing.
"^WJ" or "^w<down>": Move to the next window, this command does not loop around, if it is already the bottom of the window, then the command is not valid. And when the command moves down, other windows in the same row are skipped;
" ^wk "or" ^w<up> ": Move to the previous window, just as opposed to the" ^WJ "command;
" ^WH "or" ^w<left> ": Move to the window to the left of the current window;
" ^WL "or" ^w<right> ": Move to the right window of the current window;
"^WW" or "^W^ W": Move to the window below or to the right window. This command loops through all vim windows. Move order: From left to right, top to bottom,
"^WT" or "^W^ T" : Move to the top-left window (t stands for top),
"^WB" or "^W^ B" : Move the window at the bottom right corner (b for bottom),
"^WP" or "^W^ p" : Moves to the previous (last accessed) window (P stands for previous);
To move a window:
Vim has two ways of moving the window itself: one is to simply switch the window on the screen, and the other is to change the actual layout of the window. The first case: although the window's position on the screen changes, but the dimensions remain unchanged, in the second case, the window not only moves, but also resizes to fill the position where they move.
Move the window itself (rotate or swap):
The next command will move the window, but it will not adjust the layout. That is, the window moves in a rotated form. The objects of these commands are limited to the row or column on which the current window is located.
"^WR": Rotates the window to the right or down, that is, the current row or each window in the front of the column moves one bit in the specified direction, and the last window moves to the first bit;
"^WR": Similar to "^WR", except that it rotates in the opposite direction. If no other window is in the same row or column as the current window, these commands do not have an action. And when the window is rotated, the cursor moves with the window;
"^WX": The position of the peer or the same column window can be exchanged. The default is to swap the position of the current window with its next window, and if there is no window below, try swapping the position with the previous window. You can also add a quantity before this command to exchange the position with the specified window;
Move the window and change the layout:
"^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 side of the screen, using the full height of the screen;
"^WL": Move the current window to the right end of the screen, using the full height of the screen;
"^WT": Moves the current window to a new existing page, if the current window is the only window in the current paging, no action occurs;
After moving and expanding the window to full screen width and height, vim re-allocates other windows in a reasonable way. Of course, redistribution is also affected by some of the window option settings.
This chapter VIM commands summary:
"-o" option, "laststatus" option, "split", "Vsplit", "^ws", "new", "Vnew", ": Sview", ": Sfind", ": TopLeft", ": Set Mouse=a "," ^WJ "," ^w<down> "," ^wk "," ^w<up> "," ^wh "," ^w<left> "," ^wl "" ^w<right> "," ^ww "," ^W^ W "," ^WT "," ^W^ T "," ^WB "," ^W^ B "," ^WP "," ^W^ P "," ^WR "," ^WR "," ^wx "," ^wk "," ^WJ "," ^wh "," ^wl "," ^WT ".
Learning VI and VIM editor (+): Vim's multi-window function (1)