Practical Vim< /a> vit Practical Vim< /a> Note that the styling for a Visual selection is the... development I thought I knew Vim, but Practical" name="description"/>
Tải bản đầy đủ - 0 (trang)
Tip 39. Divide Your Workspace into Split Windows

Tip 39. Divide Your Workspace into Split Windows

Tải bản đầy đủ - 0trang

Chapter 6. Manage Multiple Files



• 86



In Vim’s terminology, a window is a viewport onto a buffer (:h window ). We

can open multiple windows, each containing the same buffer, or we can load

different buffers into each window. Vim’s window management system is

flexible, allowing us to build a workspace tailored to the demands of our

workflow.



Creating Split Windows

When Vim starts up, it contains a single window. We can divide this window

horizontally with the s command, which creates two windows of equal

height. Or we can use the v command to split the window vertically,

producing two windows of equal width. We can repeat these commands as

often as we like, splitting our workspace again and again in a process that

resembles cell division.

The following figure illustrates a few of the possible results. In each case, the

shaded rectangle represents the active window.



s



v



s v



s v



Each time we use the s and v commands, the two resulting split

windows will contain the same buffer as the original window that was divided.

Having the same buffer displayed in separate windows can be useful, especially if we’re working on a long file. For example, we could scroll in one of

the windows to show a part of the buffer that we want to refer to while making

changes to another part of the buffer in the other window.



www.it-ebooks.info



report erratum • discuss



Divide Your Workspace into Split Windows



• 87



We can use the :edit command to load another buffer into the active window.

If we run s followed by :edit {filename}, we can divide our workspace and

then open another buffer in one split window while keeping the existing buffer

visible in the other split. Alternatively, we could use the command :split {filename}, which combines those two steps into one. This table summarizes the

ways of dividing our workspace into split windows:

Command



Effect



s



Split the current window horizontally, reusing the current buffer

in the new window



v



Split the current window vertically, reusing the current buffer

in the new window



:sp[lit] {file}



Split the current window horizontally, loading {file} into the new

window



:vsp[lit] {file}



Split the current window vertically, loading {file} into the new

window



Changing the Focus Between Windows

Vim provides a handful of commands for switching the focus between split

windows. This table summarizes some of the highlights (for the complete list,

see :h window-move-cursor ):

Command



Effect



w



Cycle between open windows



h



Focus the window to the left



j



Focus the window below



k



Focus the window above



l



Focus the window to the right



In fact, does the same thing as w . That means we can press

the key and hold it while typing ww (or wj or any of the others from the

table) to change the active window. It’s easier to type than w ,

even though it looks nastier when written down. Still, if you use split windows

heavily, you might want to consider mapping these commands to something

even more convenient.

If your terminal supports mouse interactions or if you’re using GVim, then

you can also activate a window by clicking it with the mouse. If it doesn’t

work for you, check that the ‘mouse’ option is set appropriately (:h 'mouse' ).



www.it-ebooks.info



report erratum • discuss



Chapter 6. Manage Multiple Files



• 88



Closing Windows

If we want to reduce the number of windows in our workspace, we can take

one of two strategies. We could use the :close command to close the active

window, or if we want to close all windows except the active one, we can

instead use the :only command. This table summarizes the options and shows

the normal mode equivalents:

Ex Command



Normal Command



Effect



:cl[ose]



c



Close the active window



:on[ly]



o



Keep only the active window, closing all others



Resizing and Rearranging Windows

Vim provides several key mappings for resizing windows. For the full list, look

up :h window-resize . This table summarizes a handful of the most useful

commands:

Keystrokes



Buffer Contents



=



Equalize width and height of all windows



_



Maximize height of the active window



|



Maximize width of the active window



[N]_



Set active window height to [N] rows



[N]|



Set active window width to [N] columns



Resizing windows is one of the few tasks I prefer to do with the mouse. It’s

simple: click on the line that separates two windows, drag the mouse until

