Learn-Vim/ch02_buffers_windows_tabs.md

218 lines
9.9 KiB
Markdown
Raw Normal View History

# Ch02. Buffers, Windows, and Tabs
2020-07-21 13:34:32 +00:00
2021-01-16 16:31:56 +00:00
If you have used a modern text editor, you are probably familiar with windows and tabs. Vim uses three display abstractions instead of two: buffers, windows, and tabs. In this chapter, I will explain what buffers, windows, and tabs are and how they work in Vim.
2020-07-21 13:34:32 +00:00
2021-01-16 16:31:56 +00:00
Before you start, make sure you have the `set hidden` option in vimrc. Without it, whenever you switch buffers and your current buffer is not saved, Vim will prompt you to save the file (you don't want that if you want to move quickly). I haven't cover vimrc yet. If you don't have a vimrc, create one. It is usually placed at the root directory and named `.vimrc`. I have mine on `~/.vimrc`. To see where you should create your vimrc, check out `:h vimrc`. Inside it, add:
```
set hidden
```
2021-01-08 16:16:26 +00:00
Save it, then source it (run `:source %` from inside the vimrc).
2020-07-21 13:34:32 +00:00
2020-11-13 01:02:04 +00:00
## Buffers
2020-07-21 13:34:32 +00:00
2021-01-16 16:31:56 +00:00
What is a *buffer*?
2021-01-16 16:32:49 +00:00
A buffer is an in-memory space where you can write and edit some text. When you open a file in Vim, the data is bound to a buffer. When you open 3 files in Vim, you have 3 buffers.
2020-07-22 12:54:07 +00:00
2021-01-16 16:31:56 +00:00
Have two empty files, `file1.js` and `file2.j` (if possible, create them with Vim) available. Run this in the terminal:
2020-07-21 13:34:32 +00:00
2020-12-10 10:20:52 +00:00
```bash
2020-07-21 13:34:32 +00:00
vim file1.js
```
2021-01-18 22:56:29 +00:00
![one buffer displayed with highlight](./images/screen-one-buffer-file1-highlighted.png)
2020-07-21 13:34:32 +00:00
2021-01-16 16:31:56 +00:00
What you are seeing is `file1.js` *buffer*. Whenever you open a new file, Vim creates a new buffer.
2020-07-21 13:34:32 +00:00
Exit Vim. This time, open two new files:
2020-12-10 10:20:52 +00:00
```bash
2020-07-21 13:34:32 +00:00
vim file1.js file2.js
```
2021-01-16 16:31:56 +00:00
2021-01-18 22:56:29 +00:00
![one buffer displayed.png](./images/screen-one-buffer.png)
2020-07-21 13:34:32 +00:00
2021-01-16 16:31:56 +00:00
Vim displays `file1.js` buffer, but it actually creates two buffers: `file1.js` buffer and `file2.js` buffer. Run `:buffers` to see all the buffers (alternatively, you can use `:ls` or `:files` too).
2020-07-21 13:34:32 +00:00
2021-01-18 22:56:29 +00:00
![buffers command showing 2 buffers](./images/screen-one-buffer-buffers-command.png)
2020-07-21 13:34:32 +00:00
There are several ways you can traverse buffers:
2021-01-16 16:31:56 +00:00
- `:bnext` to go to the next buffer (`:bprevious` to go to the previous buffer).
- `:buffer` + filename. Vim can autocomplete filename with `<Tab>`.
- `:buffer` + `n`, where `n` is the buffer number. For example, typing `:buffer 2` will take you to buffer #2.
- Jump to the older position in jump list with `Ctrl-o` and to the newer position with `Ctrl-i`. These are not buffer specific methods, but they can be used to jump between different buffers. I will talk more about jumps in Chapter 5.
- Go to the previously edited buffer with `Ctrl-^`.
2020-07-21 13:34:32 +00:00
2021-01-16 16:31:56 +00:00
Once Vim creates a buffer, it will remain in your buffers list. To remove it, you can type `:bdelete`. It accepts either a buffer number (`:bdelete 3` to delete buffer #3) or a filename (`:bdelete` then use `<Tab>` to autocomplete).
2020-07-21 13:34:32 +00:00
2021-01-16 16:31:56 +00:00
The hardest thing for me when learning about buffer was visualizing how buffers worked. Imagine a deck of playing cards. If I have 2 buffers, I have a stack of 2 cards. The card on top is the card I see. If I see `file1.js` buffer displayed then the `file1.js` card is on the top of the deck. I can't see the other card, `file2.js`. If I switch buffers to `file2.js`, that `file2.js` card is now on the top of the deck and `file1.js` card is at the bottom.
2020-07-21 13:34:32 +00:00
2021-01-16 16:31:56 +00:00
If you haven't used Vim before, this is a new concept. Take your time to understand it.
2020-07-21 13:34:32 +00:00
2020-11-13 01:02:04 +00:00
## Exiting Vim
2021-01-16 16:31:56 +00:00
By the way, if you have multiple buffers opened, you can close all of them with quit-all:
```
:qall
```
2021-01-16 16:31:56 +00:00
If you want to close without saving your changes, just add `!` at the end:
```
:qall!
```
2021-01-16 16:31:56 +00:00
To save and quit all, run:
```
:wqall
```
2020-11-13 01:02:04 +00:00
## Windows
2020-07-21 13:34:32 +00:00
2021-01-16 16:31:56 +00:00
A window is a viewport on a buffer. You can have multiple windows. Most text editors have the ability to display multiple windows. Below you see a VSCode with 3 windows:
2020-07-21 13:34:32 +00:00
2021-01-18 22:56:29 +00:00
![VSCode showing 3 windows](./images/screen-vscode-3-windows.png)
2020-07-21 13:34:32 +00:00
Let's open `file1.js` from the terminal again:
2021-01-16 16:31:56 +00:00
2020-12-10 10:20:52 +00:00
```bash
2020-07-21 13:34:32 +00:00
vim file1.js
```
2021-01-16 16:31:56 +00:00
2021-01-18 22:56:29 +00:00
![one buffer displayed.png](./images/screen-one-buffer.png)
2020-07-21 13:34:32 +00:00
Earlier I said that you're looking at `file1.js` buffer. While that was correct, it was incomplete. You are looking at `file1.js` buffer displayed through **a window**. A window is what you are seeing a buffer through.
2020-09-16 18:24:13 +00:00
Don't quit Vim yet. Run:
2021-01-16 16:31:56 +00:00
2020-07-21 13:34:32 +00:00
```
:split file2.js
```
2021-01-18 22:56:29 +00:00
![split window horizontally](./images/screen-split-window.png)
2020-07-21 13:34:32 +00:00
Now you are looking at two buffers through **two windows**. The top window displays `file2.js` buffer. The bottom window displays `file1.js` buffer.
2021-01-16 16:31:56 +00:00
If you want to navigate between windows, use these shortcuts:
2020-07-21 13:34:32 +00:00
```
2021-01-16 16:31:56 +00:00
Ctrl-W H Moves the cursor to the left window
Ctrl-W J Moves the cursor to the window below
Ctrl-W K Moves the cursor to the window upper
Ctrl-W L Moves the cursor to the right window
2020-07-21 13:34:32 +00:00
```
Now run:
2021-01-16 16:31:56 +00:00
2020-07-21 13:34:32 +00:00
```
:vsplit file3.js
```
2021-01-18 22:56:29 +00:00
![split window vertically and horizontally](./images/screen-split-window-vertically-and-horizontally.png)
2020-07-21 13:34:32 +00:00
You are now seeing three windows displaying three buffers. The top left window displays `file3.js` buffer, the top right window displays `file2.js` buffer, and the bottom window displays `file1.js` buffer.
You can have multiple windows displaying the same buffer. While you're on the top left window, type:
```
:buffer file2.js
```
2021-01-18 22:56:29 +00:00
![split window vertically and horizontally with two file2.js](./images/screen-split-window-vertically-and-horizontally-two-file2.png)
2020-07-21 13:34:32 +00:00
2021-01-16 16:31:56 +00:00
Now both top left and top right windows are displaying `file2.js` buffer. If you start typing on the top left, you can see that the content on both top left and top right window are being updated in real-time.
To close the current window, you can run `Ctrl-W C` or type `:quit`. When you close a window, the buffer will still be there (run `:buffers` to confirm this).
2020-07-21 13:34:32 +00:00
2021-01-16 16:31:56 +00:00
Here are some useful normal-mode window commands:
2020-07-21 13:34:32 +00:00
```
2021-01-16 16:31:56 +00:00
Ctrl-W V Opens a new vertical split
Ctrl-W S Opens a new horizontal split
Ctrl-W C Closes a window
Ctrl-W O Makes the current window the only one on screen and closes other windows
2020-07-21 13:34:32 +00:00
```
2021-01-16 16:31:56 +00:00
And here is a list of useful window command-line commands:
2020-07-21 13:34:32 +00:00
```
:vsplit filename Split window vertically
2020-08-27 14:04:09 +00:00
:split filename Split window horizontally
2020-07-21 13:34:32 +00:00
:new filename Create new window
```
2021-01-16 16:31:56 +00:00
Take your time to understand them. For more, check out `:h window`.
2020-07-21 13:34:32 +00:00
2020-11-13 01:02:04 +00:00
## Tabs
2020-07-21 13:34:32 +00:00
2021-01-16 16:31:56 +00:00
A tab is a collection of windows. Think of it like a layout for windows. In most modern text editors (and modern internet browsers), a tab means an open file / page and when you close it, that file / page goes away. In Vim, a tab does not represent an open file. When you close a tab in Vim, you are not closing a file. You are only closing the layout. The data for those files are stored in-memory in buffers. The buffers are still there.
2020-07-21 13:34:32 +00:00
Let's see Vim tabs in action. Open `file1.js`:
2021-01-16 16:31:56 +00:00
2020-12-10 10:20:52 +00:00
```bash
2020-07-21 13:34:32 +00:00
vim file1.js
```
To open `file2.js` in a new tab:
```
:tabnew file2.js
```
2021-01-18 22:56:29 +00:00
![screen displays tab 2](./images/screen-tab2.png)
2020-07-21 13:34:32 +00:00
2021-01-16 16:31:56 +00:00
You can also let Vim autocomplete the file you want to open in a *new tab* by pressing `<Tab>` (no pun intended).
2020-07-21 13:34:32 +00:00
Below is a list of useful tab navigations:
2021-01-16 16:31:56 +00:00
2020-07-21 13:34:32 +00:00
```
2021-01-16 16:31:56 +00:00
:tabnew file.txt Open file.txt in a new tab
2020-07-21 13:34:32 +00:00
:tabclose Close the current tab
:tabnext Go to next tab
:tabprevious Go to previous tab
:tablast Go to last tab
:tabfirst Go to first tab
```
2021-01-16 16:31:56 +00:00
2020-09-19 16:46:34 +00:00
You can also run `gt` to go to next tab page (you can go to previous tab with `gT`). You can pass count as argument to `gt`, where count is tab number. To go to the third tab, do `3gt`.
2020-07-21 13:34:32 +00:00
2020-09-19 16:46:34 +00:00
One advantage of having multiple tabs is you can have different window arrangements in different tabs. Maybe you want your first tab to have 3 vertical windows and second tab to have a mixed horizontal and vertical windows layout. Tab is the perfect tool for the job!
2020-07-21 13:34:32 +00:00
2021-01-18 22:56:29 +00:00
![first tab with multiple windows](./images/tabs-file1js.png)
2020-07-21 13:34:32 +00:00
2021-01-18 22:56:29 +00:00
![second tab with multiple windows](./images/tabs-file2js.png)
2020-07-21 13:34:32 +00:00
To start Vim with multiple tabs, you can do this from the terminal:
2021-01-16 16:31:56 +00:00
2020-12-10 10:20:52 +00:00
```bash
2020-07-21 13:34:32 +00:00
vim -p file1.js file2.js file3.js
```
2021-01-16 16:31:56 +00:00
2020-11-14 20:44:37 +00:00
## Moving In 3D
2020-07-21 13:34:32 +00:00
2021-01-16 16:31:56 +00:00
Moving between windows is like traveling two-dimensionally along X-Y axis in a Cartesian coordinate. You can move to the top, right, bottom, and left window with `Ctrl-W H/J/K/L`.
2020-07-21 13:34:32 +00:00
2021-01-18 22:56:29 +00:00
![cartesian movement in x and y axis](./images/cartesian-xy.png)
2020-07-21 13:34:32 +00:00
2021-01-16 16:31:56 +00:00
Moving between buffers is like traveling across the Z axis in a Cartesian coordinate. Imagine your buffer files lining up across the Z axis. You can traverse the Z axis one buffer at a time with `:bnext` and `:bprevious`. You can jump to any coordinate in Z axis with `:buffer filename/buffernumber`.
2020-07-21 13:34:32 +00:00
2021-01-18 22:56:29 +00:00
![cartesian movement in z axis](./images/cartesian-z.png)
2020-07-21 13:34:32 +00:00
2020-08-27 14:04:09 +00:00
You can move in *three-dimensional space* by combining window and buffer movements. You can move to the top, right, bottom, or left window (X-Y navigations) with window navigations. Since each window contains buffers, you can move forward and backward (Z navigations) with buffer movements.
2020-07-21 13:34:32 +00:00
2021-01-18 22:56:29 +00:00
![cartesian movement in x, y, and z axis](./images/cartesian-xyz.png)
2020-07-21 13:34:32 +00:00
2020-11-14 20:44:37 +00:00
## Using Buffers, Windows, and Tabs The Smart Way
2020-07-21 13:34:32 +00:00
2021-01-16 16:31:56 +00:00
You have learned what buffers, windows, and tabs are and how they work in Vim. Now that you understand them better, you can use them in your own workflow.
2020-07-21 13:34:32 +00:00
2021-01-16 16:31:56 +00:00
Everyone has a different workflow, here is mine for example:
- First I use buffers to store all the required files for the current task. Vim can handle many open buffers before it starts slowing down. Plus having many buffers opened won't crowd my screen. I am only seeing one buffer (assuming I have only one window) at any time, allowing me to focus on one screen. When I need to go somewhere, I can quickly fly to any open buffer anytime.
- I use multiple windows to view multiple buffers at once, usually when diffing files, reading docs, or following a code flow. I try to keep the number of windows opened to no more than three because my screen will get crowded (I use a small laptop). When I am done, I close any extra windows. Fewer windows means less distractions.
- Instead of tabs, I use [tmux](https://github.com/tmux/tmux/wiki) windows. I usually use multiple tmux windows at once. For example, one tmux window for client-side codes and another for backend codes.
2020-07-21 13:34:32 +00:00
2021-01-16 16:31:56 +00:00
My workflow may look different than yours based on your editing style and that's fine. Experiment around to discover your own flow suited for your coding style.