Golang Project Structure

Tips and tricks for writing and structuring Go code

Cheat Sheet for the Vim Text Editor: Commands and Keys

Language

  • unknown

by

Not everyone likes to write code using impressive IDEs. Many experienced programmers prefer to use the old-school Vim text editor, which runs natively in the Linux terminal. There are also versions available for Windows and MacOS.

Although Vim has a reputation for being intimidating to new users, it offers an incredible degree of control and customization, making it a highly efficient tool for all kinds of text editing.

The Vim text editor running on the Windows operating system.
Vim was first released in 1991, but this version for Windows was developed much more recently.

In this post, we will run through many of Vim‘s basic commands — as well as some more advanced features that will help you become a power user of Vim.

Creating a New File

At the Linux command line, you can always use the touch command to create an empty file:

touch notes.txt

However, you can also simply pass a new filename to the vim text editor, and it will create a new file that is ready for editing:

vim notes.txt

This will start the Vim text editor with an empty buffer that can be saved under the given filename.

A buffer in vim is just an in-memory representation of text.

Getting Started With Vim

If you launch the Vim command without any arguments, then the text editor will launch with an empty buffer that can later be saved wherever you choose.

You can start typing immediately, but it is recommended to switch to Insert mode first. To do so, press the i key. This will allow you to insert text into the current buffer.

Entering Normal Mode

Once you have finished writing, you can switch back to Normal mode by pressing the Esc key.

When the editor is in Normal mode, Vim offers a wide range of commands that allow you to work with your text in various ways.

And this is where Vim‘s real power comes into play.

Let’s start by looking at some commands that move the position of the current cursor.

Moving Around by Line

To move the cursor to the beginning of the current line, press 0.

To move to the end of the line, press $.

To move up or down a line, press k or j, respectively.

To move to the first line in the current buffer, press gg, or to move to the last line in the current buffer, press G.

Moving Around by Character

To move left or right one character at a time, use the h or l keys respectively.

Likewise, to move up or down one character at a time, use the k or j keys respectively.

You can also use the keyboard’s arrow keys in order to move up, down, left or right by one character at a time.

Moving Around by Word

To move to the beginning of the next word, press w.

To move to the end of the next word, press e.

To move to the beginning of the previous word, press b.

To move to the end of the previous word, press ge.

Copying Text

To copy a character at the cursor’s current position, press y.

To copy a word, press yw.

To copy a line, press yy.

Pasting Text

To paste copied text after the cursor’s current position, press p.

Alternatively, to paste copied text before the cursor’s current position, press P.

Deleting Text

To delete a character at the cursor’s current position, press x.

To delete a word, press w.

To delete a line, press dd.

To delete everything from the cursor’s current position to the end of the buffer, press D.

Setting Options

To display line numbers, type :set number.

To activate the text editor’s spell-checking feature, type :set spell.

To convert tabs into a sequence of spaces, type :set expandtab.

To automatically indent new lines based on the indentation of the previous line, type :set autoindent.

Vim Has Three Modes

Vim can operate in one of three modes:

  • Normal;
  • Insert;
  • Visual.

We have already looked at the Normal and Insert modes.

Introducing Visual Mode

Visual mode allows you to select text in a more intuitive way.

In order to enter Visual mode, press v.

This will allow you to select text using the arrow keys.

Once you have selected your text, you can perform operations on it, such as the following:

  • to copy the selected text, press y;
  • to delete the selected text, press d;
  • to copy and delete the selected text, press x;
  • to toggle the case of the selected text, press ~;
  • to join multiple lines of selected text into a single line, press J.

Exiting Vim and Saving Files

To exit the text editor without saving any changes, press :q!.

To save the current buffer to disk, press :w. If a filename has not already been provided or the file has not already been save, then you will be prompted to enter a filename.

To save the current buffer to disk and then exit the text editor, press :x. However, this will only work if changes have been made to the current buffer, so if that isn’t the case, you should press :wq instead.

Advanced Features of Vim

Now that you have a basic understanding of Vim and its many commands, it’s time to undertake a quick run-through of some of the text editor’s more advanced features.

In the final sections of this blog post, we will look at the following features of Vim:

  • how to search and replace text;
  • how to store text in registers;
  • how to record macros.

Performing Search and Replace

To search for text in Vim, enter Normal mode and press /, followed by the text you want to search for. Then press the Enter key.

Pressing n will move the cursor to the next occurrence of the search text.

To replace text, enter Normal mode and type :%s/old/new/g, where old is the text that you want to replace, and new is the replacement text.

The g at the end of the command stands for “global”, which will replace all occurrences of old with new, rather than just the first occurrence.

Using Registers

Vim allows you to store and retrieve text using registers.

A register is just a named storage area where text can be saved.

There are various types of registers in Vim, but the most commonly used are the named registers. There are twenty-six named registers, and each one of them has a single-character name from a to z.

To store text in a register, enter Normal mode and type "a, where a is the name of the register.

Then select the text you want to store using Visual mode, and type y to save it. This operation is sometimes known as yanking.

To retrieve the text from the register, enter Normal mode and type "ap, where a is the name of the register.

Recording Macros

Macros allow you to automate repetitive tasks in Vim.

To record a macro, enter Normal mode and type q, followed by a register name — for example, qa.

Then perform the actions that you want to record.

Once you’re done, type q again to stop the recording.

To play back the macro, enter Normal mode and type @a, where a is the name of the register where you stored the macro.

Leave a Reply

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