Vi and vim Editor (16): vim's Multi-Window Function (1)

Source: Internet
Author: User

Vi and vim Editor (16): vim's Multi-Window Function (1)

By default, vim edits all files in a window. Only one buffer zone is displayed when files are moved or moved to different parts of the file. However, vim also provides multi-window editing to simplify the composite editing tasks. This article will learn how to use multiple windows in a running vim process, including initializing and starting multi-window editing, moving the cursor between windows, and moving the window in the display area.

Start multi-window Editing:

You can enable multi-window editing when you open vim, or split the window in the editing session.

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. To open multiple windows from the command line, use the "-o" option of vim. In this case, vim tries to open a window for each file listed on the command line. You can also attach a value to "-o" to specify the number of opened windows. For the "-o" option, see learning vi and vim Editor (15): vim's major improvements to vi.

When vim creates more than one window, it creates status rows for each window by default (if only one window is used, no status rows are displayed by default ). You can use the laststatus option of vim to control this behavior. The default value is laststatus = 1. If you set laststatus to 2, you can see the status line of each window, even if only one window is opened.

Vim multi-window Editing:

After vim is started, you can use ": split" to create a new window. This command divides the current window into two halves and displays the content in the same buffer zone. Therefore, you can browse the same file in the two windows. Similarly, you can use the ": vsplit" command to create a new Vertical Split Window.

Many Commands mentioned in this Chapter have more convenient command sequences. For example, "^ Ws" (press CTRL + W, and then press the s key) is equivalent to the ": split" command. All vim Window commands start with "^ W" (W stands for Window ). However, using the ex command allows us to provide optional parameters to customize the default function.

Vim separates windows horizontally or vertically. If the ": split" command does not specify a file, we will edit the same file in the two windows. If you want to edit or browse another file, you can pass the new file as a parameter to the ": split" command, for example, ": split otherfile ".

Open the window:

Next, we will introduce in detail how to obtain more precise behaviors when splitting windows.

Window separation options:

The complete ": split" command for opening a new horizontal window is as follows: ": [n] split [++ opt] [+ cmd] [file]". Command:

N: specify the number of lines displayed in the new window for vim. The new window is located at the top of the screen;

Opt: Pass the vim option information to the new window session;

Cmd: Enter the command to be executed in the new window;

File: Specifies the file to be edited in the new window;

": [N] new [++ opt] [+ cmd] [file]" command can also open a horizontal window in the preceding way, but there is a slight difference: in addition to creating new windows, the automatic commands WinLeave, WinEnter, BufLeave, and BufEnter are also executed.

In addition to the horizontal split command, vim also provides similar Vertical Split commands: ": vsplit" or "vnew ". However, there are two horizontal split commands without the corresponding Vertical Split command:

": Sview filename": horizontal split screen to open a new window and set the buffer to readonly. Filename in ": sview" is a required parameter.
": Sfind [++ opt] [+ cnd] filename": similar to the ": split" command, but the filename is searched in the path. If not found, the window is not split.

Conditional splitting command:

Vim allows us to execute a command to open a new window only when a new file is found. ": Topleft cmd" tells vim: Execute cmd and display a new window. If cmd successfully opens a new file, the cursor must be in the upper left corner. Three possible results of this command:

Cmd splits the window horizontally, and the new window occupies the upper half of the vim window;

Cmd vertically splits the window, 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.

Move the cursor between Windows ):

When gvim and vim are used, it is convenient to move between windows. Gvim supports mouse clicking by default, while vim enables the mouse option through the ": set mouse = a" command to activate the mouse for all purposes. Vim also provides a full set of navigation commands to quickly and accurately move between session windows. The move commands between these windows closely correspond to the action commands during editing.

"^ Wj" or "^ W <DOWN>": Move to the next window. This command does not move cyclically. If the window is already at the bottom, this command is invalid. When this command moves down, other windows in the same line will be skipped;

"^ Wk" or "^ W <UP>": Move to the previous window, which is the opposite of the "^ Wj" command;

"^ Wh" or "^ W <LEFT>": Move to the LEFT window of the current window;

"^ Wl" or "^ W <RIGHT>": Move to the window on the RIGHT of the current window;

"^ Ww" or "^ W": Move to the bottom window or the right window. This command will move cyclically across all vim windows. The moving order is: from left to right, from top to bottom;

"^ Wt" or "^ W ^ t": Move to the upper left corner of the window (t stands for top );

"^ Wb" or "^ W ^ B": Move the bottom right window (B stands for bottom );

"^ Wp" or "^ W ^ p": Move to the previous (last accessed) window (p stands for previous );

Move window:

Vim has two ways to move 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 position of the window on the screen changes, the size remains unchanged. In the second case, the window not only moves, but also adjusts the size to fill the position in which they move.

Move the window itself (rotation or switching ):

The following command can move the window without adjusting the layout. That is, the window is moved in rotation. The objects of these commands can only be the rows or columns of the current window.

"^ Wr": rotate the window to the right or down, that is, each window in the current row or current column moves one bit in the specified direction, and the window at the end moves to the first place;

"^ WR": similar to "^ Wr", the rotation direction is the opposite. If no other window is in the same row or column as the current window, these commands will not act. In addition, when the window is rotated, the cursor moves with the window;

"^ Wx": the position of the window in the same row or column. By default, the position of the current window and its next window is switched. If there is no window below, try to swap the position with the previous window. You can also add a number before the command to switch the location with the specified window;

Move the window and change the layout:

"^ WK": Move the current window to the top of the screen and use the full width of the screen;

"^ WJ": Move the current window to the bottom of the screen and use the full width of the screen;

"^ WH": Move the current window to the left side of the screen, and use the full height of the screen;

"^ WL": Move the current window to the right of the screen and use all the screen height;

"^ WT": Move the current window to the new existing page. If the current window is the only window in the current page, no operation is performed;

After the window is moved and expanded to the width and height of the full screen, vim allocates other windows in a reasonable way. Of course, the re-allocation will also be affected by some window options.

Summary of vim commands in this chapter:

"-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", "^ Wt", "^ W ^ t", "^ Wb", "^ W ^ B", "^ Wp ", "^ W ^ p", "^ Wr", "^ WR", "^ Wx", "^ WK", "^ WJ", "^ WH ", "^ WL", "^ WT ".

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.