Using VIM as a text editing tool on the Mac

The bluffers guide

VIM allows you to create and edit text files on your Mac. It is wonderful, fast and free. However there seems to be no secret guide to this to help understand VIM. But this is it!

People tend to use vim because of it’s small size (it does not use a large amount of system resources) and is fast and lightweight, even when dealing with large files. This means that if you are editing a large source code file, that VIM can be a great choice. People also love the speed of search and replace, meaning no waiting around for the editor. Good stuff!

Prerequisites:

  • Some experience with the Terminal on the Mac

Terminology

Command line: An interface for interacting with the operating system.

Terminal: A command line interface to control the UNIX-based operating system that underlies the Mac.

VIM: This is a contraction of Vi and IMproved. That is, it is a clone (with additions) of the vi text editor for Unix. In other words, it is a command-line interface for creating and manipulating files.

The essentials

Want a tutor to help you? From the command line you can enter vimtutor

$ vimtutor

but once you have entered there, to get out again you will need to quit (:q)

It can feel like you can get stuck in VIM. You might want to just save and exit at any time, and in fact it tells you something about this as soon as you load up VIM. But here are the essential commands — but be aware that the colon (:) is required for these to work in VIM:

:q — to quit

:w — write your file (i.e.: save)

:wq — write and quit (save and exit!)

However when editing you might need to press escape to be able to type these commands.

You can add an exclamation mark to the end of a command which forces it to happen. i..e :q will fail when changes have been made, but :q! forces a quit even if changes have been made.

Help? This allows you to access the help in the editor.

:h

but once you are in the help you will need to quit it (:q)

The step-by-step tutorial

You can open up Terminal on your Mac (perhaps by opening Spotlight on your Mac with Command⌘-Space key combination and type “Terminal”.

Traverse to a folder you want to work in (documents will do, but it might get a little bit messy!).

You can move through the text editor using the cursor keys, or JKJL:

Image for post
Image for post

Cursor keys do the same, however they are considered less efficient because you need to move your hand to the cursor keys on your keyboard.

Creating a file

In order to create a file, this is the same as opening a file. There is no difference between the two in VIM (if there is a file, it will be opened in normal mode and if not a new file will be created).

$vim test.txt

The modes overview

Normal mode (also known as command mode). You use the arrow keys to navigate around the file and start editing, once you have moved into insert mode. From this mode you can save, undo (u) and use some other interesting text editor commands.

Insert mode. To go from the default normal mode you need to press I (in either upper or lower case). You will then be able to type to add to the file, until the escape key is pressed and VIM is returned to normal mode.

Visual mode. To move from the default normal mode you press V (in either upper or lower case). In visual mode it is possible to select text and move it.

Normal mode (replacing the mouse)

Normal (command) mode basic commands

  • a — append to end of current character (useful as just typing puts text BEFORE the highlighted character)

Insert mode (inserting and editing text)

In insert mode you are able to type like in a normal text editor. Access insert mode through i (in upper or lower case).

When entering insert mode you should be able to see

— — INSERT — —

at the bottom of the window.

To get out of insert mode, press escape.

Visual mode (selecting text)

Select where you want to move a word (character, or sentence) in normal mode. Press v to enter visual mode, and then you can select the word (character, or sentence) you want to move.

When entering insert mode you should be able to see

— — VISUAL — —

at the bottom of the window.

To get out of visual mode, press escape.

Once you have selected the word (character, or sentence) you can do the following:

  • d — delete (which is actually a cut operation in VIM)

Visual line mode

Want to copy and paste line of text? Go from normal mode to visual line mode (shift-v).

When entering visual line mode you should be able to see

— — VISUAL LINE— —

at the bottom of the window.

To get out of visual line mode, press escape.

Then you can select several lines of text

  • d — delete

Opening two files together

You can launch multiple files at once! You can write the following from the terminal

$ vim -O leftwindow.txt rightwindow.txt

To switch between the Windows you need to be in normal mode. You tell VIM you want to switch between the open windows with Control-W, and then can toggle windows with w or choose which window with the cursor keys.

  1. Toggling:

Control + w, then W

2. Selecting

Control + w, then directions (or h/j/k/l)

When exiting you need to save the files separately (in other words save the file that has the focus, and then move onto the other file).

you can close all windows with

:wqa

Did you know?

You can suspend your vim session. This can be done through:

Control + Z

from normal mode in VIM.

To get it back you bring VIM back into the foreground

$ fg

This exits VIM completely. A similar result can be achieved trough

:sh

to exit the default shell and then

$ exit

will return you to vim

Within vim you can use

syntax: on

to be able to visually see things like markdown syntax

Further reading

To get used to the movement of Vim you can use vim-adventures.com. however, be aware that there is a fee if you continue to use the game beyond the first three levels:

An alternative to learn the movement keys is Snake VimTrainer:

And if you want a great intermediate tutorial? Try here:

Want to get in contact? Use Twitter:

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store