Learn Vim Progressively

来自《Learn Vim Progressively》该博客上有法文版。

tl;dr: You want to teach yourself vim (the best text editor known to human kind) in the fastest way possible. This is my way of doing it. You start by learning the minimal to survive, then you integrate all the tricks slowly.

Vim the Six Billion Dollar editor

Better, Stronger, Faster.

Learn vim and it will be your last text editor. There isn’t any better text editor that I know of. It is hard to learn, but incredible to use.

I suggest you teach yourself Vim in 4 steps:

  1. Survive
  2. Feel comfortable
  3. Feel Better, Stronger, Faster
  4. Use superpowers of vim

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. It will take time. It will be a lot like playing a musical instrument. 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.

1st Level – Survive

  1. Install vim
  2. Launch vim
  3. 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. Vim is in Normal mode. Let’s go to Insert mode. Type the letter i.

You should feel a bit better. You can type letters like in a standard editor. To get back to Normal mode just press the ESC key.

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:

  • iInsert mode. Type ESC to return to Normal mode.
  • x → Delete the char under the cursor
  • :wq → Save and Quit (:w save, :q quit)
  • dd → Delete (and copy) the current line
  • p → Paste

Recommended:

  • hjkl (highly recommended but not mandatory) → basic cursor move (←↓↑→). Hint: j looks like a down arrow.
  • :help <command> → Show help about <command>. You can use :help without a <command> to get general help.

Only 5 commands. That is all you need to get started. Once these command start to become natural (maybe after a day or so), you should move on to level 2.

But first, just a little remark about Normal mode. In standard editors, to copy you have to use the Ctrl key (Ctrl-c generally). 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.

A last word about notations:

  • instead of writing Ctrl-λ, I’ll write <C-λ>.
  • commands starting with : end with <enter>. For example, when I write :q, I mean :q<enter>.

2nd Level – Feel comfortable

You know the commands required for survival. It’s time to learn a few more commands. These are my suggestions:

  1. Insert mode variations:

    • a → insert after the cursor
    • o → insert a new line after the current one
    • O → insert a new line before the current one
    • cw → replace from the cursor to the end of the word
  2. Basic moves

    • 0 → go to the first column
    • ^ → go to the first non-blank character of the line
    • $ → go to the end of line
    • g_ → go to the last non-blank character of line
    • /pattern → search for pattern
  3. Copy/Paste

    • P → paste before, remember p is paste after current position.
    • yy → copy the current line, easier but equivalent to ddP
  4. Undo/Redo

    • u → undo
    • <C-r> → redo
  5. Load/Save/Quit/Change File (Buffer)

    • :e <path/to/file> → open
    • :w → save
    • :saveas <path/to/file> → save to <path/to/file>
    • :x, ZZ or :wq → save and quit (:x only save if necessary)
    • :q! → quit without saving, also: :qa! to quit even if there are modified hidden buffers.
    • :bn (resp. :bp) → show next (resp. previous) file (buffer)

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.

3rd Level – Better. Stronger. Faster.

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.

Better

Let’s look at how vim could help you to repeat yourself:

  1. . → (dot) will repeat the last command,
  2. N<command> → will repeat the command N times.

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. NG → Go to line N
  2. gg → shortcut for 1G - go to the start of the file
  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.

    By default, words are composed of letters and the underscore character. Let’s call a WORD a group of letter separated by blank characters. If you want to consider WORDS, then just use uppercase characters:

    1. W → go to the start of the following WORD,
    2. E → go to the end of this WORD.

