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).
- `:bnext` to go to the next buffer (`:bprevious` to go to the previous buffer).
@ -75,7 +75,7 @@ To save and quit all, run:
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:
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.
Now you are looking at two buffers through **two windows**. The top window displays `file2.js` buffer. The bottom window displays `file1.js` buffer.
@ -112,7 +112,7 @@ Now run:
:vsplit file3.js
```
![split window vertically and horizontally](images/screen-split-window-vertically-and-horizontally.png)
![split window vertically and horizontally](screen-split-window-vertically-and-horizontally.png)
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.
@ -121,7 +121,7 @@ You can have multiple windows displaying the same buffer. While you're on the to
```
:buffer file2.js
```
![split window vertically and horizontally with two file2.js](images/screen-split-window-vertically-and-horizontally-two-file2.png)
![split window vertically and horizontally with two file2.js](screen-split-window-vertically-and-horizontally-two-file2.png)
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.
@ -162,7 +162,7 @@ To open `file2.js` in a new tab:
:tabnew file2.js
```
![screen displays tab 2](images/screen-tab2.png)
![screen displays tab 2](screen-tab2.png)
You can also let Vim autocomplete the file you want to open in a *new tab* by pressing `<Tab>` (no pun intended).
@ -181,9 +181,9 @@ You can also run `gt` to go to next tab page (you can go to previous tab with `g
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!
![first tab with multiple windows](images/tabs-file1js.png)
![first tab with multiple windows](tabs-file1js.png)
![second tab with multiple windows](images/tabs-file2js.png)
![second tab with multiple windows](tabs-file2js.png)
To start Vim with multiple tabs, you can do this from the terminal:
@ -195,15 +195,15 @@ vim -p file1.js file2.js file3.js
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`.
![cartesian movement in x and y axis](images/cartesian-xy.png)
![cartesian movement in x and y axis](cartesian-xy.png)
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`.
![cartesian movement in z axis](images/cartesian-z.png)
![cartesian movement in z axis](cartesian-z.png)
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.
![cartesian movement in x, y, and z axis](images/cartesian-xyz.png)
![cartesian movement in x, y, and z axis](cartesian-xyz.png)
@ -254,9 +254,7 @@ You can mix and match these options. For example, `^hello | ^welcome friends$` w
To search for files inside Vim using fzf.vim plugin, you can use the `:Files` method. Run `:Files` from Vim and you will be prompted with fzf search prompt.
<palign="center">
<imgalt="Finding files in fzf"width="900"height="auto"src="images/fzf-files.gif"/>
</p>
![Finding files in fzf](fzf-files.gif)
Since you will be using this command frequently, it is good to have this mapped. I map mine to `Ctrl-f`. In my vimrc, I have this:
<imgalt="Basic diffing with Vim"width="900"height="auto"src="images/diffing-basic.png">
</p>
![Basic diffing with Vim](diffing-basic.png)
`vimdiff` displays two buffers side-by-side. On the left is `file1.txt` and on the right is `file2.txt`. The first differences (apples and oranges) are highlighted on both lines.
Suppose you want to make the second buffer to have apples, not oranges. To transfer the content from your current position (you're currently on `file1.txt`) to `file2.txt`, first go to the next diff with `]c` (to jump to the previous diff window, use `[c`). The cursor should be on apples now. Run `:diffput`. Both files should now have apples.
<palign="center">
<imgalt="Finding files in FZF"width="900"height="auto"src="images/diffing-apples.png">
</p>
![Diffing apples](diffing-apples.png)
If you need to transfer the text from the other buffer (orange juice, `file2.txt`) to replace the text on the current buffer (apple juice, `file1.txt`), with your cursor still on `file1.txt` window, first go to the next diff with `]c`. Your cursor now should be on apple juice. Run `:diffget` to get the orange juice from another buffer to replace apple juice in our buffer.
@ -175,9 +171,7 @@ A conflict, great! Let's resolve the conflict using our newly-configured `merget
git mergetool
```
<palign="center">
<imgalt="Three-way mergetool with Vim"width="900"height="auto"src="images/mergetool-initial.png">
</p>
![Three-way mergetool with Vim](mergetool-initial.png)
Vim displays four windows. Pay attention to the top three:
@ -276,9 +270,7 @@ When you run the `:Git` command without any parameters, vim-fugitive displays a
- `u` to unstage the file name under the cursor.
- `>` / `<` to display or hide an inline diff of the file name under the cursor.
<palign="center">
<imgalt="Finding files in FZF"width="900"height="auto"src="images/fugitive-git.png">
</p>
![Fugitive Git](fugitive-git.png)
For more, check out `:h fugitive-staging-maps`.
@ -294,17 +286,13 @@ Some things you can do while in this `"git blame"` mode:
For more, check out `:h :Git_blame`.
<palign="center">
<imgalt="Finding files in FZF"width="900"height="auto"src="images/fugitive-git-blame.png">
</p>
![Fugitive Git Blame](fugitive-git-blame.png)
## Gdiffsplit
When you run the `:Gdiffsplit` command, vim-fugitive runs a `vimdiff` of the current file's latest changes against the index or work tree. If you run `:Gdiffsplit <commit>`, vim-fugitive runs a `vimdiff` against that file inside `<commit>`.
<palign="center">
<imgalt="Finding files in FZF"width="900"height="auto"src="images/fugitive-gdiffsplit.png">
</p>
![Fugitive Gdiffsplit](fugitive-gdiffsplit.png)
Because you are in a `vimdiff` mode, you can *get* or *put* the diff with `:diffput` and `:diffget`.
@ -318,9 +306,7 @@ When you run the `:Gread` command in a file after you make changes, vim-fugitive
When you run the `:Gclog` command, vim-fugitive displays the commit history. It is like running the `git log` command. Vim-fugitive uses Vim's quickfix to accomplish this, so you can use `:cnext` and `:cprevious` to traverse to the next or previous log information. You can open and close the log list with `:copen` and `:cclose`.
<palign="center">
<imgalt="Finding files in FZF"width="900"height="auto"src="images/fugitive-git-log.png">
</p>
![Fugitive Git Log](fugitive-git-log.png)
While in this `"git log"` mode, you can do two things: