THE CYBERDECK CAFE

View Original

(Neo)Vim for the Uninitiated

Intro:

Hey chummer, you wanna know how to hack the Gibson like a real netrunner? Don't we all... But to get there, we're going to need some tools.

The first tool on the list for the burgeoning decker (and coincidentally what this entire article is about), is the all-powerful text editing swiss army knife known as Vim. Yes, it sounds like a cartoon about an alien invader, but it's even better than artwork by Jhonen Vasquez.

Before we get started, I want to mention that there's a nice "TL;DR" at the end for those of you who don't like filler text. I get it, I really do. So just pop on down to the bottom if you want.

Starting your adventure:

There's some history about how Vim, and the re-write Neovim, came to be. But those are secondary to the function, so feel free to look into it on your own. What you need to know is as follows: Vim is modal, this means it works by having separate modes for editing and commands. Also, Vim uses shortcut keys and commands. Finally, Vim uses things called buffers rather than editing a single file or having tabs of different files.

So hopefully you're saying to yourself right now "Ironpotato, I know what Vim is. Every time I try to talk to a linux guy about nano he calls me a skiddie and spits in my direction. But what I can't figure out is how to work that dregg! Every time I type it just spits out a bunch of garbage!" And that is why I'm here! To get you started with Vim quickly and efficiently. The hardest thing about Vim is learning how to use the modes correctly. From there you can slowly add to your text editing repertoire a little at a time. Before you know it you'll be tossing out scripts like Case himself.

The first thing to get down is switching between normal mode (where you enter commands) and insert mode (where you insert text). You can easily get to normal mode by hitting ESC. Something most tutorials don't tell you is that you can also substitute CTRL+[ to do the same thing. It's a bit easier than constantly reaching for ESC in my experience. Some people, myself included, like to map CapsLock to ESC. But sometimes I'm on a box where that isn't possible. So I have CTRL+[ burned into my muscle memory as well.

Anything but Normal, Normal Mode:

Now that you're in normal mode, the next thing to learn is the magic of 'hjkl'. These keys can be used to navigate the cursor. You can use the arrow keys to move the cursor, but you'll come to find that these keys are highly superior. No need to take your hand off the home row. The 'h' key will move the cursor left, 'j' will move it down a line, 'k' will move it up a line, and 'l' will move it left. As a tip for the future, there are some automation commands that will work with 'hjkl' but not the arrow keys. So that's another reason to get used to this scheme. Clearly, using 'hjkl' doesn't work in insert mode, otherwise, you wouldn't be able to type any of those letters. That would be counter-intuitive. So you'll notice that people who use Vim will generally jump right back into normal mode as soon as they're done typing anything.

Insertion is a virtue, Insert Mode:

So you can move around, great. But the real highlight of a text editor is inserting text. So how are you supposed to do that? Well, it's very intuitive, just hit 'i'. That will put you into insert mode. In insert mode you can, you guessed it, insert text. Then to stop, you'll simply hit ESC to go back to normal mode. Now you're a real console cowboy. Technically this is all you need to know to use Vim. Don't you feel accomplished? Well, you still need to learn how to save, otherwise, what's the use of a text editor? But we'll get there soon.

Taking a trip around Normal Mode (A few times):

So now you can "use" Vim. But let's kick it up a notch. The true power of Vim is in the ways you can manipulate it to make text entry and data lookup a breeze! So, down to the nitty-gritty, there are a few more keys you'll want to learn in normal mode. These will let you do a movement and then once at you're destination you'll automatically be in insert mode. These keys are 'a' and 'o'. The 'a' key can be thought of as "append", it moves the cursor right one character and puts you in insert mode. Alternatively, you can hit 'A' to be put into insert mode at the end of the line you're on. The next one is 'o' and it will insert a new line underneath your current line, of course leaving you in insert mode. I use this one all the time. Capital 'O' will insert a newline above your current line which also extremely helpful. Definitely make a hard mental note of these commands.

Vim also provides keys to quickly move around in normal mode that won't drop you right into insert mode. We'll start with the two easy to remember ones. The '$' will put you at the end of a line, and the number '0' will put you at the beginning. I don't use those super often, but they're there. More useful are the 'w,b,e' commands. The first, 'w', is self-explanatory. It stands for "word" and it lets you move to the beginning of the next word. Now, 'b' stands for "beginning" and will move you to the start of the previous word. Lastly, 'e' moves to the end of the current word. In classic shortcut key fashion, you can also use the capital of these three, and they function almost the same. Normally things are considered a word if they're separated by "-" or "." among others, but the capital version of 'W,B,E' will only count as a word if they are prepended or followed by a whitespace. You may want to play with these on your own to get the feel for them. They're very useful though, so don't ignore them!

A hint of power to come:

This is the second to last part, so stay with me, you're almost a Vim wizard! I'm not going to go into detail on this, because it takes a bit of practice to get down. You'll definitely want to be aware of this though, so mull this information over a bit. I've already said that Vim's power comes from the way you can move around, and one way that this power is extended is through adding a number to the beginning of these commands. So, for instance, in Normal Mode, you can type '4w' to move ahead four words at one time. Super nifty. It works for many other commands as well, so don't be afraid to play with it.

Normal Mode's conjoined twin, Command Mode:

For the final piece of this tutorial, I'll be talking about Command Mode. It's technically another name for Normal Mode, but I (and the Vim tutorial) distinguish it because it works a bit different than what we've done in Normal Mode up to this point. To enter a command in Command Mode you'll first want to be in Normal Mode and then you should type ':'. So, for instance, you can type ":help command-line-mode" to get more information about using this mode. Now the first command you'll want to commit to memory is ':w' this will "write" whatever you're currently working on to memory. It's how you save a new document or changes to a document you already have open. MAKE SURE YOU HAVE PERMISSIONS FOR THE FILE YOU'RE TRYING TO EDIT FIRST. This isn't very 1337 of me to admit, but I can't count the number of times I've tried to save a system config file and received the dreaded "E212: Permission Denied" error. Vim is pretty good at telling you when you open a file whether or not you can edit it, but sometimes I type faster than I edit. That's a problem you get when you have so much power. ;)

