Lesson 1 of 26

Why Vim?

Duration: 8:46 Watch on YouTube (external link, opens in new tab)

Markdown version

In this lesson

Roy Osherove explains why he thinks Vim (external link, opens in new tab) is worth learning by comparing it to regular expressions: it brings a different way of thinking about text as motions, commands and operators, such as delete three words back. He compares common edits in a standard editor with Vim keystrokes, such as c, i and a brace to change inside braces, yy and p to duplicate a line, Shift-J to join lines and 3dd to delete three lines. He also notes that Eclipse and Visual Studio (external link, opens in new tab) have plugins for Vim bindings, and describes Emacs as a powerful editor built on chord-like shortcuts.

Transcript

1,581 words

[00:08] (external link, opens in new tab) We get started with learning Vim, I want to explain why I think Vim is such a powerful thing. Why do I even care about it? Why should you learn yet another text editor? So to actually explain that, I want to explain that Vim is not just another text editor. And the way to explain it, I think, is by explaining why regular expressions exist and why they're useful. So let me talk about regular expressions. If you've never used them, regular expressions are really a new language to explain text.

[00:39] (external link, opens in new tab) They're a way to describe pieces of text in a very meaningful way that is very tur, very simple, and very short. And in that regard once you learn regular expressions and how to talk about text in a form of a regular expression in many fields of working in IT and programming this becomes very useful especially when you're working a lot with text obviously and it's very complicated and it can save you a long a lot of time for things that would be very long to do in a programmatic way.

[01:13] (external link, opens in new tab) Now, regular expressions are powerful because you can then take the concept and the patterns and then apply them in many other areas. And it's the same thing with Vim. Vim is not just a text editor. But Vim actually brings with it a whole baggage of a different way of thinking about text because the way Vim bindings work is that they allow you to think about text as a different as a set of actions and operators and and motions. Basically in Vim the way that you approach text is that you first navigate through it and the text actually has is separated into logical groupings such as words, sentences, paragraphs and you can navigate between all those three and you can then use operators like move three words back or three sentences or three lines and you can also then apply surgical precision changes like delete three words back.

[02:06] (external link, opens in new tab) So once you know how to move with specific motions and you know the language that Vim is thinking in, then you can apply really powerful things in and not think in individual characters but think in more logical constructs. For example, delete until the beginning of the page or just like you have page up and page down. Imagine that Vim gives you all this other new type of language to explain how things are built inside text.

[02:37] (external link, opens in new tab) And in that regard, Vim is a very powerful thing. And because you know Vim bindings once you've learned them, it's important to understand that Vim bindings are so powerful that many editors and many other tools have bindings or plugins that allow you to use Vim bindings inside them when editing text. So for example, if you were working with stuff like Eclipse or Visual Studio (external link, opens in new tab), you can have plugins that give you Vim bindings in them. So the knowledge that you have just from working in a Vim in a command line or in a Vim text editor can actually be applied in many other tools, making you giving you the best of both worlds.

[03:15] (external link, opens in new tab) Both the power of those other IDs and also the power to navigate and change text on the fly very easily instead of moving things one character at a time or one arrow or using the mouse a lot. And as an example, if you if we're comparing the way Vim thinks and the way other text editors think, here's are just a set of a couple of actions to to as an example. If you have an action of changing text (external link, opens in new tab) inside braces, usually in the standard editor, you would have to like control shift or command shift or alt shift and hit a bunch of arrows repeatedly to move the cursor from one side to the other while selecting and then pressing delete.

[03:54] (external link, opens in new tab) And many times you would use two or even three fingers on the keyboard at the same time to be able to do this and pressing things repeatedly. The Vim way is usually that there are many different ways to do it and there are it's should be very easy and you should need the least amount of fingers to achieve it. So in Vim you'd press escape and then type C I and then and brace. Basically you could use one finger press escape and then C and then I and then brace. And what happens then is that well technically you can say it's two because having a brace requires a shift.

[04:30] (external link, opens in new tab) So my mistake but it's still very easy because what I'm telling in the Vimway here is change inside brace. So there's a language here. It's not just a shortcut. Change inside doesn't have to be brace. It could be inside the paragraph or a word or a line. For example, duplicating the current line, you would have to again move arrows and select and shift and all that stuff. But in the Vimway you would press escape because that brings you to a mode called normal mode which I teach in the course and then pressing yp which is basically y

[05:08] (external link, opens in new tab) Is basically be prepare to paste. Y the second one says the current line. If you do two y's means the current line copy that and p means paste that just below just after what you are. But you can use just a single finger to achieve this. If you want to combine two line two lines to form a single line that's you know you have to move and then press delete or use backspace and you have to be at the end of a line or the beginning of the current line etc. And in Vim you just press escape again you move to normal mode which is navigation.

[05:40] (external link, opens in new tab) You press shift J no matter where you are on the line and that basically combines the line after you and removes all the ending new line characters in the current line. If you want to delete three lines, you have to control arrow maybe sometimes shift delete delete move down three and then press delete. And in Vim, you don't have to do anything. You if you're in normal mode, you just press three and then dd means delete the current line just like y means yank or copy the current line.

[06:11] (external link, opens in new tab) And here I'm just saying do that three times. So I just need one finger. So the vim way is about minimal keystrokes to change anything that you want. And it's about learning this language which is made of motions and actions and operators to understand the text you you you want to move through and change kind of like a surgical strike team. And there are usually many ways to do the same thing, but you're really the the way it works is that you're supposed to be only using your hand on the keyboard, not using the mouse, not using even the arrow keys.

[06:42] (external link, opens in new tab) And if you kind of find yourself doing things with the arrow keys or the mouse, there usually either already is an action or you can configure Vim to create an action which again I teach. And so the Vim bindings and you can see they kind of look scary. All these three different colors like the green and the yellow and the orange there are different type of language constructs and then you apply them together. The green is the motions and the yellow is the commands and you can apply commands into specific motions. So moving so applying the command of delete to all the specific motion of a word or until a specific character.

[07:19] (external link, opens in new tab) And then operators like how many times do I want to actually achieve that etc etc. And of course you have the insert mode which is just allows you to change things just by typing. But really, insert mode is the mode that you use almost the least because most of the time you navigate and you execute commands instead of typing and pressing backspace etc. in arrow keys. Vim does most of the work for you. One last thing about Vim and Emacs cuz it's the eternal struggle. Emacs is very powerful. I mean there's this sentence Emacs is basically its own operating system.

[07:52] (external link, opens in new tab) It has so much power and all it's missing is a good text editor. Nice joke. funny, but Emacs really is a very powerful editor and Emacs is really built on really almost chordlike shortcuts that usually use two or three at least three fingers to achieve something and you can do a lot of things and you can customize the hell out of it. So, it's a really powerful editor. You should definitely learn it because you should not limit your worldview to just a single point of view. Always have a critical thinking about the tools that you use and why you're using them.

[08:25] (external link, opens in new tab) So that's kind of the idea. If you want to learn Vim, what you're learning is not the text editor, but a way of thinking about text and a text editor that was built for someone who thinks about text in that way, just like you're using regular expressions. And that will make you much more productive than you are if you're using just a regular text editor.