Skip to content

Instantly share code, notes, and snippets.

@jacobw56
Created February 25, 2023 02:41
Show Gist options
  • Save jacobw56/0fb2aa74d94eb5d4110f10573a68317f to your computer and use it in GitHub Desktop.
Save jacobw56/0fb2aa74d94eb5d4110f10573a68317f to your computer and use it in GitHub Desktop.
Getting started with vim

Getting started with vim

Why vim?

The old joke here would be to say, "because emacs is lame," but there are many concrete reasons to consider learning at least the basics of vim (and, honestly, it would be equally beneficial to learn an epsilon of emacs as well).

  • vim is the basis for many other tools. Most other developer tools have at least some parts that will inevitably feel vim or emacs. This is to be expected--we all grew up with these tools and would be unlikely to adopt new tooling that didn't feel at least a little familiar. Even classic command line games like rogue use similar key strokes to vim.

  • One day, you will need to ssh in to a remote terminal and edit a file while the world is burning down around you. Being confident in your abilities to get that done can take a lot of pressure off of you!

  • Editing in vim allows you the opportunity to write, run, and debug code without ever taking your hand off the keyboard. Productivity is good.

There are plenty of other reasons, but these should provide more than enough motivation.

Setup

The minimal setup here is an environment that has vim installed, an sh-style shell (which you already have if you use macOS, Linux, or WSL on Windows), as well as gcc and the associated tools, in particular gdb.

The dreaded empty buffer...

From your command line, navigate somewhere you feel safe, make a new directory to work in (maybe vim_test is appropriate), and cd into it. Now run

vim

Dear lord what fresh hell is this?! You will see a screen with very little by way of compassion. Instead, the prompts on screen give you a few commands to possibly help you get around, although in my experience, new users aren't even sure if the should be typing those colons, or if they are just there as list demarcation.

Maybe more importantly, there is no indication what will happen if you start actually typing. Don't try it, that way lies discouragement. Instead, enter the following, including the colon.

:help

Now we've matters worse--the window has split itself in two and you still know very little.

The thing to understand is that there are two "modes" in vim, command mode and edit mode. When you first enter vim, you are in command mode. Many of the commands are called by typing a colon, then a character or two that represent what it is you want to do.

Go ahead and enter

:q

which will quit (whence the q) your active window. Now enter

:help

again to get back to the help window. Once you are there, enter

:qa!

which will "quit all the windows without saving!" If you ever get in trouble, you can always try hitting esc to make sure you are in command mode, then type qa! to escape from vim.

You should now be back at the command prompt. Now type

vim example.txt

and you will be presented with a vim window in command mode, staring at a blank document. Let's go ahead and actually start editing a file--vim is a text editor after all... Well, actually you can use it as a hex editor as well, and a file browser, and kind of a web browser, but I'm getting ahead of myself.

Press the i key. This indicates to vim that you want to insert text, so it enters edit mode. Type your name and hit the esc key. Now you are back in command mode. Enter

:wq

which will "write and quit." You will be back at the command prompt. Now run

vim example.txt

again. This time hit the a key to add to the text, sending you back into edit mode and letting you add some more text. Type some lyrics from a favorite song, several lines worth, so that you have something like

Walter Jacob
Voi che sapete che cosa e amor,
Donne, vedete, s'io l'ho nel cor,
Donne, vedete, s'io l'ho nel cor.
Quello ch'io provo, vi ridiro,
E per me nuovo capir nol so.

Nice!

Now hit esc to go back into command mode. Enter :0 and it should take you back to the very beginning of your file. If you enter :3 you should jump down to the third line, and so on. Now type G (no colon) and you should Goto the last line of the file. Type d to delete that line and move up to the line above. Now try x to delete a character. Now try moving around the file using the h, j, k, and l keys to navigate around the file a bit. When you start to get the hang of it, type ^ to go to the beginning of the current line, then $ to go to the end of it.

Now enter :q again and see that it won't let you quit because you have unwritten changed in the buffer. You'll see that word in vim a bit. When you are working in vim, you are working in a temporary space called the buffer. When you open a file, its contents are written to the buffer. You then edit the buffer, and it is not until you write it back to the file (with the :w command) that the file is updated. If something should happen to your computer while you are making changes in the buffer, your original file should remain safe, unless the thing that happened to your computer specifically affects your file, of course.

Go ahead and enter :q! to quit without saving.

Okay, so much for the basics. You can find oodles of other commands in any of the readily available vim "cheat sheets" available online.

Some more advanced stuff

Start a new file by entering

vim new_example.txt

Go ahead and hit i, enter some text, then hit esc and :w to save your file. Now type :vs ex and hit tab to tab-complete :vs example.txt. Hit enter and you should see your old file open in a vertically split window to the left of the screen. You use ctrl + ww to switch between windows. If you enter :bd it will close the beffer in the current window, almost the same as :q. You could have used :sp instead of :vs in order to split the window horizontally instead. You could also have used :E to browse for your file.

Now lets get a little fancy. Close all but one window using :q or :bd. Now enter

:vert term

and you should now have a terminal window open in a new split! Neat!

If you are smart about it, you can setup vim to pretty much work just like VS Code, but all in the command line! Imagine the gasps of your muggle co-workers when you ssh into a remote server to clean up someone else's mess and within a few short key strokes you have a full fledged IDE at your disposal :-)

Speaking of a few short keystrokes, you can actually edit/create the .vimrc file in your home directory and add command you want run at startup, then you don't even have to bother entering them yourself like an unfortunate member of the proletariat!

One last fun bit of pfaff. Close the terminal (with exit, if you are using an sh-compatible shell) to get back to a single vim window. Assuimg you have a file buffer open, enter :%!xxd. You now have a fully fldged hex editor! No fooling around looking for extensions :-)

There is much much more to vim--there are as many ways to use vim as there are users. Use it as your daily runner (if you can) for a few months, and I assure you that even if you go back to other editors, the skills you pick up from you time with vim will pay you many many times over.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment