Vi and vim Editor (17): vim's Multi-Window Function (2)

Source: Internet
Author: User

Vi and vim Editor (17): vim's Multi-Window Function (2)

This article describes how to adjust the window size, buffer zone and its interaction with the window, edit by PAGE, close and exit the window.

Adjust window size:

In gvim, you can use the mouse to adjust the window size. You only need to click and drag the window boundary with the mouse. In vim, you only need to enable the mouse selection (": set mouse = a"), you can also use the mouse to adjust the window size.

Window size adjustment command:

^ W =: try to adjust all windows to the same size (this command is affected by the current winwidth and winheight options ). If the available screen blocks cannot be evenly divided, vim Splits them evenly as much as possible.

^ W-: reduces the height of the current window by one row. In vim, there is an ex command: resize, which can specify the size of the window to be reduced. For example, the ": resize-n" command can reduce the current window by n rows. Besides, even if the window is not edited in a multi-window session (that is, only one window is opened), vim can reduce the window size and leave the screen area in this space-time window to the command line window.

^ W +: adds a row to the height of the current window. Run the ": resize + n" command to increase the number of rows in the current window. Once the maximum window height is reached, this command will not work.

: Resize n: Set the height of the current window to n rows, and set the value to absolute size.

^ W <and ^ W>: Reduce and increase the width of the current window respectively.

^ W |: adjust the current window to the maximum possible width. You can also use the vertical resize n command to specify how to change the window. n defines the new width of the window.

Window size adjustment options:

Some vim options can affect the behavior of the size Adjustment Command described above.

When the window changes to active, winheight and winwidth define the minimum height and width of the window respectively. The default behavior of vim in splitting is an average allocation. Therefore, assuming that after the average split, the width of both windows is 55 columns. If you adjust winwidth to 80, the window width is automatically adjusted to 80 each time vim moves the cursor to a window, and the other window is 30. This behavior is very convenient. The window size is automatically increased when the window is switched and the file is switched.

Equalalways: Let vim split or close the window and adjust the window size to the same size.

Eadirection: defines the direction of equalalways. The available values are hor, ver, and both. The window sizes are adjusted to the same in the horizontal, vertical, and two directions respectively.

Height: Set the height of the command line. As mentioned earlier, when only one window is opened, reducing the window height will increase the height of the command line. Set this option to maintain the height of the command line.

Winminwidth and winminheight: used to determine the minimum width and height of the window. Vim considers these two Option values as hard and hard, so the window size cannot be smaller than these two values.

The buffer zone and its interaction with the window:

Vim uses a buffer as the container of the work object. There are many commands in vim that control the buffer zone and move in the buffer zone. But first, you need to learn the basic knowledge of the buffer zone.

Use the vim editor to divide three windows and open three different files: buffer_one.file, buffer_two.file, and buffer_three.file. Use the ": ls" or ": files" or "buffers" command to list the buffer, as shown in:

Each file corresponds to a buffer. Each buffer zone has a unique and unchangeable number. If you add an exclamation point after the preceding command, you can also list additional information for each buffer. In the list of buffers, first the buffer number and then the status flag. The code and description of the Status flag are as follows:

U: the buffer is not listed. This buffer is not listed unless you use !.

% Or #: % indicates the buffer used by the current window, while # indicates the buffer to be jumped using the ": e #" command (that is, the buffer corresponding to the compensation file ).

A or h: a indicates the active buffer, that is, the buffer is loaded and visible. H indicates hiding the buffer. Although the hidden buffer exists, it cannot be viewed in any window.

-Or =:-indicates that the modifiable option is disabled in the buffer zone. = Indicates that the file cannot be changed to an adjustable read-only file.

+ Or x: + indicates that the buffer is adjustable, and x indicates that the buffer has read errors.

Vim's special buffer zone:

Some buffers used by vim are called special buffers. In general, these buffers cannot be edited.

Quickfix: contains the error list created by the command or location list.

Help: contains the vim help file. When you use the ": help" command, these help files are placed in a special buffer.

Directory: contains the contents of a directory, that is, the list of files in a directory. It is a shortcut tool in vim that allows us to move in the buffer, and you can use the cursor to add the enter key to select the file to be edited.

Scratch: These buffers contain general-purpose text.

Hide the buffer zone:

The hidden buffer is the vim buffer that is not displayed in any current window. Hiding the buffer makes it easier to edit multiple files. You need to set the hidden option to use the hidden buffer.

Buffer command:

Vim automatically manages the buffer when multiple files or windows are opened or closed. The buffer command can perform almost all operations on the buffer. Next we will introduce two buffer commands, which can do a lot of work on many files at a time.

: Windo cmd: this command is short for "window do". This pseudo-buffer command (in fact, it is a window command) runs the specified command cmd in each window. It only acts on the current page. If an error occurs during command execution, that is, it stops in the window that produces the error, and the generated error window becomes the new current window. Cmd can use the pipe symbol (|) to concatenate multiple commands. Each of these commands is executed one by one in each window.

: Bufdo [!] Cmd: similar to the windo command, the operation object is to edit all the buffers in the session, not just the visible buffer in the current page.

