Knowledgebase

Getting Started with VIM Print

  • 0

Vim is one of the most powerful and popular Linux text editors accessible through a command-line interface. It is an improved version of the old Unix editor ‘vi’ pre-installed on most Linux distributions.

Similar to other command-line-based text editors, it is easy to use and offers more functionality with standard features such as syntax highlighting, search, split editing, multi-level undo/redo, among others.

Installation

On Debian and Ubuntu, install Vim with apt.

$ sudo apt install vim

Use yum on Redhat Enterprise Linux (RHEL) based systems like CentOS, Rocky Linux, Alma Linux.

$ sudo yum install vim

On OpenBSD, FreeBSD, install with pkg_add.

$ doas pkg_add vim

On Arch Linux, use pacman.

$ sudo pacman -S vim

Vim Modes

Vim offers four modes of operation, the normal, insert, visual, and command-line modes, with different functions accessible with each.

Normal Mode

In this mode, basic operations like copying, pasting, changing, and deleting text can be achieved.

Press ESC on your keyboard to enter normal mode.

Insert Mode

This mode allows you to edit files, and insert new text in the editor. In this mode, every keyboard character you enter will be added to the file.

There are several ways to enter insert mode, and these are:

  • i - Enter insert mode.

  • I - Insert at the start of the current line

  • A - Insert at the end of the current line.

  • a - Insert below the current cursor position.

Visual Mode

Visual selection, highlighting, manipulation of text, and all normal mode commands can be used in this mode.

To enter visual mode, use v for basic cursor selections, then:

  • V – To enter visual line mode with an entire line selected.

  • Ctrl + v – Enter visual block mode with double text selection expandable to multiple lines.

Command-line Mode

This mode allows you to issue function commands using a colon :.

After the colon :, add a command to execute any function in the editor. Below are some of the most common commands to manage files:

  • :q – quit Vim if no changes are made to the file.

  • :q! – quit Vim and discard any changes made to the file.

  • :w – Save changes.

  • :wq – Save and exit the file.

  • :bn– Edit the next file.

  • :bp – Edit the previous file.

Editing Files with Vim

To open files, simply run vim with the filename as an argument.

$ vim [file_name]

Also, you can open multiple files, and use :bn,:bp navigation commands to switch between files.

$ vim [file_name] [file_name] [file_name]

Enter insert mode by pressing i to edit the file and enter text.

Other editing functions can be performed in normal mode, such as deleting, copying, and pasting text with single or double-character commands.

Copy and Paste

  • y – copy text in a single direction

  • yy – copy the entire line of text

  • P – paste above the current line

  • p – paste below the current line

Deleting Text

  • x – Delete a character

  • d – Delete a character to the next cursor direction

  • dd – Delete an entire line

  • DD – Delete an entire line and create empty space

  • u – Undo the previous operation

  • Ctrl + r – Redo the previous action

Change Text

  • c – Change text in the next cursor direction, Vim will switch to insert mode, enter the replacement text

  • C – Replace text on an entire line and create empty space, Vim will switch to insert mode for new text.

  • r – Replace a character

  • J – Join the current line to the line below. This is important when replacing empty space

Split Editing

  • :split file_name – Horizontally split Vim with another filename

  • :vsplit file_name – Vertically split Vim with another filename

  • CTRL + ww – Switch between windows in split mode

It’s possible to navigate in Vim using keyboard arrow keys, but with navigation commands available in normal mode, you can swiftly move the cursor around, and below are the common keys.

Basic Navigation

  • h – Move the cursor left.

  • l – Move the cursor right.

  • k – Move the cursor up.

  • j – Move the cursor down.

Advanced Navigation

  • :[Number] – Move to the line number

  • 0 – Switch to the start of the line.

  • $ – Switch to the last character on a line.

  • :$ – Jump to the last line in the file.

  • gg – Jump to the top of the file.

  • G – Jump to the bottom of the file.

  • w - Switch to the next word.

  • b - Jump back one word.

  • e - Switch to the end of a word.

Search

  • :/SEARCH_TERM – Move to the text matching the search term

  • n – Move to the next match of the search term

Conclusion

You have successfully installed Vim on your cloud server. For further documentation, run vimtutor in your server console for more powerful techniques on how to use the text editor.


Was this answer helpful?
Back

Powered by WHMCompleteSolution