Now let’s talk about very efficient moves:

  • % : Go to the corresponding (, {, [.
  • * (resp. #) : go to next (resp. previous) occurrence of the word under the cursor

Believe me, the last three commands are gold.

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

With all preceding commands you should be comfortable using vim. But now, here are the killer features. Some of these features were the reason I started to use vim.

Move on current line: 0 ^ $ g_ f F t T , ;

  • 0 → go to column 0
  • ^ → go to first character on the line
  • $ → go to the last column
  • g_ → go to the last character on the line
  • 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.
  • F and T → like f and t but backward.

A useful tip is: dt" → remove everything until the ".

Zone selection <action>a<object> or <action>i<object>

These command can only be used after an operator in visual mode. 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). The object can be: w a word, W a WORD (extended word), s a sentence, p a paragraph. But also, natural character such as ", ', ), }, ].

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"))

Select rectangular blocks: <C-v>.

Rectangular blocks are very useful for commenting many lines of code. Typically: 0<C-v><C-d>I-- [ESC]

  • ^ → go to the first non-blank character of the line
  • <C-v> → Start block selection
  • <C-d> → move down (could also be jjj or %, etc…)
  • I-- [ESC] → write -- to comment each line

Rectangular blocks

Note: in Windows you might have to use <C-q> instead of <C-v> if your clipboard is not empty.

Completion: <C-n> and <C-p>.

In Insert mode, just type the start of a word, then type <C-p>, magic…

Completion

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:

  • qaYp<C-a>q
    • qa start recording.
    • Yp duplicate this line.
    • <C-a> increment the number.
    • 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.

Macros

Visual selection: v,V,<C-v>

We saw an example with <C-v>. There is also v and V. Once the selection has been made, you can:

  • J → join all the lines together.
  • < (resp. >) → indent to the left (resp. to the right).
  • = → auto indent

Autoindent

Add something at the end of all visually selected lines:

  • <C-v>
  • go to desired line (jjj or <C-d> or /pattern or % etc…)
  • $ go to the end of the line
  • A, write text, ESC.

Append to many lines

Splits: :split and vsplit.

These are the most important commands, but you should look at :help split.

  • :split → create a split (:vsplit create a vertical split)
  • <C-w><dir> : where dir is any of hjkl or ←↓↑→ to change the split.
  • <C-w>_ (resp. <C-w>|) : maximise the size of the split (resp. vertical split)
  • <C-w>+ (resp. <C-w>-) : Grow (resp. shrink) split

Split

Conclusion

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. After two to three weeks you’ll start to feel the power of vim in your hands.

Learning Vim is more a matter of training than plain memorization. Fortunately vim comes with some very good tools and excellent documentation. Run vimtutor until you are familiar with most basic commands. Also, you should read this page carefully: :help usr_02.txt.

Then, you will learn about !, folds, registers, plugins and many other features. Learn vim like you’d learn piano and all should be fine.

If you liked this article, there is a follow up: Vim as IDE

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 学习Vimscript是一项有挑战性但却非常有价值的任务。Vimscript是用于编写Vim编辑器的脚本语言,掌握它可以让我们更加灵活地定制和扩展Vim的功能。 学习Vimscript的第一步是理解其基本语法和结构。它具有变量、函数、条件判断、循环等常见的编程元素。我们可以通过编写简单的脚本来熟悉这些基本概念。 接下来,我们需要学会如何使用Vimscript来操作文本。这包括了移动光标、选中文本、插入和删除文本等操作。Vimscript提供了丰富的内置函数,可以帮助我们轻松地完成这些任务。 除了操作文本,了解Vimscript的事件处理也是非常重要的。Vim具有许多事件,在特定的事件触发时,我们可以编写相应的Vim脚本来实现自定义的行为。这样可以大大提升我们的编辑效率。 学习Vimscript的过程中,我们还应该熟悉一些常用的Vimscript插件和函数。这些插件和函数可以扩展Vim的功能,并提供更多的可定制选项。了解它们的使用方法可以使我们更加高效地使用Vim。 虽然学习Vimscript可能有些困难,但通过不断的练习和实践,我们可以逐渐掌握它。阅读相关的文档、书籍和教程是一个很好的开始,同时,参与Vim社区也能够让我们与其他Vim用户和开发者交流经验,相互学习。 总之,学习Vimscript可能会有一些挑战,但掌握它将使我们成为更加高效和自定义的Vim用户。不断学习和实践,相信我们一定能够体验到Vimscript的魅力和能力。 ### 回答2: 《学习 Vim Script 的困难之路》是一本旨在帮助初学者深入学习 Vim Script(Vim 脚本)的书籍。Vim Script是一种用于定制和扩展Vim编辑器功能的脚本语言。很多人觉得学习 Vim Script 很难,因为它具有自身的语法和特殊规则。但正是通过战胜这些难点,我们才能更好地理解并灵活运用 Vim。 这本书详细介绍了 Vim Script 的各种概念和基本语法,如变量、条件语句、循环和函数等等。通过提供大量实例和练习,读者可以通过实践巩固所学知识,逐渐掌握 Vim Script 的编程能力。 学习 Vim Script 有一定难度,因为它与其他编程语言有一些区别。首先,它更专注于文本编辑方面的功能,需要适应编辑器的工作方式。其次,Vim Script拥有独特的命令和函数,需要充分理解和记忆。最后,Vim Script 的文档相对较少,需要我们进行更多的实践和尝试。 但不论学习 Vim Script 是否困难,它都是值得学习的。Vim 是一个高度可定制和扩展的编辑器,通过学习 Vim Script,我们可以根据自己的需求来定制和优化编辑器体验。掌握 Vim Script 不仅可以提高编辑效率,还能让我们更好地理解 Vim 的内部机制。 总而言之,学习 Vim Script 可能会有一些困难,但只有通过不断的实践和练习,我们才能逐渐掌握这门语言。《学习 Vim Script 的困难之路》是一本很好的指南,通过阅读并实践书中的内容,我们有望成为 Vim Script 的高手。 ### 回答3: 学习Vimscript的困难之路是面对我们必须克服的挑战。Vimscript是一种功能强大却非常独特的编程语言,用于扩展Vim编辑器的功能。为了精通Vimscript,我们需要有很高的耐心和积极性。 首先,我们需要深入了解Vim的工作原理和结构。我们需要熟悉Vimscript的基本语法和数据结构,包括变量、函数以及条件语句。我们必须理解Vimscript与其他编程语言的不同之处,例如其不同的命名规则和特殊的符号。 接下来,我们需要学习如何使用Vimscript来扩展和定制Vim编辑器。我们可以利用Vimscript创建自定义命令、映射键盘快捷方式、编写插件等。理解Vimscript的底层原理和机制将有助于我们更好地利用这些功能。 另一个困难在于Vimscript缺乏文档和资源。相较于其他编程语言,Vimscript的资料和教程相对较少。因此,我们需要积极探索和研究,利用互联网和相关社区资源来解决我们遇到的问题。 最重要的是,我们必须进行实践。仅仅阅读文档和教程是不够的,我们需要编写代码并测试我们的脚本。通过实际操作,我们才能更好地理解Vimscript的概念和用法,并不断改进和提升自己的技能。 尽管学习Vimscript是一条艰难的道路,但我们要坚持下去。随着经验的积累和不断的实践,我们将逐渐变得熟悉和精通Vimscript,从而能够更好地定制和扩展Vim编辑器,提高我们的工作效率和舒适度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值