Editors note: this is the second in a series of introductory posts on Vim. If you’re brand new to Vim, then definitely read this article first.
So you can get into and out of Vim, congratulations. You also know about Vim’s four main modes:
- Normal mode – the default mode, for editing
- Insert mode – for inserting text, not much else
- Command-line mode – for communicating with Vim
- Visual mode – for highlighting large blocks of text
This post will be a very simple introduction to Normal mode. By the end of this post you should be able to navigate around inside a text file, know how to search for text strings, and be getting a sense for why vim just feels nice to use.
Normal mode is Normal
OK, so normal mode is the mode your normally in; make sense?
So now you’re going to open a text file in Vim that you can play around with and use until you get comfortable.
If you like you can copy and paste this:
This is a dummy file for learning how to use Vim
Welcome, this is a basic text file I wrote in 5 minutes to help you to navigate through
and learn some of the other amazing functions that Vim has to offer. If you’re coming from the navigation tutorial, then please ignore the typo’s. They’re deliberate, and you’ll learn how to fix them in a post in the not-too-distant future.
Until then just get a feel for moving around.
Hey diddle-diddle, teh cat and the fiddle
The cow jumped over the moon
The little dog laughed to ese such fun
And the dish ran away with with the spoon
It’s a common misconception that you should never start a sentence with conjunctions like:
And, But, etc.
Possibly the most faux-pas of these is ‘and’. However it turns out this is perfectly fine. I’m not going to explain why though, you really just need to take my word for it. OK, fine. If you really need to you can google it.
twinkle little star
How I wonder what you are
Up above the world so so high
Like a diamond in the sky
I’m wondering if it’s plagiarism technically to quote word for word full nursery rhymes such as the ones used above, or if they’re considered public domain because they’re so old?
I’m going to assume the latter (you should too!)
This is the last line of my Vim tutorial document.
into a .txt file in something like Notepad, then save as and exit.
If you’re using Windows, then right-click on your newly formed file and hit ‘open with’, then find Vim and click to open with that.
Don’t worry, there are much faster and geekier ways of doing this which you can figure out later.
For now just right-click your file and open using Vim.
If you’re using a Linux or Mac terminal then you can simply open your new document in Vim from the command line by typing:
However you go about it, you should be looking again at a window with Vim in it, with your text file displayed in the window.
Without trying to confuse a non-programmer too much, what you’re looking at is actually called a ‘buffer’. It’s Vim’s representation of the document your editing, not the actual document – this is what makes Vim so lightweight when working with very large files. When you save your changes vim updates the real file.
You’re now looking at your document in Normal mode. Your cursor should be at the top of the screen.
Getting your cursor where you want it
Remember now that the keys on your keyboard are not what they seem. Think of their labels as just that, labels. The key with a ‘j’ on it is not the ‘j’ key, it is just a key called ‘j’.
Press your key called ‘j’. Go on, don’t be scared.
See what I mean… It’s not the ‘j’ key.
As you can see the key called ‘j’ does what you would normally expect the key with the down arrow to do.
Now try the key called ‘k’.
Yep. Does that feel nice?
Can you feel the tension easing?
You can do it.
I’ll bet you can even guess which keys will move your cursor left and right. If you’re not sure don’t worry, I won’t make you guess.
Press the key called ‘l’… Press it a few more times.
Now press the key called ‘h’ a few times.
In normal mode, these four keys ‘h’, ‘j’, ‘k’, ‘l’, are your cursor keys in Vim. They are your most basic, and slowest form of navigation.
No more clumsily having to move your hand over to the cursor keys to move up and down in the text file, or having to move your hand all the way over to your mouse, an entirely different physical object! Bleah.
If the penny hasn’t dropped yet, you should also notice that they are right on the home row where your hand rests naturally most of the time. Coincidence? Well apparently it is somewhat.
Check it this old ADM 3A. This picture has a story to tell.
So when Bill Joy wrote Vi, he used this keyboard to do it, so as you can see by the picture, that’s why the cursor arrow are where they are. Any why not honestly? Why wouldn’t you have your arrow keys right where your fingers rest?
Sensible people… very sensible.
Two other things you’ll notice about this old ADM keyboard is:
- The location of the <ESC> key…
- The key in the top right corner…
Both will explain some things down the track.
Wonderful Geoff, but you said it would be fast.
I know, we’re getting there.
Moving around one character at a time is super slow, and once you’re vimming hardcore, you’ll hardly ever use them.
Despite the fact that I said to stop thinking of the keys as being the ‘g’ or the ‘i’ key, but to think of them as blank keys, the creators of Vi knew that this might still be hard for some people. So even though, in normal mode the key called ‘w’ doesn’t exactly do what you think, the creators of Vi still made an effort to make some things a little intuitive.
If you’re pretty clever with Microsoft Word, you may have figured out that you can skip from word to word by holding down Ctrl and pressing the arrow keys. This doesn’t hold a card against Vim.
Where is your cursor now?
Press the key called ‘w’.
Can you think what the ‘w’ probably stands for here? See what I mean. Vim doesn’t completely throw intuition out the door.
Press the key called ‘w’ a few more times. Now you might be wondering how you get ‘back’ to the start of the word… think about it…
Press the key called ‘b’.
Now, with something like Microsoft word, you really only have the jump forward and back a word options (so far as I know), and then you have home and end, which get you to the start and end of the line.
Well, Vim understands that a big word like ‘understands’ means that if you hit ‘w’ a couple times to get to it, then you would have to hit ‘l’ a bunch of times to get to the end also. That’s not good enough.
Hit the key called ‘e’.
Don’t be confused. It won’t take you to the end of the line, but it does take you to the end of the word… which you’ll realise after a while is very helpful.
Now, this article is only covering the very basic basics of normal mode. In terms of moving your cursor through a document this is just a drop in the ocean.
For example, press ‘8’ then ‘j’
Now press ‘5’ then ‘k’
See what’s happening there? That’s right. You can also do the same thing to move left, right, word by word etc.
Prefixing many of these commands with a number tells vim
‘do what I am about to ask you X number of times’
I haven’t even told you about the difference between using uppercase and lowercase (you seriously cannot underestimate how much you can do with 1-2 keystrokes). I’ll leave it up to you to figure out the difference between ‘w’ and ‘W’, and ‘e’ and ‘E’.
What? It’s not like I’m getting paid for this.
w = move to beginning of next word
W = move to beginning of next unbroken string of text
e = move to end of word
E = move to end of the whole unbroken string of text
More than anything, what the above 4 commands should show you is that Vim has just about all of the bases covered, and as we’ll learn in a later post, almost anything that’s not covered by the thousands of default key bindings, you can customize…
Moving the cursor vs moving the page
So when my obsession with keyboard shortcuts began, I was writing R scripts, and I got frustrated when I realized there was no really good keyboard shortcuts to jump up or down the page, or to move the document itself (what you use the wheel on your mouse for).
Vim? Yep, it does all that and more.
Check out these bad boys (Note the upper cases!):
H = move cursor to the top of the screen
M = move cursor to the middle of the screen
L = move cursor to the bottom of the screen
gg = move cursor to beginning of whole document
G = move cursor to end of whole document
zt = shift the document until current cursor location is at the top of the screen
zz = shift the document until current cursor location is in the middle of the screen (one of my personal favourites)
zb = shift the document until current cursor location is at the bottom of the screen
Take a few minutes to try those out and see what’s going on with each.
Beat that mouse wheel.
Moving very quickly (search)
I can’t remember how much information I was absorbing and how quickly when I first started learning vim, but if you’ve read up to here without stopping, you might be starting feel a little overwhelmed.
So let’s just take a step back and review.
h-j-k-l are your cursor keys < v ^ > respectively.
b = back to start of current/previous word
w = forward to start of next word
e = forward to end of current/next word
Remember: prefixing any of these commands with a numerical value orders vim to execute that command that many times.
And if that’s not enough you can move your cursor all around the document in large chunks, and scroll through the document just as quickly as we just saw.
And we still haven’t covered all of the ways you can move quickly around your file… or the quickest ways.
One last thing I’ll show you before we move on to some actual editing, which is Vim bread and butter, is searching.
Vim can search for a near infinite amount of things.
At its most basic, the vim search has two options.
Search for the next instance, and search for the previous.
But if that wasn’t enough, you can still search forwards or backwards anyway.
There are other text editors with search capabilities, but I never knew about that before I started learning Vim, so this just blew my mind. Using the search command to navigate through my files is probably in my top three most used activities, and I am reminded of its magic every time I go to edit a blog post or something else in Microsoft word and have to scroll through my file and stop and read, and scroll some more, then scroll back, then read through some text until I get to where I want to go.
Even though it turns out that you can search for text in Microsoft Word, it still seems really clunky as soon as you have to reach for your mouse, point and click here and there, etc. I also don’t know how powerful searching in MS word is, but I know that in Vim, it’s amazing.
If I was going to try to convince you that Vim is even my editor of choice for writing prose, the search function would be one major selling point.
Imagine a 10k word draft of a Novel, or something else you’re writing. Imagine trying to find a specific section of that with your mouse wheel…
You know what you’re looking for, a word, or a sentence, but it’s like finding a needle in a haystack. Unless it’s at the beginning or the end, or right after a major subheading, you could waste precious time (and patience) trying to get to it.
In vim it takes seconds.
In order to search forward through your document you start with the forward slash
/something I want to find
Then, by simply pressing ‘n’, Vim will continue to jump forward to the next instance of that string of characters, and when it gets to the end of the file, it jumps back to the start and keeps searching. However, if you search forward, then find you want to look back also then ‘N’ (uppercase) will search in the opposite direction.
If you want to start your search in the backwards direction then the command is
?something I want to find
this time ‘n’ will search backward through the file, and ‘N’ will search forward.
Hope that’s not too confusing. This is just Vim’s way of covering all the bases, and you get used to it.
Learning how to do this changed my life.
This is one of the main reasons I use Vim for writing other than code. It’s why I wrote the draft of this article in Vim.
Now to apologize for being so condescending in this article, I’ll leave it up to you to figure out
- How search for a character at the start of a line and ignore it elsewhere
- How to search for any of a sequence of alphanumeric values
- How to search for something but exclude particular instances of it
- Tons of other cool shit
I hope you enjoyed this article. Remember, so far all we’ve really covered is the navigation basics.
In the last article I showed you how to get into and then get out of vim by pressing
There are actually, heaps of ways of exiting Vim. If you haven’t made any changes to your text file that you’ve been using for this tutorial, then :q will still work. If you have however, then from normal mode you’ll need to press either:
ZZ = save and exit
ZQ = exit without saving
There are even more ways than this, but these two are by far the quickest and easiest.
You might be chomping at the bit to do some real editing, but you technically can’t edit text, if you don’t have any, so next post is going to introduce insert mode, so if you enjoyed this article and the previous one, then make sure to follow me so you get updated when it comes out.
For more information about other topics on this blog and why I started it see here.
Thanks so much for reading.