Window separation with VIM tips

Source: Internet
Author: User

Split Window

The simplest command to open a new window is as follows:

: Split

This command splits the screen into two windows and places the cursor in the window above:

+ ---------------------------------- +
|/* File one. c */|
| ~ |
| ~ |
| One. c ===============================|
|/* File one. c */|
| ~ |
| One. c ===============================|
|
+ ---------------------------------- +

You can see two windows showing the same file. A row with "=" is a status bar used to display information about the window above it. (On the actual screen, the status bar is reversed.) The two Windows allow you to display two parts of a file at the same time. For example, you can display the variable definitions in the window above, and the codes using these variables are displayed in the window below.

The CTRL-W w command can be used to jump between windows. If you go to the window above, it will jump to the window below. If you go to the window below, it will jump to the window above. (The CTRL-W can do the same function which is to prevent you from shrinking your hand late from pressing the CTRL key sometimes for the second time .)


Close the window

Run the following command to close the window:

: Close

In fact, any command to exit editing can close the window, such as ": quit" and "ZZ. But "close"
This prevents you from accidentally exiting Vim when the next window is left.


Close all its window ports

If you have opened a set of windows, but now you only want to edit one of them, the following command can complete this function:

: Only

This command closes all Windows except the current window. If there is no disk in the window to be closed, Vim will display an error message and the window will not be closed.


Split the window with another file

Run the following command to open another window and use this window to edit another specified file:

: Split two. c

If you are editing one. c, the command execution result is:

+ ---------------------------------- +
|/* File two. c */|
| ~ |
| ~ |
| Two. c ===============================|
|/* File one. c */|
| ~ |
| One. c ===============================|
|
+ ---------------------------------- +

To open a window to edit a new file, run the following command:

: New

You can use the ": split" and ": new" commands to create any number of windows.


Window Size

: The split command can accept the number prefix. If this prefix is specified, this number is used as the window height. For example, the following command can open a three-line window and edit the file alpha. c:

: 3 split alpha. c

You can change the size of an opened window in several ways. If you have a mouse, it is easy: move the mouse pointer to the status bar of the split two windows, and drag up and down.

To expand the window:

CTRL-W +

To narrow down the window:

CTRL-W-

The two commands accept the number prefix to specify the number of rows to be expanded and reduced. So "4 CTRL-W +" will increase the window by 4 rows.

To set a window to a specified height, run the following command:

{Height} CTRL-W _

Is to enter a value first, then enter the CTRL-W and an underline (in the American English keyboard is Shift plus "-"). To extend a window as large as possible, you can use the CTRL-W _ command without a number prefix.


Use mouse labels

In Vim, you can use the keyboard to quickly complete a lot of work. However, unfortunately, changing the window size requires many keys. In this case, using the mouse will be faster. Move the cursor over the status bar, hold down the left button, and drag it. The status bar moves, which makes a window bigger and smaller.


Item

The 'winheight' option sets the minimum expected window height while the 'winminheight' option sets the minimum "hard" height. Similarly, 'winwidth' sets the minimum expected width while 'winwidth' sets the minimum hard width. The 'windows' option keeps all windows in the same size when they are closed or new windows are opened.


Vertical segmentation

": Split" command to create a window above the current window. To open a new window on the left of the window, run the following command:

: Vsplit

Or

: Vsplit two. c

The result of this command is as follows:

+ -------------------------------------- +
|/* File two. c */|/* file one. c */|
| ~ | ~ |
| ~ | ~ |
| ~ | ~ |
| Two. c ==================== one. c =====================|
|
+ -------------------------------------- +

In reality, the vertical lines in the middle are reversed. This is called vertical split line. It separates a window from the left and right.

There is also a "vnew" command to open a Vertical Split new window. Another method is:

: Vertical new

The "vertical" command can be placed before any command to split the window. This will replace horizontal split with vertical split when split the window. (If the command does not split the window, This prefix does not work ).


Hop between windows

Since you can use the vertical and horizontal separation commands to open any number of windows, you can set the window layout as needed. Next, you can use the following command to jump between Windows:

