Drill 6: Buffers, Windows & Tabs

Screencast

Review of Last Week

Last week's drill was focused on customizing Vim to our own needs, and creating custom keys that perform some more complex tasks in one quick keystroke. In future drills, we'll be revisiting this idea from several different angles. For now though, I want you to keep the idea of mappings in the back of your head while you do your work. It's so easy to both know that mappings exist and how they work, but never actually stop to think about how to apply them to your own workflow. Hopefully by being concious of them you can find some obvious mappings for yourself that will pay off.

This Week's Drill

This week we're going to go back to the idea of navigating around files and keeping the work you're interested in at hand.

Vim has a complex view of the files it edits. The terms window, buffer, tab, split get thrown around, and while they are all related, they all mean very distinct things to Vim.

Buffer

A buffer is an open file. For any given file, there will be only one buffer reflecting it.

:ls - Get a list of all open buffers

:buffer n - goes to buffer number n, where that number is found in the ls listing

:bdelete [id] - Delete a buffer by number. If you don't give the id, it closes the currently open buffer. Just like :q, it has a more aggressive version :bdelete! which won't ask to save the buffer first.

Windows

A window is your view into a buffer. Think of it like a porthole of a ship. You can see a piece of the ocean. And a different porthole will see a different piece.

A single buffer can be viewed by several windows. Which is useful to be able to see several parts of a file at a time.

Moving Between Windows

CTRL-W h - Goto the window on the left

CTRL-W l - Goto the window on the right

CTRL-W j - Goto the window below the cursor

CTRL-W k - Goto the window above the cursor

CTRL-W w - Goto the next window, clockwise. This will rotate you through all the windows. Also CTRL-W CTRL-W does the same thing, so no worries if you don't release the control key quick enough.

Rearranging Windows

CTRL-W H - Push the current window to the far left

CTRL-W L - Push the current window to the far right

CTRL-W J - Push the current window to the bottom

CTRL-W K - Push the current window to the top

<C-w>- - Make the current window a bit smaller height

<C-w>+ - Make the current window a bit bigger height

<C-w>< - Make the current window a bit smaller width

<C-w>> - Make the current window a bit bigger width

Closing Windows

:q - Our old friend! This actually just closes a window. If the tab page is now empty, it closes. If it was the last window open in the whole Vim instance, Vim exits.

Split

Just like it sounds, it splits a window into two. This opens a new window into the same buffer.

:split - Horizontal split, the line splitting the windows is horizontal, so the newly created windows are stacked on each other.

:vsplit - Vertical split, the line splitting the windows is vertical, so the windows created are side by side.

Tab Page

Holds several windows together. Even if you never use the ability to switch tabs, you still have one. It holds all the windows together.

:tabnew - Creates a new tab, after the current one.

gt - Tab forward

gT - Tab back

Mappings

OSX + MacVim Specific

nnoremap <D-1> 1gt - Go to the first open tab page. This matches up with how most OSX applications with tabs work as well (chrome, iterm, etc). It's nice for consistency. Of course, I have these mapped all the way up to 9.

Window Changes

nnoremap , <C-w><C-w> - This one is both very convenient, moving you forward a window, but might be a bit controversial, since it clobbers the rather useful , command. It's up to you, but I highly recommend an easily accessible key for this mapping

nnoremap - <C-w>- - Resizes the window to be a bit smaller vertically

nnoremap = <C-w>+ - Resizes the window to be a bit bigger vertically

nnoremap _ <C-w>< - Resizes the window to be smaller horizontally

nnoremap + <C-w>> - Resizes the window to be bigger horizontally

Tab Changing

nnoremap ; gt - Forwards a tab. Similarly to the , remap I have, this one steps on the useful ; command.

nnoremap <TAB>; gT - And backwards a tab

Practice

Get fast at opening, and switching windows, tabs, and buffers. Identify your normal workflow (maybe use mine? Tweak it maybe?) and optimize with windows and buffers and tabs. Keep everything in its place, but make your use consistent.

Comments

comments powered by Disqus