Your First Vim Session

~ 4 minute to read

The major barrier to any skill acquisition isn’t intellectual, but emotional. The same goes for learning Vim. That’s why, as one of the first chapters in my book Mastering Vim Quickly, I teach how to do something very cool.

It’s very motivational, especially for someone who is new to the Vim world. You see you can use some advanced concepts, even if you don’t really understand them yet. Right from the start. Many of my readers who were beginners told me this chapter gave them a huge motivation boost.

Here’s a quote from one of them: “Your tag line to the title of your work MVQ, “From WTF to OMG in no time” is well earned, because it did exactly that to me.  The example that comes to mind is quite simple but it was the first time I remember saying to myself “How marvellous”,  Page 19 Task: Change work pick from line 2 to last line (line 6) to s. The fact that moving down a line in visual mode did not select the rest of the line above was a revelation!  This simple action and explanation made me look forward to more secrets explained further in the book.  Thank you.”

This post will present you the chapter I’m talking about – Your First Vim Session. At the end of it, there’s also a video which goes along with it.


To run Vim, open your terminal, and type: vim

This will start Vim, as always, in Normal mode. To start with inserting some text, you need to switch to Insert mode. You do this by simply pressing i. Feel free to type some text, and then try moving your cursor around with arrow keys, so you can gain familiarity.

Now, when you’re done with typing, let’s save this text in a file. To do so, you need to get back to Normal mode. You do this always by pressing the Esc key.

If you have never used Vim before, you’re probably a bit confused. What in the world is a mode? I’ll explain it to you very soon. For now, just try to understand that Vim is different than any other text editor you’ve used. The concept of modes is very simple: you want to insert text—then enter the Insert mode first. You’re done with typing? Exit Insert mode.

Now, whenever you want to type a command, you need to type :. This is how every command starts. The command to save the file and exit Vim is :wq. Letter w comes from write, letter q comes from quit. So, type :wq test.txt and press Enter. Voilà, you’ve just created a file called test.txt.

If you want to open a file you’ve just created, just type in your terminal: vim test.txt. Press key i and add some more text to your file. But, you don’t want to save it this time.
In order to close the file without saving it, and exit Vim, you’ll need to get back to Normal mode (press Esc) and type :q!.

Simple as that. Of course, there are several more ways to open and close files in Vim, but this is enough for your fist Vim session.

Now, let’s do something cool. You won’t understand everything now, but you’ll have a complete understanding of this example when you read the entire book.

If you use https://github.com and git in the command line, you’ve probably had to squash some of your commits at times. This example will show you how quickly you can do it with Vim, even though you’re just at the very start.

In case you don’t know what I’m talking about, never mind—just copy the snippet below to Vim and try out the example.

Info: You’re squashing your commits, here’s the list of them:

pick e08be68 Add initial Rexfile
pick 5b4143f Fixes
pick 855be75 fix
pick d7a5285 Initial work
pick 59e82a2 add more stuff
pick 34cfc9c Fixes

Task: Change word pick from line 2 to last line (line 6) to s.

Solution:

1. Open Vim. Press i to enter Insert mode. Then copy the text from above. Paste the text in Vim, using the shortcut for pasting in your terminal.

2. Now press Esc to get back to Normal mode. Use arrows to place your cursor on the beginning of the second line.

3. Press Ctrl-v. Then press right-arrow three times. You’ll notice that you just selected word pick in second line.

4. Now press down-arrow four times. You’ll notice that you’ve selected all words pick starting from second to last line.

5. While this block of words is selected, press c. That’s a command for changing text.

6. All words in your selection will be deleted, and cursor placed on second line. Mode will change to Insert. Now just type s. That’s what we want to replace word pick with.

7. Last final step – press Esc. And voila! You did it!

Here’s how the result should look like:

pick e08be68 Add initial Rexfile
s 5b4143f Fixes
s 855be75 fix
s d7a5285 Initial work
s 59e82a2 add more stuff
s 34cfc9c Fixes

This might look complicated, but trust me, it’s very easy. Keep reading, and soon you’ll understand what actually happened in this example.

 

If you’re interested in Vim, definitely take a look at my book Mastering Vim Quickly: From WTF to OMG in no time.


If you liked the post, we should get connected - follow me on Twitter

Leave a Reply

Your email address will not be published. Required fields are marked *