Edit ch20

pull/62/head
Igor Irianto 3 years ago
parent 2440c510d9
commit dc23c6857a

@ -1,14 +1,12 @@
# Ch20. Views, Sessions, And Viminfo
After you worked on a project for a while, you may find that the project to gradually take shape with its own settings, folds, buffers, layouts, etc. It's like decorating your apartment to make it feel like home.
After you worked on a project for a while, you may find the project to gradually take shape with its own settings, folds, buffers, layouts, etc. It's like decorating your apartment after living in it for a while. The problem is, when you close Vim, you lose those changes. Wouldn't it be nice if you can keep those changes so the next time you open Vim, it looks just like you had never left?
The problem is, when you close Vim, you lose those changes. Wouldn't it be nice if you can keep those changes so the next time you open Vim, it looks just like you had never left?
In this chapter, you will learn how use View, Session, and Viminfo to preserve a "snapshot" of your projects.
Vim has three tools for this: View, Session, and Viminfo. In this chapter, you will learn how to leverage all three to take a "snapshot" of your project.
## View
## Views
A View is the smallest subset of the three (View, Session, Viminfo). A Vim View is a collection of settings for one window. If you spend a long time working on a window and you want to preserve the mappings and folds, you can use a View.
A View is the smallest subset of the three (View, Session, Viminfo). It is a collection of settings for one window. If you spend a long time working on a window and you want to preserve the maps and folds, you can use a View.
Let's create a file called `foo.txt`:
@ -26,9 +24,8 @@ foo10
```
In this file, create three changes:
1. On line 1, create a manual fold `zf4j` (fold the next 4 lines).
2. Change the `number` setting: `setlocal nonumber norelativenumber`.
2. Change the `number` setting: `setlocal nonumber norelativenumber`. This will remove the number indicators on the left side of the window.
3. Create a local mapping to go down two lines each time you press `j` instead of one: `:nnoremap <buffer> j jj`.
Your file should look like this:
@ -42,9 +39,7 @@ foo9
foo10
```
Now, your display won't have any numbers on the left side and pressing the `j` key goes down two lines instead of one line.
## Configuring View Attributes
### Configuring View Attributes
Run:
@ -52,13 +47,13 @@ Run:
:set viewoptions?
```
By default it should say:
By default it should say (yours may look different depending on your vimrc):
```
viewoptions=folds,cursor,curdir
```
Yours might look different. Let's configure `viewoptions`. The three attributes you want to preserve are the folds, the mapping, and the local set option. If your setting looks like mine, you already have the `folds` option. You need to tell View to remember the `localoptions`. Run:
Let's configure `viewoptions`. The three attributes you want to preserve are the folds, the maps, and the local set options. If your setting looks like mine, you already have the `folds` option. You need to tell View to remember the `localoptions`. Run:
```
:set viewoptions+=localoptions
@ -70,7 +65,7 @@ To learn what other options are available for `viewoptions`, check out `:h viewo
viewoptions=folds,cursor,curdir,localoptions
```
## Saving The View
### Saving The View
With the `foo.txt` window properly folded and having `nonumber norelativenumber` options, let's save the View. Run:
@ -80,7 +75,7 @@ With the `foo.txt` window properly folded and having `nonumber norelativenumber`
Vim creates a View file.
## View Files
### View Files
You might wonder, "Where did Vim save this View file?" To see where Vim saves it, run:
@ -94,7 +89,7 @@ The default should say `~/.vim/view` (if you have a different OS, it might show
set viewdir=$HOME/else/where
```
## Loading The View File
### Loading The View File
Close the `foo.txt` if you haven't, then open `foo.txt` again. You should see the original text without the changes. That's expected. Let's load the View file. Run:
@ -115,7 +110,7 @@ foo10
The folds, local settings, and local mappings are restored. If you notice, your cursor should also be on the line where you left it when you ran `:mkview`. As long as you have the `cursor` option, View also remembers your cursor position.
## Multiple Views
### Multiple Views
Vim lets you save 9 numbered Views (1-9).
@ -149,18 +144,18 @@ To load the original View, run:
:loadview
```
## Automating View Creation
### Automating View Creation
One of the worst things that can happen is, after spending countless hours organizing a large file with folds, you accidentally close the window and lose all fold information. To prevent this, you might want to automatically create a View each time you close a buffer. Add this in your vimrc:
```
au BufWinLeave *.txt mkview
autocmd BufWinLeave *.txt mkview
```
Additionally, it might be nice to load View when you open a buffer:
```
au BufWinEnter *.txt silent loadview
autocmd BufWinEnter *.txt silent loadview
```
Now you don't have to worry about creating and loading View anymore when you are working with `txt` files. Keep in mind that over time, your `~/.vim/view` might start to accumulate View files. It's good to clean it up once every few months.
@ -169,11 +164,11 @@ Now you don't have to worry about creating and loading View anymore when you are
If a View saves the settings of a window, a Session saves the information of all windows (including the layout).
## Creating A New Session
### Creating A New Session
Suppose you are working with these 3 files in a `foobarbaz` project:
`foo.txt`:
Inside `foo.txt`:
```
foo1
@ -188,7 +183,7 @@ foo9
foo10
```
`bar.txt`:
Inside `bar.txt`:
```
bar1
@ -203,7 +198,7 @@ bar9
bar10
```
and `baz.txt`:
Inside `baz.txt`:
```
baz1
@ -218,11 +213,11 @@ baz9
baz10
```
And you've been working on these 3 files for a while, so that your windows layout look like (using strategically placed `split` and `vsplit`):
Let's say that your windows layout look like the following (using strategically placed `split` and `vsplit`):
![Session Layout](images/session-layout.png)
To save the Session, run:
To preserve this look, you need to save the Session. Run:
```
:mksession
@ -238,7 +233,7 @@ If you want to save the Session file somewhere else, you can pass an argument to
If you want to overwrite the existing Session file, call the command with a `!` (`:mksession! ~/some/where/else.vim`).
## Loading A Session
### Loading A Session
To load a Session, run:
@ -246,13 +241,13 @@ To load a Session, run:
:source Session.vim
```
`Session.vim` is the Session file. Now Vim looks like the way you left it. Alternatively, you can also load a Session file from the terminal:
Now Vim looks like just the way you left it! Alternatively, you can also load a Session file from the terminal:
```
vim -S Session.vim
```
## Configuring Session Attributes
### Configuring Session Attributes
You can configure the attributes Session saves. To see what is currently being saved, run:
@ -266,9 +261,7 @@ Mine says:
blank,buffers,curdir,folds,help,tabpages,winsize,terminal
```
Yours might look different. To add or remove an option, you can use `+=` or `=`.
If you don't want to save `terminal` when you save a Session, run:
If you don't want to save `terminal` when you save a Session, remove it from the session options. Run:
```
:set sessionoptions-=terminal
@ -281,7 +274,6 @@ If you want to add an `options` when you save a Session, run:
```
Here are some attributes that `sessionoptions` can store:
- `blank` stores empty windows
- `buffers` stores buffers
- `folds` stores folds
@ -295,20 +287,17 @@ Here are some attributes that `sessionoptions` can store:
For the complete list check out `:h 'sessionoptions'`.
Session is a useful tool to preserve your project's external attributes. However, there are a number of internal attributes that Session doesn't save: local marks, registers, histories, etc.
To save them, you need to use Viminfo!
Session is a useful tool to preserve your project's external attributes. However, some internal attributes aren't saved by Session, like local marks, registers, histories, etc. To save them, you need to use Viminfo!
## Viminfo
After yanking a word into register a and quitting Vim, the next time you open Vim you can immediately paste the text from register a. This is actually one of Viminfo's features. Without it, Vim won't remember the register after you close.
If you notice, after yanking a word into register "a and quitting Vim, the next time you open Vim you still that text stored in the register. This is actually a work of Viminfo. Without it, Vim won't remember the register after you close Vim.
If you use Vim 8 or higher, Vim enables Viminfo by default, so you may have been using Vim info this whole time without knowing it!
If you use Vim 8 or higher, Vim enables Viminfo by default, so you may have been using Viminfo this whole time without knowing it!
You might ask: "What does Viminfo save? How does it differ from Session?"
You just learned the things that Session saves from `sessionoptions`. To use Viminfo, first you need to have `+viminfo` feature available (`:version`). Viminfo stores:
To use Viminfo, first you need to have `+viminfo` feature available (`:version`). Viminfo stores:
- The command-line history.
- The search string history.
- The input-line history.
@ -319,7 +308,7 @@ You just learned the things that Session saves from `sessionoptions`. To use Vim
- The buffer list.
- Global variables.
When comparing the items that can be saved for Session and Viminfo, you will notice that `sessionoptions`, in general, saves the "external" attributes while `viminfo` saves the "internal" attributes.
In general, Session stores the "external" attributes and Viminfo the "internal" attributes.
Unlike Session where you can have one Session file per project, you normally will use one Viminfo file per computer. Viminfo is project-agnostic.
@ -327,7 +316,7 @@ The default Viminfo location for Unix is `$HOME/.viminfo` (`~/.viminfo`). If you
*Make sure that you have `nocompatible` option set (`set nocompatible`), otherwise your Viminfo will not work.*
## Writing And Reading Viminfo
### Writing And Reading Viminfo
Although you will use only one Viminfo file, you can create multiple Viminfo files. To write a Viminfo file, use the `:wviminfo` command (`:wv` for short).
@ -347,13 +336,13 @@ By default Vim will read from `~/.viminfo` file. To read from a different Viminf
:rv ~/.viminfo_extra
```
You can also start Vim with a different Viminfo file from the terminal with the `i` flag:
To start Vim with a different Viminfo file from the terminal, use the `i` flag:
```
vim -i viminfo_extra
```
Multiple Viminfo files can be used to create several different settings for different text editing needs:
If you use Vim for different tasks, like coding and writing, you can create a Viminfo optimized for writing and another for coding.
```
vim -i viminfo_writing
@ -361,9 +350,9 @@ vim -i viminfo_writing
vim -i viminfo_coding
```
## Starting Vim Without Viminfo
### Starting Vim Without Viminfo
To start Vim without Viminfo data, you can run from the terminal:
To start Vim without Viminfo, you can run from the terminal:
```
vim -i NONE
@ -375,7 +364,7 @@ To make it permanent, you can add this in your vimrc file:
set viminfo="NONE"
```
## Configuring Viminfo Attributes
### Configuring Viminfo Attributes
Similar to `viewoptions` and `sessionoptions`, you can instruct what attributes to save with the `viminfo` option. Run:
@ -390,17 +379,16 @@ You will get:
```
This looks cryptic. Let's break it down:
- `!` saves global variables that start with an uppercase letter and don't contain lowercase letters. Recall that `g:` indicates a global variable. For example, if you call `:let g:FOO = "foo"`, Viminfo will save the global variable `FOO`. However if you do `:let g:Foo = "foo"`, Viminfo will not save this global variable because it contains lowercase letters. Without `!`, Vim won't safe those global variables.
- `'100` represents marks. In this case, Viminfo will save the local marks (a-z) of the last 100 files. Be aware that if you tell Viminfo to save too many files, Vim can start slowing down. 100-1000 files is a good range.
- `<50` tells Viminfo how many lines are saved for each registers (50 or less in this case). If I yank 100 lines of text into register a (`"ay99j`) and close Vim, the next time I open Vim and paste from register a (`"ap`), Vim will only paste 50 lines max. If you don't give it anything, *all* lines will be saved (it might slow down Vim). If you give it a 0, nothing will be saved.
- `!` saves global variables that start with an uppercase letter and don't contain lowercase letters. Recall that `g:` indicates a global variable. For example, if at some point you wrote the assignment `let g:FOO = "foo"`, Viminfo will save the global variable `FOO`. However if you did `let g:Foo = "foo"`, Viminfo will not save this global variable because it contains lowercase letters. Without `!`, Vim won't safe those global variables.
- `'100` represents marks. In this case, Viminfo will save the local marks (a-z) of the last 100 files. Be aware that if you tell Viminfo to save too many files, Vim can start slowing down. 1000 is a good number to have.
- `<50` tells Viminfo how many maximum lines are saved for each registers (50 in this case). If I yank 100 lines of text into register a (`"ay99j`) and close Vim, the next time I open Vim and paste from register a (`"ap`), Vim will only paste 50 lines max. If you don't give maximum line number, *all* lines will be saved. If you give it 0, nothing will be saved.
- `s10` sets a size limit (in kb) for a register. In this case, any register greater than 10kb size will be excluded.
- `h` disables highlighting (from `hlsearch`) when Vim starts.
There are other options. To learn more, check out `:h 'viminfo'`.
There are other options that you can pass. To learn more, check out `:h 'viminfo'`.
## Using Views, Sessions, And Viminfo The Smart Way
Vim has View, Session, and Viminfo to take different level of "snapshot" of your Vim environment. For small projects, use Views. For larger projects, use Sessions. You should take your time to check out all the options that View, Session, and Viminfo offers.
Vim has View, Session, and Viminfo to take different level of your Vim environment snapshots. For micro projects, use Views. For larger projects, use Sessions. You should take your time to check out all the options that View, Session, and Viminfo offers.
Create your own View, Session, and Viminfo for your own editing style. Store them in your dotfiles. If you ever need to use Vim outside of your computer, you can just load your settings and you will immediately feel at home!
Create your own View, Session, and Viminfo for your own editing style. If you ever need to use Vim outside of your computer, you can just load your settings and you will immediately feel at home!

Loading…
Cancel
Save