each window is the desired size, and then let go of the mouse. This works

only if your terminal supports the mouse or if you’re using GVim.

Vim includes commands for rearranging windows, but rather than describing

them here, I’d like to point you toward a screencast on Vimcasts.org that

demonstrates the possibilities.1 You can also find more details by looking up

:h window-moving .



1.



http://vimcasts.org/e/7



www.it-ebooks.info



report erratum • discuss



Organize Your Window Layouts with Tab Pages



• 89



Tip 40



Organize Your Window Layouts with Tab Pages

Vim’s tabbed interface is different from that of many other text editors. We can

use tab pages to organize split windows into a collection of workspaces.



Figure 1—Organizing split windows into tab pages

In Vim, a tab page is a container that can hold a collection of windows

(:h tabpage ). If you’re accustomed to using another text editor, then Vim’s

tabbed interface might seem strange at first. Let’s begin by considering how

tabs work in many other text editors and IDEs.

The classic graphical user interface (GUI) for a text editor features a main

workspace for editing files and a sidebar that shows the directory tree of the

current project. If we click on a file in the sidebar, it opens a new tab in the

main workspace for the specified file. A new tab is created for each file that

we open. In this model, we could say that the tabs represent the set of files

that are currently open.

When we open a file using the :edit command, Vim doesn’t automatically create

a new tab. Instead, it creates a new buffer and loads it into the current window. Vim keeps track of the set of files that are open using the buffer list, as

we saw in Tip 36, on page 77.

Vim’s tab pages are not mapped to buffers in a one-to-one relationship.

Instead, think of a tab page as a container that can hold a collection of windows. Figure 1, Organizing split windows into tab pages, on page 89, illustrates

a workspace with three tab pages, each containing one or more windows. In

each scenario, the shaded rectangles represent the active windows and tabs.



www.it-ebooks.info



report erratum • discuss



Chapter 6. Manage Multiple Files



• 90



Tab pages are available to us whether we’re using GVim or running Vim inside

a terminal. GVim draws a tab bar as part of the GUI, giving it an appearance

much like that of a web browser or any other tabbed interface. When Vim

runs inside a terminal, it draws a tab bar as a textual user interface (TUI).

Apart from the differences in appearance, tab pages are functionally identical

whether rendered as a GUI or a TUI.



How to Use Tabs

Vim’s tab pages can be used to partition work into different workspaces. They

have more in common with the virtual desktops in Linux than they do with

the tabbed interface of most other text editors.

Suppose that we’re at work on a project, with our workspace divided into a

few split windows. Out of the blue, something urgent comes up and we have

to switch contexts. Rather than opening new files in our current tab page,

which would mess up our carefully assembled workspace, we can create a

new tab page and do the work there. When we’re ready to resume our previous

work, we just have to switch back to the original tab page where all of our

windows will have been preserved as we left them.

The :lcd {path} command lets us set the working directory locally for the current

window. If we create a new tab page and then use the :lcd command to switch

to another directory, we can then comfortably scope each tab page to a different project. Note that :lcd applies locally to the current window, not to the

current tab page. If we have a tab page containing two or more split windows,

we could set the local working directory for all of them by running :windo lcd

2

{path}. Check out episode 9 of Vimcasts for more information.



Opening and Closing Tabs

We can open a new tab page with the :tabedit {filename} command. If we omit

the {filename} argument, then Vim creates a new tab page containing an

empty buffer.

Alternatively, if the current tab page contains more than one window, we can

use the T command, which moves the current window into a new tab

page (see :h CTRL-W_T ).

If the active tab page contains only a single window, the :close command will

close the window and the tab page with it. Or we can use the :tabclose command, which closes the current tab page no matter how many windows it



2.



http://vimcasts.org/e/9



www.it-ebooks.info



report erratum • discuss



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tip 39. Divide Your Workspace into Split Windows

Tải bản đầy đủ ngay(0 tr)

×