A Few Points for People Thinking About Trying Out Vim

Vim has been around in one form or another for a long time. Created by Bram Moolenaar and others, It's a clone of the vi editor that was created in 1976, and it has a strong following despite the numerous tools that have come and gone. For me, Vim has given me very little reason to use other tools. One of Vim's strengths is that it allows the user to keep their fingers on the home row. This means no wasted time moving a hand to find a mouse that's just out of sight. However, there are other benefits. vi, can be found in most Linux shells that power a significant chunk of IT infrastructure. Knowing Vim makes means that you won't feel that uncomfortable in vi since they share so many things. Another benefit is that Vim is highly configurable, and one user's set up can be completely foreign to another.

Typically Vim's natural habitat is a *nix operating system like Ubuntu, Debian, Arch, or even MacOS, but it can be run on Windows as well. When I used Windows for work I used gVim, and it worked fine. However, I do prefer running Vim from the terminal. You can check the Vim homepage for installers for your particular OS

Regardless, becoming accustomed to Vim can be a little daunting at first, but once learned it's difficult to move to other editors, even if they offer a "Vim mode". This post is for any new comer to Vim who is just starting starting out. I try to cover the things I wish someone told me in the first day or two (or even the first year). However, this post is far from being a definitive guide. There are so many parts to Vim that this post could have easily ballooned to a much bigger size that it already is.

Exiting Vim

