< return home

Vim

Instructions not included

This article was originally published on #dev-a-day.
  1
~
~                             VIM - Vi IMproved
~
~                             version 8.0.1283
~                         by Bram Moolenaar et al.
~                Vim is open source and freely distributable
~
~                       Help poor children in Uganda!
~              type  :help iccf<Enter>       for information
~
~              type  :q<Enter>               to exit
~              type  :help<Enter>  or  <F1>  for on-line help
~              type  :help version8<Enter>   for version info
~
~
~

A Brief History

Way back when Git was just a twinkle in the eye of one Linus Torvalds, the forerunner of the modern Linux operating system was in development. One of the most basic components of any modern OS is a text editor, and for a while pico was the de facto standard. As the open source community is wont to do with most closed source software, an open source clone named nano soon cropped up. If you've done much of anything from a command line before, you're likely familiar.

How do we bridge the gap between basic text editing and modern code editors like Sublime Text, Brackets, and VS Code? There was a lengthy middle ground between the two filled largely by two more advanced CLI text editors - VIM and Emacs. Emacs joined pico amongst the incredible open source software suite released under the GNU open source project, and subsequently joined tabs in the list of things I like slightly less than the alternative (spaces FTW).

Vim as an Art Form

VIM is the modern continuation of the vi editor. It builds on the existing feature set with additions such as multilanguage support (syntax highlighting and code folding), advanced undo/redo, split view for editing multiple files, and a rich diff view, amongst many others.

What good would a development tool be if we as programmers didn't grossly underuse it? There are quite literally hundreds of incredible features that I would never begin to understand made possible by vim, not to mention the incredible plugin environment augmented by tools like Vundle and pathogen, to name a couple.

Nearly all modern OS distros either include vim by default or make it available behind a simple installation. Check your distro for specifics, but it should be available out of the box on Mac and can be grabbed from your system's package manager something like

sudo apt install vim

Here's What You Need To Know

Without further ado, here are the absolute basics you'll want to know to flit around vim whenever you find yourself with the need to do so. It's a neat party trick, but I believe you'll find it invaluable to have a bit more control over your text editor when working on a project only accessible via SSH.

Configuration

Before we do anything else, let's get started with a few sane defaults. Copy the following into a file named .vimrc inside your user directory (~ on most systems):

" enable line numbers
set number

" use 2 spaces for indentation
set tabstop=2 expandtab shiftwidth=2

" enable syntax highlighting
syntax on

" start newlines with the indentation of the previous line
set autoindent

Feel free to shop around for any of the umpteen-million (only slightly exaggerating here) settings available, but I find these to be sufficient to get off the ground.

Open A File

Let's get started by opening a file. You can open an existing file, or specify a file that doesn't exist and have it created on your first save:

vim file.txt

Reboot To Close

Ok, it's a bit of a meme at this point, but getting out of vim can be a bit non-trivial. Once you've opened an editor, you'll be in command mode by default. You'll notice that if you try to start typing, nothing much will happen. To switch to *insert mode to enter text, just click i. While you're in insert mode, click [esc] to get back to command mode. While in command mode, you can enter editor commands by typing : and then entering the command.

i       INSERT mode    (allows you to type by replacing the current character)
a       INSERT mode    (allows you to type starting after the current character)
[esc]   COMMAND mode   (allows entering of commands prefixed by :)

To close an editor, type :wq[Enter]. In vim-speak, that means write then quit.

:w  Write out the file to the disk
:q  Quit the editor
:wq Write out the file to disk and then quit

If you have unsaved changes, you'll either need to save those changes with a :w or force quit the editor with :q!:

:q! Force quit the editor, discarding any unsaved changes

File & Line Navigation

While arrow keys work just fine (for peasants), we're power users and we want to move our fingers as little as humanly possible (maybe less). Vim aliases your arrow keys to the right-hand home row, like so:

[h] [j] [k] [l]
[←] [↓] [↑] [→]

j Down
k Up
h Left
l Right

In case this feels totally foreign to you (don't worry if it does), here's a cool game to help you learn the ropes. Again, you can just use the arrow keys like normal, but this is the vim-ish way to get around.

You can use 0 and $ to jump between the beginning and end of lines:

0 jump to the beginning of the current line
$ jump to the end of the current line

Similarly, you can use gg and GG to jump between the beginning and end of the file:

gg jump to the beginning of the current file
GG jump to the end of the current file

Deletion

The d command is your bread and butter for deleting lines and characters. You can follow it up with any number of modifiers to perform a wide range of deletion (remember to prefix it with :):

:dd  delete the current line
:d$  delete from the current cursor position to the end of the line
:d2  delete the 2 characters following the current cursor position
:d5  delete the 5 characters following the current cursor position (etc)
:d2d delete the 2 lines starting at the current line
:d5d delete the 5 lines starting at the current line (etc)
:dw  delete the word that starts at the current cursor position (space delimited)

Visual Selection & Pasting

From command mode, pressing v will enter visual mode, allowing you to create a selection using the navigation keys. Once you have a block of text selected, you can press y to yank it from the file (think cut in normal editor terms).

v VISUAL mode (allows for visually selecting text)
y yank text (essentially cut it)
p paste the yanked text at the current cursor position

You can also paste the most recently deleted text using p.

Regex Searching

From command mode, pressing / will enter a special command prompt for finding text. You can search by string or by regex (UNIX-flavored, of course).

Conclusion

There is so much you can do with Vim, but this should hopefully give you a good place to start. If you want to learn more, head over to the Vim Tips wiki.

Peace out 😎.

ramblings by Aaron Ross, otherwise known as superhawk610
> ...
© 2023 all rights reserved
built with Gatsby