Introduction to Vim and 12 Useful Commands

,

So today I learned that Vim is a thing while using SSH to a remote WordPress server, so I thought I’d write about it. Here we go.

Are Vi and Vim the same thing?

The popular text editor Vim is an implementation that improves upon the original Vi standard.

Vim is a text editor that was originally developed to be more user-friendly and easy to use. The name “Vim” stands for Vi IMproved, which means it upgrades your standard input with new user-friendly features like macros, Multi-level undo and redo, tabs, and much more.

How do you install Vim?

Vim and Vi are Unix implementations of the POSIX standard. Vim can be found on most Linux systems and most Unix systems like macOS, out of the box these days.

Note: You will need to install Vim separately if you are using Windows as your operating system.

You can open the terminal and type vi --version to check the version you have installed.

How to create or open a file in Vim

To open a folder in Vim open the terminal and type the following:

$ vi <filename>

If you type a name that doesn’t exist a new file will be created in the folder you’re in.

If you type an existing file name it will open it as long as you’re in the correct directory

Tip: Make a duplicate of an existing file before editing with vi or any other text editor. You will probably bork something.

How to switch between Vim Control and Insert mode

There are two modes in Vim Control and Insert.

When you first open a file in Vim you’ll be in command mode by default.

Control mode can be entered by clicking esc on your keyboard. This is where you have the most control, and options to control the document to make changes.

Insert mode can be entered by clicking i on your keyboard. This is where you actually type into the document, aka insert text into the document.

What are some usful Vim Controls?

First, you can navigate through the document you’ve opened with your up and down arrows.

CommandWhat it does
xDelete characters
uUndo changes
ctrl-rRedo changes
:wqSave and Quit
:q!Quit without saving
dddelete a line
{x}dddelete x amount of lines. e.g. 3dd deletes three lines
:set numberTo number the lines of the file
:{x}jump to x line. e.g. :20 takes you to line 20.
/{your text}Search for text in a document
:s/foo/bar/gSearch foo replace with bar in the current line. Finds and runs.
:%s/foo/bar/gcSearch foo replace with bar in the current line. Finds and asks for confirmation.

While the above commands are not exhaustive, they’ll be the ones I’ll use the most. If you’re looking for more, check out this Vim Cheat Sheet from MIT.


Leave a Reply

Your email address will not be published. Required fields are marked *