Before starting Vim it's important to understand how to exit Vim; otherwise, you'll end up frustrated with a seemingly unforgiving terminal that won't let you give up. It's usually at this point another meme is added to the internet about trying to quit Vim (it's what Vim is truly famous for).

The trouble is that quitting Vim depends on your situation. The most basic way to quit Vim is to issue the quit command by typing :q. However, problems might arise if you've somehow managed to stumble into insert mode and modified your buffer. For starters, if you're in insert mode (you'll see -- INSERT -- at the bottom left of your screen) you'll have to make it back to normal mode by pressing the escape key once or twice. Once in normal mode you'll be able to quit Vim with the quit command. However, if you made changes to you're file then you'll have to quit and discard those changes with :q!.

One last thing, there is a way to get Vim completely hung up where even CTRL-c won't help you. If you press CTRL-s Vim is seemingly paralyzed. The only way to get out of it, other than killing the terminal, is to press CTRL-q.

About Modes

Vim has a number of modes that it can operate in, but for the most part you'll be in one of four: normal, insert, replace, and visual. For now, I'd suggest getting comfortable to these four first before learning other modes.

Normal Mode

When you start up Vim it will be in normal mode. Typing while in this mode will not translate to text on the screen. Instead keys are mapped to commands. For example typing an x will delete the character the cursor is on. Typing * will start a search for the word that the cursor is on. Pressing the escape key once or twice while in another mode will typically return Vim to normal mode.

A way to tell if you're in normal mode is if the bottom left of your Vim window is blank. Vim will not say it's in normal mode like it does for other modes.

Insert Mode

If we want to start editing the text on our screen we'll need to enter insert mode. From normal mode you can enter insert mode by typing i. At that point you'll see -- INSERT -- has appeared in the bottom left of your screen. You'll also be able to type on the screen as one might expect from an editor which is what you're probably trying to do in the first place. Some commands are still available for you in this mode, but not nearly as many as in regular mode. Most likely you'll want to escape back to normal mode to perform a command.

Replace Mode

Like Insert mode this is another mode that allows you to edit the text on your screen. Like the name suggests and characters typed will replace characters on the screen. This is different from insert mode since that mode will shift old characters to the right as you type. You won't use this mode as often as insert mode, but it's helpful in some contexts. It's entered by typing R while in normal mode. You'll know that you're in replace when you see -- REPLACE -- in the bottom left of the screen.

Visual Mode

Another useful mode is visual mode. It can be entered by pressing v from normal mode. This mode allows you to see what text you're looking to manipulate by highlighting text. That text can then be manipulated with other commands like delete, yank, change, etc.

A handy variation of visual mode is visual block mode. This can be entered by typing CTRL-v from regular mode. You'll know you're in this version of visual mode if you see -- VISUAL BLOCK -- in the bottom left of your screen. Once in visual block mode you'll notice that moving the cursor will highlight a block of text that isn't constrained by the natural flow of your text. You can literally select a column of text. This is helpful for changing parts of lines that line up vertically.

However, don't be surprised surprised if you find yourself using the visual modes less and less over time. You'll find other tools within Vim that are usually better suited for your tasks.

Moving the Cursor

You can use the arrow keys, but it's not recommended. Instead use h, j, k, and l. The reason for this is that this will allow you to keep your fingers on the home row and that means that you'll be more efficient. Note that these work in Normal mode.

  • h: moves the cursor to the left one character.
  • j: moves the cursor down a line.
  • k: moves the cursor up a line.
  • l: moves the cursor to the right.

Once you get the hang of it it becomes pretty easy. However, there are also many other ways to get around.

  • gg: move the cursor to the first line of the file.
  • G: move the cursor to the last line of the file.
  • $: move the cursor to the end of the current line.
  • 0: that's the number 0, it moves the cursor to the start of the current line.
  • b: move the cursor to the beginning of the word the cursor is on. Goes to the beginning of the previous word if the cursor is already at the beginning of a word.
  • e: move the cursor to the end of the word the cursor is on. Goes to the end of the next word if the cursor is already at the end of a word.
  • f: this is followed by a character and it will move to the cursor to the first instance of that character to the right of the cursor.
  • F: this is followed by a character and it will move to the cursor to the first instance of that character to the left of the cursor.
  • CTRL-u: move the cursor up half a screen.
  • CTRL-d: move the cursor down half a screen.

That should cover a basic set of commands that will help you get around faster. There are many more commands and there are even variations of these commands. I recommend getting familiar with this basic set and then dive into the Vim help files to learn more.

Copy Pasta

Copy, pasting, and cutting are represented by yank, put, and delete. Yank, y, allows you to copy some text to a register. Then you can put , p, that text somewhere else in the text on your screen. If you want to move the text, similar to cut on many operating systems such as Windows, then you can delete, d, text. See the following examples.

If I want to copy a line of text I can type yy while in normal mode while the cursor is on the desired line. Then I can place the copy by typing p. The extra y is needed because you can use modifiers to do different things, and that extra y tells Vim to yank just the line that the cursor is on.

If I want to move a line of text I can type dd in the same fashion. Then I can place the text by again typing p.

If instead I want to copy a word while the cursor is on the first character of that word I can press ye to copy the text up to the end of the word. The e tells yank what text should be yanked. You can also do the same with delete via de.

There are many ways you can use yank, put, and delete, but that's for a different post. Once you're more comfortable with Vim I highly recommend looking into text objects.

[count] Modifier

Now that you know a few basic command you should know that many commands can be modified, and one of the most basic ways to modify a command is to prefix it with a number. That number will tell Vim to run that command that many times. For example 5ewill move to end of the 5th word. You can even apply this to commands that edit the text on the screen. Foe example 10dd will delete ten lines

Undo/Redo

This is easy to remember u for undo and r for redo. You need to be in normal mode for it to work.

That's it.

Searching

This can get pretty complicated depending on what you want to do, so let's keep it simple.

Typing / flips Vim into a search mode. At that point you can typing something, hit enter. The cursor will move to the first instance of that search term. If you want to move to the next instance of it then press n. If you want to move back then press p. Highlighting search results will require setting up configuration settings in your .vimrc file, but that's out of the scope for this post.

Saving your Work

This is another activity that's simple once you know how to do it. In normal mode type :w. If the file doesn't exist you'll need to provide a file name. Just note that Vim will place that file in the directory that you started Vim.

Buffers, Windows, and Tabs Oh My!

Once you get the hang of Vim you'll probably want to have more than one file open at once. There are a few mechanisms that you need to understand to see how Vim accomplishes this. Vim's help files has a great summary of the three:

A buffer is the in-memory text of a file. A window is a viewport on a buffer. A tab page is a collection of windows.

So that text that you've been using this whole time was in a buffer that was shown in a window.

Buffers

Buffers are really handy. Vim keeps a list of them that you can view by using the :buffers or :ls commands. They do the same thing, so use the one that comes more naturally to you. You can then use that list to load that buffer you want by using :buffer command followed by the id of the buffer you want to use.

Buffers can be cycled through using :bnext, :bprev, :bfirst, :blast. Of course there are variations on these commands so use what you like. Later on in your Vim journey, I highly recommend Tim Pope's unimpaired.vim. That plugin has a number of shortcuts that make using buffers and other things much easier to use. For example instead of :bprev and :bnext you can use [b and ]b and save typing three characters per command. It might sound silly, but those three keys add up over time, and you'll never look back.

Windows

As stated above windows are how buffers are displayed to you, and they were one of the first things I learned when getting into Vim. To be productive, you'll need a few commands if you want to use windows. One thing to note before we dive into these commands is that some of them use the CTRL-w prefix. By that I mean that you'll need to press CTRL-w before you enter the desired command. You'll also find that there are sometimes alternatives too.

The first commands you should know are for window splitting. You can split a window horizontally with :sp (or :split or CTRL-w s they are all aliases of one another), or it can be split vertically with :vs (or :vsp or :vsplit or CTRL-v). However, this will split the current file. If you wanted to open a new file horizontally then you'd use CTRL-w n (or :new). For a new vertical window you'd want to use :vnew. Note that there is not a CTRL-w alternative for this one.

Now let's say you've created a split but you want to swap the windows. For that you want to swap the windows. For that you could use CTRL-w x. There are a few other commands for moving windows around, but that's the one I rely on.

One last thing for windows is that they can be resized in a few different ways.

  • CTRL-w =: makes all windows equal. This is a good way to reset windows.
  • CTRL-w +: increase the height of the current window.
  • CTRL-w -: decrease the height of the current window.
  • CTRL-w >: increase the width of the current window.
  • CTRL-w <: decrease the width of the current window.

One last thing to remember about resizing windows is that you can prefix the command with a number. This is multiply the effect so that you can get to the right window size faster.

Tabs

You can get tabs in Vim, but if you embrace buffers and windows you'll find that you don't use them as often as you would in other editors. This is because they add more clutter to the screen. However, they can still be very helpful. I tend to use them to separate windows for different categories of files like application files, testing files, documentation files or even different sets of related application files.

To start out I would focus on three commands, :tabnew to create a new tab, :tabn to move to the next tab, and :tabp to move to the prior tab. To be honest that's all I use. I don't use the other tab commands, but you can dive in further if you wish.

The Leader Key

Eventually you'll find yourself wishing there was a better way to invoke commands, and this is the beauty of Vim. It's highly customizable.

One of the things you can do is to use the leader key to map commands that you use frequently. By default the leader is set to the \ key, but that's a bit of a stretch so many people remap it to the , key. This is up to you. From there you can add custom mappings for your favorite actions. I won't go into details here, but the point here is to let you know that it exists.

Your vimrc File

The .vimrc file is your configuration file. You can set up max widths, how tabs are used, highlighting, color schemes, plugins, and many many more things here. For a post like this it's out of scope to go into details, but you should know that the file existed. Many Vimmers share their .vimrc files to show others how their workflow is setup, and it's not uncommon to ask others how they set their up. You can see mine on my github account..

The one warning I have is that you may grow to rely on those configurations. I keep mine pretty boring so that I am relatively comfortable in vi if I ever have to ssh into a production system.

Getting Help

If you're familiar with invoking help in most applications then you'll be reaching for the F1 key, and you'll be greeted with a read only buffer that will give you more information than you ever wanted. However, that first help screen is designed to help beginners. It teaches you are how to move around with h,j,k, and l. Then it covers how to exit Vim since you'll probably have enough after you realize h,j,k, and l are preferred over the arrow keys.

With the bare necessities out of the way it covers how to jump to subjects with CTRL-] This is helpful when navigating the help files. Most highlighted words serve as points that can be used to jump to specific topics. If you want to return then CTRL-o will do that.

If you know what you want to find help on you can use the help command, :help a_topic

What's Next

There is a problem, Vim is incredibly vast. If there's something that can be done it can probably be done more efficiently, and we haven't even scratched the surface with this post. There are so many ways to use Vim, and I am not an expert. However, don't despair. If you find yourself struggling with something see if you can find a better way to do it, and if you can't find an answer from Google ask a seasoned Vimmer. The point is you'll never be done expanding your Vim skills. Even if you master Vim itself there are countless plugins that can do more. Plugins to integrate git, make file searches easier, add better handling for your language of choice, etc.

What's next is up to you. At the very least I recommend switching to Vim permanently. From there you can peruse the help files vim offers or check out the built in vimtutor. The important part is that if you run into a problem look it up and learn how to do it.

I hope you've enjoyed this post. If you have, drop me a message on twitter. If there was something you struggled with that you think could have been explained better or that I missed please let me know.

Thanks for reading and happy Vimming!

Back