Do you want to be efficient writing code? Using Vim can be a powerful help. Using it has let me move more effectively through the process of writing and editing code. But getting to where I am now was frustrating. Vim resources are scattered across the web, and contain a lot of different advice. It’s tough to figure out how to make Vim work for you. In my next few posts, I’m going to talk about the process of learning a 23 year old programming power tool, and what you need to know in 2014 to be productive with Vim. Today I’ll start with the basics, stuff that goes back all the way to the 1970s, and we’ll grow from there.

Why has Vim Survived?

Vim has been around for a while. Originally released in 1991 as an improvement on vi (which had been around since 1976), it has outlasted a generation of programmers. The software world has changed a lot since 1991. So why do people still use it?

Vim provides a system for editing text that is quite simply more powerful than its modern graphical based alternatives. It asks a lot of its users. To use Vim effectively you have to internalize the syntax for many commands that have better affordances on other editors. But you’re rewarded with a friction-free editing environment. Vi’s commands are fast, composable, and powerful. So let’s get started.

Step 0: Setup

The first thing you need to do is install a copy of Vim. If you’re on a Linux or Mac system, there is likely already a system copy, but it is probably not up to date. So use your system’s package manager to get the latest version (7.4 at the time of this writing). For Mac users I recommend using Homebrew to download MacVim. Windows users can download the latest executable on Vim.org.

The second thing you’ll need is a vimrc (the file vim uses for configuration). These files can become quite extensive, setting up many different options, configurations, and plugins. But we’re going to start simple. Download this minimal vimrc file and install it in the appropriate directory for your OS as instructed. If you already have a .vimrc file but don’t understand it, I’d recommend saving it elsewhere and using a minimal one for now. You can then build it out as you learn more.

Step 1: Learn the Syntax

Just like a programming language, the first thing to learn for Vim is the basic syntax. Vim has a built in tool to help you get comfortable called vimtutor. It’s a text file set up to help you learn to navigate Vim. Once vim is installed you can run it by calling vimtutor on unix-style systems. On Windows you can follow the instructions here. It will take you through a series of 7 lessons that will familiarize you with the basic commands.

Vim has rather non-standard commands, and it may be frustrating at first adapting to its different terminology and conventions. The payoff comes slowly, but it does come.

Modes

The most important thing to understand is that Vim is a modal editor. At any given time you’re in one of 6 modes. You will have different abilities and keystrokes available to you in each of these modes. For right now the key modes to understand are Normal Mode, Insert Mode, and Command line Mode.

Normal Mode is the default mode. It’s roughly equivalent to the state that other editors are in when you’re holding down the ctrl or cmd keys. Instead of entering text onto the screen, different keys trigger different commands. Initially this is a bit weird. Those of us coming from other editors or IDEs are used to being able to type text by default. The thought is that adding text is the primary task in a text editor. But Vim treats adding text as sitting on equal footing with editing, deleting and manipulating existing text, and starts us in a mode where we can quickly make any change. To steal an analogy from Drew Neil, normal mode is the opportunity to take our paintbrush off the canvas while we decide our next stroke.

Insert mode is the equivalent of most editor’s normal state. When you type a key in normal mode, it appears on the screen. You can enter insert mode by pressing i in visual mode, and exit it by hitting <esc>.

Command line mode lets you enter ex-commands, a command line language that complements normal modes shortcuts. You enter it by hitting : in normal mode. Important commands are :w to write (save) a file, and :q! to quit.

The above commands, and an understanding of the modes are enough for you to survive in Vim, and treat it as a sort of awkward Notepad clone. You can edit files, save them, and quit. Going through vimtutor will introduce you to many more commands, and a few more modes. But you can learn those at your own pace. Right now you want to see what taking time to learn this awkward unfamiliar syntax can buy you.

Step 2: Learn the Language

Vim’s key commands are different than you’ll find in other editors like Sublime Text. They’re not a series of standalone actions that you can do one after another to get what you want. Instead they form a language.

Vim statements are made up of actions and motions. The actions are what you’re trying to do, the motions are where you want to do them.

For example let’s take the d action. d deletes. You can delete a single character by typing dl. You can delete 2 characters by typing d2l. You can delete a whole line by typing dd, or delete inside a word by typing diw.