CTRL-W h jump to the Left window
CTRL-W j jumps to the following window
CTRL-W k jumps to the window above
CTRL-W l jump to the right window

CTRL-W t jump to the top of the window
CTRL-W B Jump to the bottom of the window

You may have noticed that the command like moving the cursor is used to jump to the window. If you like, use the direction keys. Other commands can be used to jump to other windows. For more information, see: | Q_wi |.


Move window

You have split some windows, but the current location is incorrect. In this case, you need a command to move the window. For example, you have opened three windows, as shown in the following figure:

+ ---------------------------------- +
|/* File two. c */|
| ~ |
| ~ |
| Two. c ===============================|
|/* File three. c */|
| ~ |
| ~ |
| Three. c =============================|
|/* File one. c */|
| ~ |
| One. c ===============================|
|
+ ---------------------------------- +

Obviously, the last window should be at the top. Move to that window (w with CTRL-W) and enter the following command:

CTRL-W K

Here, the upper-case K is used. In this way, the window will be moved to the top. You can note that K is used to move up. If you are using vertical split, the CTRL-W K will move the current window above and extend to the width of the screen. Assume that your layout is as follows:

+ ------------------------------------------- +
|/* Two. c */|/* three. c */|/* one. c */|
| ~ | ~ | ~ |
| ~ | ~ | ~ |
| ~ | ~ | ~ |
| ~ | ~ | ~ |
| ~ | ~ | ~ |
| Two. c =========== three. c =========== one. c =======|
|
+ ------------------------------------------- +

When you use the CTRL-W K in the middle window (three. c), the result is:

+ ------------------------------------------- +
|/* Three. c */|
| ~ |
| ~ |
| Three. c ========================================= |
|/* Two. c */|/* one. c */|
| ~ | ~ |
| Two. c ============================ one. c =====================|
|
+ ------------------------------------------- +

There are three similar commands (you have guessed it ):

CTRL-W H move current window to leftmost
CTRL-W J move current window to the bottom
CTRL-W L move the current window to the rightmost


Execute commands on all windows

You opened several windows and now you want to exit Vim. You can close each window separately. The faster method is:

: Qall

This indicates "quit all" (exit all ). If no disk is saved in any window, Vim will not exit. At the same time, the cursor will automatically jump to that window. You can use the ": write" command to save the file or ": quit! "Discard modification.

If you know that a window has been changed and you want to save it all, run the following command:

: Wall

This indicates "write all" (save all ). But in fact, it only saves modified files. Vim knows that it makes no sense to save a file that has not been modified.
In addition, there are combined commands of ": qall" and "wall:

: Wqall

This will save all modified files and exit Vim.
Finally, the following command exits Vim and discards all modifications:

: Qall!

Note that this command cannot be undone.


Opens a window for all parameters

To enable Vim to open a window for each file, you can use the "-o" parameter:

Vim-o one.txt two.txt three.txt

The result will be:

+ ------------------------------- +
| File one.txt |
| ~ |
| One.txt =============================|
| File two.txt |
| ~ |
| Two.txt =============================|
| File three.txt |
| ~ |
| Three.txt =============================|
|
+ ------------------------------- +

The "-o" parameter is used to vertically split the window.
If Vim has been started, run the ": all" command to open a window for each file in the parameter list.
Port. ": Vertical all" opens the window vertically separated.


Differences in vimdiff display

There is a special way to start Vim to display the differences between the two files. Let's open a "main. c" and insert some characters. When the 'backup 'option is set, save the file to generate "main. c ~ "Backup file.
Enter the following command in the command line: (not in Vim)

Vimdiff main. c ~ Main. c

Vim splits two files vertically. You can only see the modified and the upper and lower rows.

