Skip to content

VIM

Vim is a highly configurable text editor built to enable efficient text editing. It is an improved version of the vi editor distributed with most UNIX systems. Vim is often called a “programmer’s editor,” and so useful for programming that many consider it an entire IDE .

To open a file using Vim you can use the following command (simply replace filename.css with your actual file name).

vim filename.css

Vim modes

Command mode

This is the default mode that you’ll be in once you open Vim. If you’re in a different mode and want to go back to command mode, just hit the Escape key. This mode allows you to use Vim commands and move through your document. From command mode, you can also use last-line commands, which generally start with the use of a colon. For example, :w saves your file and :q allows you to exit Vim.

Insert mode

This mode allows you to enter text into your document. You can enter insert mode by pressing the i key. Keep in mind that to save your document, you’ll need to go back to command mode since only text input is allowed in this mode.

Installing Vim

Install Vim using Git:

git clone https://github.com/vim/vim.git
cd vim/src
make

Install Vim on Ubuntu/Debian:

If you’re using Ubuntu or Debian use apt-get to install Vim, like so:

sudo apt-get install vim

Install Vim on CentOS/Fedora:

If you’re using CentOS or Fedora, use yum to install Vim:

sudo yum install vim

If you want a more advanced set of features on CentOS/Fedora, you’ll need to install vim-enhanced, to do this, run the following command instead:

sudo yum install -y vim-enhanced

Vim commands

1. Basic Vim commands

:help [keyword] – Performs a search of help documentation for whatever keyword you enter

:e [file] – Opens a file, where [file] is the name of the file you want opened

:w – Saves the file you are working on

:w [filename] – Allows you to save your file with the name you’ve defined

:wq – Save your file and close Vim

:q! – Quit without first saving the file you were working on

2. Vim commands for movement

h – Moves the cursor to the left

l – Moves the cursor to the right

j – Moves the cursor down one line

k – Moves the cursor up one line

H – Puts the cursor at the top of the screen

M – Puts the cursor in the middle of the screen

L – Puts the cursor at the bottom of the screen

w – Puts the cursor at the start of the next word

b – Puts the cursor at the start of the previous word

e – Puts the cursor at the end of a word

0 – Places the cursor at the beginning of a line

$ – Places the cursor at the end of a line

) – Takes you to the start of the next sentence

( – Takes you to the start of the previous sentence

} – Takes you to the start of the next paragraph or block of text

{ – Takes you to the start of the previous paragraph or block of text

Ctrl + f – Takes you one page forward

Ctrl + b – Takes you one page back

gg – Places the cursor at the start of the file

G – Places the cursor at the end of the file

# – Where # is the number of a line, this command takes you to the line specified

3. Vim commands for editing

yy – Copies a line

yw – Copies a word

y$ – Copies from where your cursor is to the end of a line

v – Highlight one character at a time using arrow buttons or the h, k, j, l buttons

V – Highlights one line, and movement keys can allow you to highlight additional lines

p – Paste whatever has been copied to the unnamed register

d – Deletes highlighted text

dd – Deletes a line of text

dw – Deletes a word

D – Deletes everything from where your cursor is to the end of the line

d0 – Deletes everything from where your cursor is to the beginning of the line

dgg – Deletes everything from where your cursor is to the beginning of the file

dG – Deletes everything from where your cursor is to the end of the file

x – Deletes a single character

u – Undo the last operation; u# allows you to undo multiple actions

Ctrl + r – Redo the last undo

. – Repeats the last action

4. Vim commands for searching text

/[keyword] – Searches for text in the document where keyword is whatever keyword, phrase or string of characters you’re looking for

?[keyword] – Searches previous text for your keyword, phrase or character string

n – Searches your text again in whatever direction your last search was

N – Searches your text again in the opposite direction

:%s/[pattern]/[replacement]/g – This replaces all occurrences of a pattern without confirming each one

:%s/[pattern]/[replacement]/gc – Replaces all occurrences of a pattern and confirms each one

5. Vim commands for working with multiple files

:bn – Switch to next buffer

:bp – Switch to previous buffer

:bd – Close a buffer

:sp [filename] – Opens a new file and splits your screen horizontally to show more than one buffer

:vsp [filename] – Opens a new file and splits your screen vertically to show more than one buffer

:ls – Lists all open buffers

Ctrl + ws – Split windows horizontally

Ctrl + wv – Split windows vertically

Ctrl + ww – Switch between windows

Ctrl + wq – Quit a window

Ctrl + wh – Moves your cursor to the window to the left

Ctrl + wl – Moves your cursor to the window to the right

Ctrl + wj – Moves your cursor to the window below the one you’re in

Ctrl + wk – Moves your cursor to the window above the one you’re in

6. Marking text (visual mode)

v – starts visual mode, you can then select a range of text, and run a command

V – starts linewise visual mode (selects entire lines)

Ctrl + v – starts visual block mode (selects columns)

ab – a block with ()

aB – a block with {}

ib – inner block with ()

iB – inner block with {}

aw – mark a word

Esc – exit visual mode

Once you’ve selected a particular range of text, you can then run a command on that text such as the following:

d – delete marked text

y – yank (copy) marked text

> – shift text right

< – shift text left

~ – swap case (upper or lower)

7. Tab pages

:tabedit file – opens a new tab and will take you to edit “file”

gt – move to the next tab

gT – move to the previous tab

#gt – move to a specific tab number (e.g. 2gt takes you to the second tab)

:tabs – list all open tabs

:tabclose – close a single tab

Simple Vim workflow example

If you haven’t had a chance to play around with Vim much yet, you might be wondering what a simple workflow looks like when using it. It’s relatively simple:

  1. Open a new or existing file with vim filename.
  2. Type i to switch into insert mode so that you can start editing the file.
  3. Enter or modify the text with your file.
  4. Once you’re done, press the escape key Esc to get out of insert mode and back to command mode.
  5. Type :wq to save and exit your file.