Once you learn the d action, you can use it with every motion you know. Similarly, when you learn a new motion, you can then use it with every action you know.

So if somebody shows me the y action and tells me that it yanks (copies) text, I’ll know I can yank a character with yl a line with yy and inside the current word with yiw. Each new Vim command is a tool in your toolbox, and since the tools build off of each other they become exponentially more valuable.

Step 3: Learn the Mindset

Vim can make you hyper-efficient. But to get there you need to know more than just the command combinations available. You have to shift your mindset. Vim commands aren’t just easy to learn, they’re designed to be repeatable. Bram Moolenar, Vim’s creator has listed out 7 habits of effective text editing. The second habit, don’t type it twice, is a pretty good summary of the “Vim Way” of doing things.

Vim provides a bunch of ways to avoid repetition. I’ll get into them in another post. For now though, let’s take a look at ., the dot command. The dot command repeats the last action you’ve taken. For instance, you can use ciw to change a word, hit <esc> to return to normal mode, then move to another word and type . to replace that word as well. You are able to avoid retyping the replacement word as well as the ciw command. You can run the whole replace with one keystroke, regardless of what words you’re replacing. Like other Vim commands, the dot command is small on its own, but gains power as you add more tools to your toolbox.

More Resources

If you enjoyed this article you’ll probably also enjoy

  • Practical Vim : This amazing book by Drew Neil, the creator of Vimcasts was the single best resource I found as I learned Vim. I highly recommend it for anyone who uses Vim.
  • Everyone Who Tried to Convince Me to use Vim was Wrong : This blog post is another take on how to start learning Vim, by Yehuda Katz, the prolific developer who’s a core member of the Rails, EmberJS, and jQuery teams.
  • Janus : Janus is another Yehuda Katz project. It’s a starting configuration for Vim meant to make it feel more comfortable for developers who want to come in and be productive immediately. I personally found that it didn’t work for me. The plugin list overwhelmed me and prevented me from learning the basics. But it may be a better fit for some people and is a great place to look to see configuration ideas and plugins that you can try out on your own.

你想高效地编写代码吗?使用Vim会是一个强大的帮助。使用它让我在编写和编辑代码的过程中更有效地推进了我的工作。 但是走到现在这个地步是令人沮丧的。Vim资源散布在网络上,包含了很多不同的建议。要想知道如何让Vim为你所用是一件很困难的事情。 在接下来的几篇帖子中,我将谈谈学习这个23年的编程利器的过程,以及在2014年你需要知道哪些东西,才能在Vim上有所收获。 今天我将从基础知识入手,这些东西可以追溯到20世纪70年代,我们将从那里开始发展。

为什么Vim能活下来?

Vim已经存在了一段时间了。它最初是在1991年作为vi的改进版发布的(vi从1976年开始就已经出现了),它已经超过了一代程序员。 1991年之后,软件世界发生了很大的变化。那么,为什么人们还在使用它呢?

Vim提供了一个编辑文本的系统,它比它的现代图形化的替代品更强大。它对它的用户提出了很多要求。 为了有效地使用Vim,你必须将许多命令的语法内化,而这些命令在其他编辑器上有更好的功能。但你会得到一个无摩擦的编辑环境。 Vi的命令是快速、可组合的,而且功能强大。所以,让我们开始吧。

第0步:设置

你需要做的第一件事是安装一个Vim的副本。如果你使用的是Linux或Mac系统,很可能已经有一个系统副本,但可能还没有更新。所以使用系统的软件包管理器来获取最新的版本(在写这篇文章的时候是7.4)。对于Mac用户,我建议使用Homebrew来下载MacVim。Windows用户可以在Vim.org上下载最新的可执行文件。

你需要的第二件事是vimrc(vim用于配置的文件)。这些文件可以变得相当广泛,设置了许多不同的选项、配置和插件。但我们要从简单的开始。下载这个最小的vimrc文件,然后按照指示安装到你的操作系统的适当目录下。如果你已经有了一个.vimrc文件,但不了解它,我建议你把它保存在其他地方,暂时用一个最小的vimrc文件。然后,你可以在学习更多的知识时再建立它。

第1步:学习语法