Next we will list common buffer commands:

: Ls [!], : Files [!], : Buffers [!] : List the buffer and file names. If you add !, This includes non-list buffers;

: Ball,: sball: edit all parameters or buffers. Each buffer corresponds to a window;

: Unhide,: sunhide: edit all loading buffers. Each buffer corresponds to a window;

: Badd file: adds the file to the list;

: Bunload [!] : Detaches the buffer from the memory. If the buffer is not saved after modification, vim will not detach the buffer. With an exclamation point added, the vim warning will be ignored and the buffer zone will be forcibly uninstalled;

: Bdelete [!] : Unmount the buffer and delete it from the buffer list. The exclamation point serves the same purpose as above;

: Buffer [n],: sbuffer [n]: Move to buffer n (sbuffer opens a new window );

: Bnext [n],: sbnext [n]: Move to the next n buffer (sbnext opens a new window );

: BNext [n],: sbNext [n],: bprevious [n],: sbprevious [n]: Move to the nth buffer (sbNext, sbprevious opens a new window );

: Bfirst,: sbfirst: Move to the first buffer (sbfirst opens a new window );

: Blast,: sblast: Move to the last buffer (sblast will open a new window );

: Bmod [n],: sbmod [n]: Move to the nth modified buffer (sbmod opens a new window );

Track tags in the window:

Vim extends the vi tag function to a window and provides the tag traversal mechanism for multiple windows. This allows tracking a tag to open associated files in a new window.

Use ": stags [!] "Split the window to display the location of the tag found. If no tag is found, the command fails and a new window is not created.

^ W ^] Or ^ W] command: the effect is equivalent to ": stag ".

^ Wg]: similar to ^ W], but in the new window, vim executes the ": tselect tag" command, where tag is the identifier under the cursor;

^ Wg ^]: similar to ^ Wg], but execute the ": tjump" command in a new window;

^ Wf or ^ W ^ f: Split the window and edit the name of the file where the cursor is located;

^ Wgf: Open the name of the file where the cursor is located in the new page. If the object does not exist, no new page is created;

Edit by page:

Vim allows you to create new pages. Each page has its own behavior. You can split the screen and edit multiple files on each page. You can use paging in vim and gvim. commands related to paging editing include:

: Tabnew filename: open a new page and edit the new file. If no file is specified, vim only opens a new page with a buffer above it;

: Tabclose: Close the current page;

: Tabonly: Disable all pages except the current page;

Use Ctrl + PgUp (move one page to the right) and Ctrl + PgDn (move one page to the left) to move around between pages (in actual testing, it does not work in vim, in gvim ).

For paging editing, there are too few commands in the book. Here are a few. For more details, you can use ": help tab-page-commands ".

: Tabnext, gt: Move to the next page and move it cyclically (after moving to the last page, the first page will be returned );

: Tabnext [count], [count] gt: Move to the count page;

: Tabprevious,: tabNext, gT: Move to the previous page and move it cyclically;

: Tabprevious [count],: tabNext [count], [count] gT: Move back count pages;

: Tabfirst: Move to the first page;

: Tablast: Move to the last page;

Close and exit windows:

Vim has the following commands to close the window. These commands have a lot of details to note. For more details, refer to the vim Help documentation.

: Quit, ^ Wq, ^ W ^ q: exit the current window. If it is the last window on the screen, exit vim. If the hidden option is set and the window opens the last window of the file, the buffer zone is hidden. If the hidden option is not set, and the window is the last window to open the file, if there are unsaved changes in the buffer, the command fails (at this time, you want to force exit the window, you can add it after the quit command !, To discard the modification to the buffer ).

^ Wc: Close the current window. If the current window is the only window on the screen, this command fails to be executed.

: Only, ^ Wo, ^ W ^ O: Close all Windows except the current window.

Summary of vim commands in this chapter:

^ W =, ^ W-, ^ W +,: resize n,: resize + n,: resize-n, ^ W <, ^ W>, ^ W | ,: vertical resize, winheight option, winwidth option, export always option, eadirection option, fill height option, winminwidht option, winminheight option,: ls,: files,: buffers,: windo cmd ,: bufdo [!] Cmd,: ball,: sball,: unhide,: sunhide,: badd file,: bunload [!], : Bdelete [!], : Buffer [n],: sbuffer [n],: bnext [n],: sbnext [n],: bNext [n],: sbNext [n],: bprevious [n],: sbprevious [n],: bfirst,: sbfirst,: blast,: sblast,: bmod [n],: sbmod [n],: stags [!], ^ W], ^ W ^], ^ Wg], ^ Wg ^], ^ Wf, ^ W ^ f, ^ Wgf,: tabnew, filename,: tabclose,: tabonly, ": help tab-page-commands",: tabnext, gt,: tabnext [count], [count] gt,: tabprevious,: tabNext, gT,: tabprevious [count],: tabNext [count], [count] gT,: tabfirst,: tablast,: quit, ^ Wq, ^ W ^ q, ^ Wc,: only, ^ Wo, ^ W ^ O.

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.