The next important command is ':q'. This stands for quit, and it's how you exit a file that's open in Vim. Vim will warn you if you've already made changes and tell you that you can exit by hitting ':q!'. This is not a good idea if you don't want to lose your work. The '!' basically forces a command. But, the ':q' command is a nice segue into my last tip for getting started in Vim. If you want to save a document and then exit in a single command, all you need to do is type ':wq'. This will save the document and close it. Notice you've combined two commands at once? That's an important thing to understand about Vim. A fun game is figuring out how much you can do with a single command, instead of, say, five separate ones. Sometimes, of course, it's quicker to just do a couple of commands instead of staring and thinking about the "more efficient" way to do it, but that's life.

Through the Ringer:

So there you have it! You can use Vim! With only a handful of commands, you can successfully write your favorite programming language in Vim! Now there are all sorts of plugins to improve Vim's function for <insert your favorite language here>. Those all have their own commands, configs, and options. So I can't possibly cover it all here. Besides that, there are tons of basic things I left out! You can do windowing right in Vim natively, which is extra handy for remote SSH sessions. There's also a Visual Mode for selecting text and copy/pasting. Buffers are a whole lecture in and of themselves, but learning them opens up all sorts of new avenues for the budding Vim user. If you want to get a bit more familiar with Vim, then the install (usually) includes a package called "vimtutor" that you can run from the command line. It walks you through interactively using Vim and I recommend doing so if you've got a little spare time.

A final word(s):

Let me wrap this up, by saying that Vim is capable of so much more, this is just a small taste. What I am aiming for with this guide is to get people using Vim (or Neovim) instead of feeling like it's too complicated or outside of their abilities. Some people even feel like it takes too much time to learn, which simply isn't true. With ten minutes you can be passible. If you force yourself to use it for a couple days, you'll have the muscle memory down. So it goes without saying that just a few tidbits of knowledge can get you by with Vim, or even Vi if it's all you have available. It's a wonderful tool to have in your back pocket if you do any work on Linux systems. My real goal is to get people to stop editing files in notepad. At least use notepad++ and stop mangling my git repositories! So until next time!

:wq

:e ~/Documents/vim_tut

Oops, almost forgot, I wanted to mention, there's a '.' command that repeats your previous command, and it's really cool and handy. There are some limitations, but I encourage you to keep this tip in mind if you learn a bit more about Vim ;) Okay for real this time, bye darlings. :wq

TL;DR:

You start in Normal Mode

  • i (In Normal Mode) - Puts you into Insert Mode

  • ESC or CTRL+[ - Go back to Normal Mode

  • :q (In Normal Mode) - Quit

  • :q! (In Normal Mode) - Force quit and don't save changes

  • :w (In Normal Mode) - Write (Save)

  • :wq (In Normal Mode) - Write and then quit

  • h,j,k,l - Move the cursor

  • a - Moves the cursor right one character and drops you into Insert Mode

  • A - Moves to the end of the current line and drops you into Insert Mode

  • o - Creates a newline under the current line and drops you into Insert Mode

  • O - Creates a newline above the current line and drops you into Insert Mode

  • $ - Moves to the end of the current line

  • 0 - Moves to the beginning of the current line

Each of these commands can be started with a number to repeat that action (n) times.

  • w - Moves forward a single "word"

  • W - Moves forward a single "word" that follows a whitespace.

  • b - Moves to the beginning of the previous word

  • B - Moves to the beginning of the previous word that is followed by a whitespace

  • e - Moves to the end of the current word

  • E - Moves to the end of the current word behind the next whitespace

  • . - Repeat the last command

(Credit) Ironpotato - Author of this great how to.