就像编程语言一样,Vim首先要学习的就是基本的语法。Vim有一个内置的工具,叫做vimtutor,可以帮助你轻松上手。它是一个文本文件,可以帮助你学习浏览Vim。安装好Vim后,你可以在unix系统上调用vimtutor来运行它。在Windows系统上,你可以按照这里的说明来操作。它将带你经历一系列的7个课程,让你熟悉基本命令。

Vim有相当多的非标准命令,刚开始适应不同的术语和习惯可能会让人感到沮丧。慢慢地就会有回报,但确实会有回报的。

模式

最重要的是要明白,Vim是一个模态编辑器。在任何时候,你都会处于6种模式中的一种。在每一种模式下,你都会有不同的能力和按键。现在要了解的关键模式是正常模式、插入模式和命令行模式。

正常模式是默认模式。它大致相当于其他编辑器在你按住ctrl或cmd键时的状态。不同的按键会触发不同的命令,而不是在屏幕上输入文字,而是在不同的按键上触发不同的命令。刚开始的时候,这有点奇怪。我们这些来自于其他编辑器或IDE的人都习惯于在默认情况下输入文字。大家的想法是,添加文本是文本编辑器中的主要任务。但Vim却把添加文本与编辑、删除和操作已有的文本平起平坐,并将我们开启了一个可以快速进行任何修改的模式。借用Drew Neil的一个比喻,普通模式是在我们决定下一步的笔触时,把画笔从画布上拿下来。

插入模式相当于大多数编辑器的正常状态。当你在正常模式下输入一个键时,屏幕上会出现一个键。你可以在视觉模式下按i进入插入模式,然后按<esc>退出。

命令行模式可以让你输入ex-commands,这是一种命令行语言,是对普通模式快捷键的补充。在普通模式下,你可以通过点击 :进入。重要的命令是:w来写(保存)文件,和:q!退出。

以上的命令,以及对模式的理解就足以让你在Vim中生存下来,并把它当作一种尴尬的记事本克隆。你可以编辑文件,保存文件,然后退出。通过vimtutor会给你介绍更多的命令,还有一些模式。但你可以按照自己的节奏来学习这些。现在,你想看看花时间去学习这个尴尬的陌生语法能给你带来什么好处。

第2步:学习语言

Vim的关键命令与其他编辑器(如Sublime Text)不同。它们不是一连串独立的操作,你可以一个接一个地做,以获得你想要的东西。相反,它们形成了一种语言。

Vim语句是由动作和动作组成的。动作是你要做的事情,动作是你想做的事情,动作是你想做的地方。

例如,让我们以d动作为例,d deletes。你可以通过键入dl来删除一个字符。你可以通过键入d2l来删除2个字符。你可以通过键入dd来删除整行,或者通过键入diw来删除一个字内的字符。

一旦你学会了d的动作,你就可以用它来做每一个动作。同样,当你学会了一个新的动作后,你就可以把它和你知道的每一个动作一起使用。

所以,如果有人给我看y动作,告诉我它可以拔掉(复制)文本,我就知道我可以用yl拔掉一个字符,用yy拔掉一行,用yiw拔掉当前字里面的字。每一个新的Vim命令都是你工具箱中的一个工具,由于这些工具相互之间的建立,它们的价值会成倍地增加。

第3步:学习心态

Vim可以让你的效率更高。但要达到这个目标,你需要知道的不仅仅是可用的命令组合。你必须改变你的思维方式。Vim的命令不仅容易学习,而且还可以重复使用。Vim的创建者Bram Moolenar列出了7个有效文本编辑的习惯。第二个习惯,不要打两次,这是对 "Vim方式 "的一个相当好的总结。

Vim提供了一堆避免重复的方法。我将在另一篇文章中详细介绍。现在,让我们来看看.,点命令。点命令可以重复你上次的操作。例如,你可以使用 ciw 来改变一个单词,按 <esc> 返回到正常模式,然后移动到另一个单词,再输入 .来替换这个单词。你可以避免重复输入替换的单词和ciw命令。你可以用一个键程来运行整个替换,不管你要替换什么字,都可以。和其他的Vim命令一样,点命令本身并不大,但当你将更多的工具添加到工具箱中时,它的功能就会越来越强大。

通过www.DeepL.com/Translator(免费版)翻译