scratch/content/html/en/blog/Learn-Vim-Progressively.md

350 lines
12 KiB
Markdown
Raw Normal View History

2011-08-26 13:48:47 +00:00
-----
isHidden: false
menupriority: 1
kind: article
created_at: 2011-08-25T19:28:20+02:00
title: Learn Vim Progressively
author_name: Yann Esposito
author_uri: yannesposito.com
tags:
- vi
- vim
- editor
- tutorial
2011-10-26 08:49:00 +00:00
- learn
2011-08-26 13:48:47 +00:00
-----
blogimage("uber_leet_use_vim.jpg","Über leet use vim!")
2011-08-26 13:48:47 +00:00
begindiv(intro)
2012-06-05 12:30:15 +00:00
%tldr You want to teach yourself vim (the best text editor known to human kind) in the fastest way possible. This my way of doing it. You start by learning the minimal to survive, then you integrate all the tricks slowly.
2011-08-26 13:48:47 +00:00
enddiv
[Vim] the Six Billion Dollar editor
> Better, Stronger, Faster.
Learn [vim] and it will be your last text editor.
2012-06-05 12:30:15 +00:00
There isn't any better text editor that I know of.
It is hard to learn, but incredible to use.
2011-08-26 13:48:47 +00:00
2012-06-05 12:30:15 +00:00
I suggest you teach yourself Vim in 4 steps:
2011-08-26 13:48:47 +00:00
1. Survive
2. Feel comfortable
3. Feel Better, Stronger, Faster
2012-06-05 12:30:15 +00:00
4. Use superpowers of vim
2011-08-26 13:48:47 +00:00
By the end of this journey, you'll become a vim superstar.
But before we start, just a warning.
Learning vim will be painful at first.
2011-08-26 14:32:49 +00:00
It will take time.
2012-06-05 12:30:15 +00:00
It will be a lot like playing a musical instrument.
2011-08-26 13:48:47 +00:00
Don't expect to be more efficient with vim than with another editor in less than 3 days.
In fact it will certainly take 2 weeks instead of 3 days.
[Vim]: http://www.vim.org
[vim]: http://www.vim.org
## 1st Level -- Survive
0. Install [vim]
1. Launch vim
2. DO NOTHING! Read.
In a standard editor, typing on the keyboard is enough to write something and see it on the screen.
Not this time.
2011-08-29 13:14:17 +00:00
Vim is in _Normal_ mode.
2012-06-05 12:30:15 +00:00
Let's go to _Insert_ mode.
Type the letter `i`.
2011-08-26 13:48:47 +00:00
2011-08-26 14:32:49 +00:00
You should feel a bit better.
2012-06-05 12:30:15 +00:00
You can type letters like in a standard editor.
To get back to _Normal_ mode just press the `ESC` key.
2011-08-26 13:48:47 +00:00
2012-06-05 12:30:15 +00:00
You now know how to switch between _Insert_ and _Normal_ mode.
And now, here are the commands that you need in order to survive in _Normal_ mode:
2011-08-26 13:48:47 +00:00
2011-08-29 21:13:33 +00:00
> - `i` → _Insert_ mode. Type `ESC` to return to Normal mode.
> - `x` → Delete the char under the cursor
> - `:wq` → Save and Quit (`:w` save, `:q` quit)
2012-06-05 12:30:15 +00:00
> - `dd` → Delete (and copy) the current line
2011-08-29 21:13:33 +00:00
> - `p` → Paste
2011-08-26 14:32:49 +00:00
>
> Recommended:
>
2012-06-05 12:30:15 +00:00
> - `hjkl` (highly recommended but not mandatory) → basic cursor move (<-&darr;&uarr;→). Hint: `j` looks like a down arrow.
> - `:help <command>` → Show help about `<command>`. You can use `:help` without a `<command>` to get general help.
2011-08-26 13:48:47 +00:00
2012-06-05 12:30:15 +00:00
Only 5 commands. That is all you need to get started.
Once these command start to become natural (maybe after day or so), you should move on to level 2.
2011-08-26 13:48:47 +00:00
2012-06-05 12:30:15 +00:00
But first, just a little remark about _Normal mode_.
2011-08-26 14:32:49 +00:00
In standard editors, to copy you have to use the `Ctrl` key (`Ctrl-c` generally).
2012-06-05 12:30:15 +00:00
In fact, when you press `Ctrl`, it is as if all of your keys change meaning.
Using vim in normal mode is a bit like having the editor automatically press the `Ctrl` key for you.
2011-08-26 13:48:47 +00:00
2011-08-29 21:13:33 +00:00
A last word about notations:
- instead of writing `Ctrl-λ`, I'll write `<C-λ>`.
2012-06-05 12:30:15 +00:00
- commands starting with `:` end with `<enter>`. For example, when I write `:q`, I mean `:q<enter>`.
2011-08-26 13:48:47 +00:00
## 2nd Level -- Feel comfortable
2011-08-29 13:31:54 +00:00
You know the commands required for survival.
It's time to learn a few more commands.
2012-06-05 12:30:15 +00:00
These are my suggestions:
2011-08-26 13:48:47 +00:00
2011-08-29 13:14:17 +00:00
1. Insert mode variations:
2011-08-26 13:48:47 +00:00
2011-08-26 14:39:45 +00:00
> - `a` → insert after the cursor
2011-08-26 13:48:47 +00:00
> - `o` → insert a new line after the current one
> - `O` → insert a new line before the current one
2012-06-05 12:30:15 +00:00
> - `cw` → replace from the cursor to the end of the word
2011-08-26 13:48:47 +00:00
2. Basic moves
2012-06-05 12:30:15 +00:00
> - `0` → go to the first column
> - `^` → go to the first non-blank character of the line
2011-08-26 13:48:47 +00:00
> - `$` → go to the end of line
2011-08-29 14:35:13 +00:00
> - `g_` → go to the last non-blank character of line
2011-08-26 13:48:47 +00:00
> - `/pattern` → search for `pattern`
3. Copy/Paste
2011-08-29 11:39:13 +00:00
> - `P` → paste before, remember `p` is paste after current position.
2012-06-05 12:30:15 +00:00
> - `yy` → copy the current line, easier but equivalent to `ddP`
2011-08-26 13:48:47 +00:00
4. Undo/Redo
2011-08-29 12:03:03 +00:00
> - `u` → undo
2011-08-29 13:05:28 +00:00
> - `<C-r>` → redo
2011-08-26 13:48:47 +00:00
5. Load/Save/Quit/Change File (Buffer)
2011-08-29 12:03:03 +00:00
> - `:e <path/to/file>` → open
> - `:w` → save
> - `:saveas <path/to/file>` → save to `<path/to/file>`
2011-08-31 08:55:25 +00:00
> - `:x`, `ZZ` or `:wq` → save and quit (`:x` only save if necessary)
2012-06-05 12:30:15 +00:00
> - `:q!` → quit without saving, also: `:qa!` to quit even if there are modified hidden buffers.
2011-08-29 12:03:03 +00:00
> - `:bn` (resp. `:bp`) → show next (resp. previous) file (buffer)
2011-08-26 13:48:47 +00:00
2012-06-05 12:30:15 +00:00
Take the time to learn all of these command.
Once done, you should be able to do every thing you are able to do in other editors.
You may still feel a bit awkward. But follow me to the next level and you'll see why vim is worth the extra work.
2011-08-26 13:48:47 +00:00
## 3rd Level -- Better. Stronger. Faster.
2012-06-05 12:30:15 +00:00
Congratulation for reaching this far!
Now we can start with the interesting stuff.
At level 3, we'll only talk about commands which are compatible with the old vi editor.
2011-08-26 13:48:47 +00:00
### Better
2011-08-30 06:07:50 +00:00
Let's look at how vim could help you to repeat yourself:
2011-08-26 13:48:47 +00:00
1. `.` → (dot) will repeat the last command,
2012-06-05 12:30:15 +00:00
2. N&lt;command&gt; → will repeat the command N times.
2011-08-26 13:48:47 +00:00
Some examples, open a file and type:
> - `2dd` → will delete 2 lines
> - `3p` → will paste the text 3 times
> - `100idesu [ESC]` → will write "desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu "
> - `.` → Just after the last command will write again the 100 "desu ".
> - `3.` → Will write 3 "desu" (and not 300, how clever).
### Stronger
Knowing how to move efficiently with vim is _very_ important.
Don't skip this section.
1. N`G` → Go to line N
2012-06-05 12:30:15 +00:00
2. `gg` → shortcut for `1G` - go to the start of the file
2011-08-26 13:48:47 +00:00
3. `G` → Go to last line
4. Word moves:
> 1. `w` → go to the start of the following word,
> 2. `e` → go to the end of this word.
>
2012-06-05 12:30:15 +00:00
> By default, words are composed of letters and the underscore character.
2011-08-29 21:13:33 +00:00
> Let's call a WORD a group of letter separated by blank characters.
2012-06-05 12:30:15 +00:00
> If you want to consider WORDS, then just use uppercase characters:
2011-08-26 13:48:47 +00:00
>
2011-08-29 21:13:33 +00:00
> 1. `W` → go to the start of the following WORD,
> 2. `E` → go to the end of this WORD.
2011-08-26 13:48:47 +00:00
>
> blogimage("word_moves.jpg","Word moves example")
2011-08-26 13:48:47 +00:00
2011-08-29 14:35:13 +00:00
Now let's talk about very efficient moves:
2011-08-26 13:48:47 +00:00
2012-06-05 12:30:15 +00:00
> - `%` : Go to the corresponding `(`, `{`, `[`.
2011-08-26 13:48:47 +00:00
> - `*` (resp. `#`) : go to next (resp. previous) occurrence of the word under the cursor
2011-08-29 13:31:54 +00:00
Believe me, the last three commands are gold.
2011-08-26 13:48:47 +00:00
### Faster
Remember about the importance of vi moves?
Here is the reason.
Most commands can be used using the following general format:
`<start position><command><end position>`
For example : `0y$` means
- `0` → go to the beginning of this line
- `y` → yank from here
- `$` → up to the end of this line
We also can do things like `ye`, yank from here to the end of the word.
But also `y2/foo` yank up to the second occurrence of "foo".
But what was true for `y` (yank),
is also true for `d` (delete), `v` (visual select), `gU` (uppercase), `gu` (lowercase), etc...
## 4th Level -- Vim Superpowers
2012-06-05 12:30:15 +00:00
With all preceding commands you should be comfortable using vim.
2011-08-26 13:48:47 +00:00
But now, here are the killer features.
Some of these features were the reason I started to use vim.
2011-09-08 15:49:59 +00:00
### Move on current line: `0` `^` `$` `g_` `f` `F` `t` `T` `,` `;`
2011-08-26 13:48:47 +00:00
> - `0` → go to column 0
> - `^` → go to first character on the line
2011-09-08 15:45:10 +00:00
> - `$` → go to the last column
> - `g_` → go to the last character on the line
2012-06-05 12:30:15 +00:00
> - `fa` → go to next occurrence of the letter `a` on the line. `,` (resp. `;`) will find the next (resp. previous) occurrence.
> - `t,` → go to just before the character `,`.
> - `3fa` → find the 3rd occurrence of `a` on this line.
2011-08-26 13:48:47 +00:00
> - `F` and `T` → like `f` and `t` but backward.
> blogimage("line_moves.jpg","Line moves")
2011-08-26 13:48:47 +00:00
2011-08-29 21:13:33 +00:00
A useful tip is: `dt"` → remove everything until the `"`.
### Zone selection `<action>a<object>` or `<action>i<object>`
2012-06-05 12:30:15 +00:00
These command can only be used after an operator in visual mode.
2011-08-29 21:13:33 +00:00
But they are very powerful. Their main pattern is:
`<action>a<object>` and `<action>i<object>`
Where action can be any action, for example, `d` (delete), `y` (yank), `v` (select in visual mode).
2012-06-05 12:30:15 +00:00
The object can be: `w` a word, `W` a WORD (extended word), `s` a sentence, `p` a paragraph. But also, natural character such as `"`, `'`, `)`, `}`, `]`.
2011-08-26 13:48:47 +00:00
2011-08-29 21:13:33 +00:00
Suppose the cursor is on the first `o` of `(map (+) ("foo"))`.
> - `vi"` → will select `foo`.
> - `va"` → will select `"foo"`.
> - `vi)` → will select `"foo"`.
> - `va)` → will select `("foo")`.
> - `v2i)` → will select `map (+) ("foo")`
> - `v2a)` → will select `(map (+) ("foo"))`
blogimage("textobjects.png","Text objects selection")
2011-08-26 13:48:47 +00:00
2011-08-29 14:35:13 +00:00
### Select rectangular blocks: `<C-v>`.
2011-08-26 13:48:47 +00:00
2012-06-05 12:30:15 +00:00
Rectangular blocks are very useful for commenting many lines of code.
2011-08-29 14:35:13 +00:00
Typically: `0<C-v><C-d>I-- [ESC]`
2011-08-26 13:48:47 +00:00
- `^` → go to start of the line
2011-08-29 13:41:15 +00:00
- `<C-v>` → Start block selection
2011-08-29 13:05:28 +00:00
- `<C-d>` → move down (could also be `jjj` or `%`, etc...)
2011-08-29 14:35:13 +00:00
- `I-- [ESC]` → write `-- ` to comment each line
2011-08-26 13:48:47 +00:00
blogimage("rectangular-blocks.gif","Rectangular blocks")
2011-08-26 13:48:47 +00:00
2012-06-05 12:30:15 +00:00
Note: in Windows you might have to use `<C-q>` instead of `<C-v>` if your clipboard is not empty.
2011-08-29 13:41:15 +00:00
2011-08-29 13:05:28 +00:00
### Completion: `<C-n>` and `<C-p>`.
2011-08-26 13:48:47 +00:00
2011-08-29 13:05:28 +00:00
In Insert mode, just type the start of a word, then type `<C-p>`, magic...
blogimage("completion.gif","Completion")
2011-08-26 13:48:47 +00:00
### Macros : `qa` do something `q`, `@a`, `@@`
`qa` record your actions in the _register_ `a`.
Then `@a` will replay the macro saved into the register `a` as if you typed it.
`@@` is a shortcut to replay the last executed macro.
> *Example*
>
> On a line containing only the number 1, type this:
>
2011-08-29 13:05:28 +00:00
> - `qaYp<C-a>q` →
2011-08-26 13:48:47 +00:00
>
> - `qa` start recording.
> - `Yp` duplicate this line.
2011-08-29 13:05:28 +00:00
> - `<C-a>` increment the number.
2011-08-26 13:48:47 +00:00
> - `q` stop recording.
>
> - `@a` → write 2 under the 1
> - `@@` → write 3 under the 2
> - Now do `100@@` will create a list of increasing numbers until 103.
blogimage("macros.gif","Macros")
2011-08-26 13:48:47 +00:00
2011-08-29 13:05:28 +00:00
### Visual selection: `v`,`V`,`<C-v>`
2011-08-26 13:48:47 +00:00
2011-08-29 13:41:15 +00:00
We saw an example with `<C-v>`.
2011-08-26 13:48:47 +00:00
There is also `v` and `V`.
2012-06-05 12:30:15 +00:00
Once the selection has been made, you can:
2011-08-26 13:48:47 +00:00
2012-06-05 12:30:15 +00:00
- `J` → join all the lines together.
2011-08-26 13:48:47 +00:00
- `<` (resp. `>`) → indent to the left (resp. to the right).
- `=` → auto indent
blogimage("autoindent.gif","Autoindent")
2011-08-26 13:48:47 +00:00
2011-08-29 13:31:54 +00:00
Add something at the end of all visually selected lines:
2011-08-26 13:48:47 +00:00
2011-08-29 13:05:28 +00:00
- `<C-v>`
- go to desired line (`jjj` or `<C-d>` or `/pattern` or `%` etc...)
2012-06-05 12:30:15 +00:00
- `$` go to the end of the line
2011-08-29 13:31:54 +00:00
- `A`, write text, `ESC`.
2011-08-26 13:48:47 +00:00
blogimage("append-to-many-lines.gif","Append to many lines")
2011-08-26 13:48:47 +00:00
2011-08-26 14:39:45 +00:00
### Splits: `:split` and `vsplit`.
2011-08-26 13:48:47 +00:00
2012-06-05 12:30:15 +00:00
These are the most important commands, but you should look at `:help split`.
2011-08-26 13:48:47 +00:00
> - `:split` → create a split (`:vsplit` create a vertical split)
2012-06-05 12:30:15 +00:00
> - `<C-w><dir>` : where dir is any of `hjkl` or <-&darr;&uarr;→ to change the split.
> - `<C-w>_` (resp. `<C-w>|`) : maximise the size of the split (resp. vertical split)
2011-08-29 13:05:28 +00:00
> - `<C-w>+` (resp. `<C-w>-`) : Grow (resp. shrink) split
2011-08-26 13:48:47 +00:00
blogimage("split.gif","Split")
2011-08-26 13:48:47 +00:00
## Conclusion
2012-06-05 12:30:15 +00:00
That was 90% of the commands I use every day.
I suggest that you learn no more than one or two new commands per day.
2011-08-26 13:48:47 +00:00
After two to three weeks you'll start to feel the power of vim in your hands.
2011-08-29 21:13:33 +00:00
Learning Vim is more a matter of training than plain memorization.
2012-06-05 12:30:15 +00:00
Fortunately vim comes with some very good tools and excellent documentation.
2011-08-29 21:13:33 +00:00
Run vimtutor until you are familiar with most basic commands.
2012-06-05 12:30:15 +00:00
Also, you should read this page carefully: `:help usr_02.txt`.
2011-08-29 21:13:33 +00:00
2012-06-05 12:30:15 +00:00
Then, you will learn about `!`, folds, registers, plugins and many other features.
2011-08-26 14:39:45 +00:00
Learn vim like you'd learn piano and all should be fine.
2011-08-26 13:48:47 +00:00
2011-08-29 21:13:33 +00:00
2011-08-26 13:48:47 +00:00
<script>
// Style the keywords
$(document).ready(function() {
$('code').css({ 'border': 'solid 1px #CCC', 'padding':'3px'});
});
</script>