VV
+ ----------------------------------------- +
| ++ -- 123 lines:/* a | ++ -- 123 lines:/* a | <-fold
| Text |
| Text |
| Text |
| Text | changed text | <-modified row
| Text |
| Text | ------------------ | <-Delete row
| Text |
| Text |
| Text |
| ++ -- 432 lines: text | ++ -- 432 lines: text | <-fold
| ~ | ~ |
| ~ | ~ |
| Main. c ~ ================ Main. c ======================== |
|
+ ----------------------------------------- +

(This figure does not show the highlighted effect. You can use the vimdiff command to see better results)

Rows that have not been modified are folded into one row, which is called "closed fold ).
The row marked by "<-fold" is a fold that represents 123 rows with one row. These rows are identical in the two files. Marked as "<-modified rows" is highlighted, and the added rows are represented in another color. This clearly shows the differences between the two files. The deleted rows are marked with "---" display "and" <-deleted rows "in the main. c window. These characters do not exist. They are only used to fill in main. c to align with another window.


Stacked column

Each window has a display bar with slightly different colors on the left, marked as "VV" in the figure ". You will find that each fold has a plus sign at that position. Move the mouse over there and press the left button to open the fold, so that you can see the content inside. A minus sign is displayed in the fold column. If you click the minus sign, the fold will be closed again. Of course, this can only be used when you have a mouse. If you do not have one, you can use "zo" to open a fold. Disable zc ".


Comparison with Vim

Another way to start the comparison mode is to start from Vim: edit the "main. c" file, and split the window to display the difference:

: Edit main. c
: Vertical diffsplit main. c

The ": vertical" command separates windows vertically. If you do not write this command, the result will become a horizontal split.

If you have a patch or diff file for the current file, you can use the third method to start the comparison mode: edit the file first, and then tell Vim the patch file name:

: Edit main. c
: Vertical diffpatch main. c diff

Warning the patch file must contain only patches for the target file. Otherwise, you may get a lot of error messages. Some files that you do not intend to patch may be patched. The patch function only changes files in your memory and does not modify files on your hard disk (unless you decide to write the changes ).


Screen rolling binding

When there are many changes in the file, you can scroll the screen in the usual way. Vim tries its best to keep the two files aligned so that you can see the differences between the files side by side. To disable this feature temporarily, run the following command:

: Set noscrollbind


Jump to the modified location

If you cancel the folding function in some way, it may be difficult to find a change. Run the following command to jump to the next modification point:

] C

Reverse jump:

[C

You can jump farther with the number prefix.


Distinct

You can move the text from one window to another to eliminate the difference, or add several lines to one file. Vim may not be able to update the highlighted display in time. To fix this problem, run the following command:

: Diffupdate

To eliminate the difference, you can move a highlighted block from one window to another. Above
"Main. c" and "main. c ~ "For example, move the cursor to the Left window and the row to be deleted in another window. Run the following command:

: Dp

This copies the text from the left to the Right to eliminate the differences between the two sides. "Dp" indicates "diff put ".
You can also move the cursor to the right window, to the row that has been changed, and then execute
Run the following command:

: Do

This copies the text from the left to the Right to eliminate the difference. Since there is no difference between the two files, Vim will fold all the text. "Do" indicates "diff obtain ". It would be better to use "dg" (diff get. But it has another meaning ("dgg" deletes all text from the cursor to the first line ).

For more information about the comparison mode, see | vimdiff |.


Miscellaneous

The 'laststatus' option is used to specify when the status bar is displayed for the last window:

0 Never
1. Only split the window (default)
2 always

Many Commands for editing another file have a variant that uses a split window. For command line commands, this is implemented by a "s" in front. For example, ": tag" is used to jump to a tag. "stag" Splits a new window and jumps to that tag. For normal mode, a front CTRL-W can do this. For example, CTRL-^ jumps to the alternative file, And the CTRL-W CTRL-^ opens a new window and edits the alternative file.

The 'splitbelow' option allows a new window to appear under the current window. The 'splitright' option enables Vertical Split windows to appear on the right of the current window.

When opening a new window, you can add a modifier before the command to indicate where the new window should appear:

: Leftabve {cmd} upper left of the current window
: Aboveleft {cmd} Same as above
: Rightbelow {cmd} bottom right of the current window
: Belowright {cmd} Same as above
: Topleft {cmd} top or left of the entire Vim window
: Botright {cmd} bottom or rightmost of the entire Vim window

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.