Top Vim Commands

Vim is the main text editor available on the Linux Command Line Interface to create and edit files. The following shows some commands you will find useful. There are modes in vim. One is the command mode also known as normal mode and another is the insert mode.

In this guide, we will cover some basic commands in the Vim editor.

Basic Vim Commands

Create/Edit a File
Changing modes in the Vim editor
Macros
Search and replace
Exit commands
Cursor movement
Text deletion commands
Cut, Copy & Paste
Window Spliting
Other useful commands

The following is a simple program that assigns a greeting message to the variable as a string and outputs a greeting message in PHP. This program was used to introduce basic Vim commands in the screencast.

<!DOCTYPE html>
<html>
<body>

<?php
$welcomeText = "Hello world!";
echo $welcomeText;
echo "<br>How's it going?";
?>

</body>
</html>

To create/edit a file in the Vim editor

vim filename

For example, type the vim command to create a file named hello.php:

vim hello.php

Saves the file you're working on but don't exit

:w

Changing modes from one to another

To type commands, you have to first activate Vim's command mode. To do so, press the Esc key. You can then type any command of your choice. By default, Vim starts in command mode.

Switch to insert mode to start adding text to your file

i

Switch to command mode

Esc

To record a Macro in Vim press q in command mode and then the letter to which you want to save it. So if you press qa and start recording, your macro would be saved to a. You stop recording by pressing q again in command mode. Vim macros (also known as vim recordings) are an efficient way to carry out repetitive edits on a text file and are one of the most powerful features of Vim. To execute the macro type @ followed by the register letter where the macro is stored.

Suppose you want to clear a macro. You simply clear a register by assigning it an empty string:

:let @a = ""

This assigns an empty string to the register a, effectively deleting the macro stored there.

Searching and replacing

Type / then search term and press enter. Once you've performed a search, you can navigate through the search results by pressing n for next instance and N for previous instance.

Search backward

If you want to search in reverse (upwards in the file), use ? instead of /

Press ?
Type the word or pattern you want to search for
Press Enter

For example,

?hello

To replace the first match in the current line, use the following:

:s/old/new/

If you want to be prompted to confirm each replacement, use the c flag. This will allow you to confirm or skip each match individually

Replace and confirm each occurrence in the current line

:s/old/new/c

Replace and confirm each occurrence in the entire file

:%s/old/new/c

Replace and confirm each occurrence between lines 1 and 12

:1,12s/old/new/c

If you want to search and replace the pattern in the entire file, use the percentage character. To replace all occurrences of the search pattern, use the g flag.

:%s/foo/bar/g

If the string part is omitted, it is considered as an empty string, and the matched pattern is deleted.

:%s/foo//g

Will return the number of matches and the number of lines those matches occur on, without doing any substitution

:%s/some_pattern//n

In Vim, when searching for text that includes special characters like ".", "*", "?", "$", "&", "!", "^", "/", "\", "[", "]", "|", "{", "}", "(", or ")", you need to "escape" them by placing a backslash ( \ ) before them.

For example, to search for the string "anything?"

/anything\? and press Return

For more information on searching and replacing with Vim

http://docs.oracle.com/cd/E19253-01/806-7612/editorvi-62/index.html

Exit commands

To save and quit

:wq

To quit

:q

To save forcefully and throw away any unsaved changes

:q!

Cursor movement

Scroll up half a page

ctrl + u

Scroll down half a page

ctrl + d

Move to the top of the screen

H

Move to the middle of the screen

M

Move to the bottom of the screen

L

Jump to the end of the line

$

Jump to the start of the line

0

Go to the first line of the document

gg

Go to the last line of the document

G

Move one word to the right

w

Move ahead three words

3w

Move one word to the left

b

Move back three words

3b

Text deletion commands

Cut the current line

dd

To delete a range of lines the syntax is as follows:

:[start],[end]d
Example: :3,5d In this command the editor will delete the lines from 3 to 5

Delete all lines at or below the current one

dG

Delete from the cursor to the end of the line

d$

Delete from the cursor to the end of the word

dw

Delete to start of line

d0

Yank

Duplicate the current line

yyp

Copy the current line

yy

Copy from the cursor to the end of the line

y$

Copy from the cursor to the end of the word

yw

Paste

Paste above cursor

P

Paste below cursor

p

Window spliting

Split the window vertically and open filename

:vsp filename

Split the window horizontally and open filename

:sp filename

Switch between windows

ctrl + w, then (separately) followed by left/down/up/right arrow to move to the left/bottom/top/right window accordingly

To close the split like you would close vim

:q

Other useful commands

Will show line numbers

:set nu

Hide line numbers

:set nonu

Go to a specific line number

:<line number> and press the enter button

To enable syntax highlighting type the below command and press Enter

:syntax on

To turn off syntax highlighting type the below command and press Enter

:syntax off

To undo a change

u

To redo a change

ctrl + r

Find out the latest news, updates, scripts, tips and more on the official Vim website:

http://www.vim.org

See the accompanying guide on our YouTube channel

https://www.youtube.com/watch?v=rjNyLW6uGuQ

Learn

Related articles

Getting Started with React

In this guide, we will cover the basics of setting up a local development environment, starting a simple React project, and the basics of how it works.

How to Create Bash Aliases

This tutorial demonstrates how to use the alias command to create personalized shortcuts, which can help you save time and feel less frustrated.

Using Git for PHP Development

This guide walks through the fundamentals of Git. In this tutorial, we will show you Git commands. For Linux you can use the built-in terminal.

How to Connect to MySQL with Laravel

In this guide, you will learn how to connect your Laravel application to your MySQL database.

How do you change the default SSH Port on CentOS Stream 9?

Changing the default SSH port adds an extra layer of security by reducing the risk of your password being cracked from a brute force attack.

What is Inheritance in PHP?

In this tutorial we will explain inheritance in PHP, a mechanism that allows a child class to inherit properties and behaviors from a parent class.