Getting Started with Vim Editor in Linux – GeeksforGeeks

Vim is an advanced and highly configurable text editor created to enable efficient text editing. Vim text editor is developed by Bram Moolenaar. It supports most file types and vim editor is also known as scheduler editor. We can use your plugin depending

on our needs

Installing and configuring

vim on our Linux system To install vim on

Debian-based Linux like ubuntu

run the command:sudo apt-get install vim To install vim on

an arch-based distribution, run the following command:

sudo pacman -S vim Now vim

will be installed on your system

.

You can open vim by running the vim command in the terminal. vim

vim window

There are some commands given to use the vim editor. You can view all commands, and their documentation by help command as follows

::help Now

to exit

, type this command :!q<img src="https://media.geeksforgeeks.org/wp-content/uploads/20210201151417/q1png.png" alt="vim

help” />

Now, let’s start using Vim

To open a file in the vim

editor, simply type the file name after the vim command in the terminal as follows: vim

filename.txt

The file will then open

.

Write

to file In the

previous step we have opened the file now, Let’s write some content in to write the data we need to go in insert mode. To enter write mode, type i. As follows:

i

After entering insert mode, you will see INSERT in the status bar. After that, we can write any data on it.

vim window

Save and Exit:

We have written the data to a file now the task is to save and close the file to make that first exit from insertion mode by pressing the Esc key. To type a command, first type semicolon ( : ) and then type the command wq! or x! (both do the same thing) And then press ENTER.

:wq!

vim window for save and exit

Exit without save the file:To

exit the file

without saving the file simply use the q command! As follows

:q!

Vim also comes with its own tutorial. You can view this tutorial using the vimtutor command in the terminal.

vimtutorvim window for save and exitNow the vim

tutorial will open as follows

:

vim window for save and exit

Move the cursor:

So far, we are using arrow keys to move the cursor to a file, but using arrow keys in vim is not recommended. Vim provides the special key to move the cursor in the vim editor The following are the keys used to move to a file.

* k -> move up ^ * j -> move down k * h -> move right < h l > * l -> move left j v

Just use these commands in vim files and move the cursor faster in the files.

Quit Vim

😛 to exit without

doing anything, enter command mode by pressing the Esc key and type the following command:

q

To exit vim without saving changes, type the following command in vim

:q!

To exit and save your changes, type the following

command:wq

Text editing: Deletion

We provide the x key in command mode to delete the character below the cursor. Move the cursor to the character you have to delete and press the Esc key and then press the x x key

The character below the cursor will be deleted.

Text editing

: Insertion

We have edited some text files before using the i key. There are four keys used for text insertion. Just type the key in normal mode in vim.

i -> This key is used to place the cursor before the current position. a -> This key is used to position the cursor after the current position. or -> This key is used to put the cursor below the line. O -> This key is used to hover over the line.

Movement: Movements provide context to your operators. These execute the action in a particular way.

Here is a list of some movements

w – until the beginning of the next word, EXCLUDING its first character. e – at the end of the current word, INCLUDING the last character. $ – to the end of the line, INCLUDING the last character. We can use the movement with

the d key and with many more keys

Count

: Count is the number for which the movement is filled to count the number. Here is a demonstration of the use of counting and movement

To move the course 2 words forward, use the following command

2w Here 2 is the number of counts and w is used

for the word

To move the cursor 4 lines forward

, use the following command 4$Removal commands

: Always use the Esc key to enter normal mode and use insertion, deletion keys and other keys.

To delete the word, move the cursor to the beginning of the word and use the dw command in normal mode. The word below the cursor will be deleted.

dw

To delete more than one word on a single line, use the following command.

To delete

2 words

, use the d2w command

To delete the line, move the cursor to the beginning of the line and use the d$ command in normal mode. The line below the cursor is deleted.

d$

Undo and Redo:

As we are programmers most of the time, we use undo and redo .vim to provide these to both features in it. To undo, press

the u key in normal mode u To redo, use the ctrl key + r in

normal mode in vim ctrl +

r

Replace : To

replace the character under the

cursor, use the rx command where ‘x’ is a character to replace

. r Change operator: In vim

c is used as a change operator. To

replace the word use ce command ce To replace the line, use the c$

c$

commandThis command will delete the cursor contents to the end of the line. Then this will automatically enter insert mode, then you can put anything on that line

, cursor

location:

we can use the h, j, k, l key to move the cursor in the file, but it will be difficult to move in a large vim file provide more commands to move to the file.

To move the cursor to the beginning of the file, use the gg:gg command To move the cursor at the

bottom of

the file, use the G:G command

To view the current cursor location in the file using the following command::

ctrl+g

Instead of 32 use you can put any line number.

Search for

:

To search for the word After the cursor uses the backslash key and then type the word and press enter.

:/ word Use n to move to the

next matching word:n Use

N to advance to the

previous matching

word:N

Find and replace

😛 to replace the word in the file use s/ command

in vim as

:s/searchword/replaceword/

To replace all word occurrences use

g :s/searchword/replaceword/g

This command will replace the word globally.

To confirm before replacing words, use gc:s/searchword/replaceword/gc To use this command on the entire

file,use % before command:%

s/searchword/replaceword/

gc

Vim settings

:

To configure vim, vim comes with the .vimrc file in the home directory If this file is not there, create one. Then open the file in vim by the command vim

~/.vimrc

You can put all your settings in this file.

You can set the line number

by using a simple set number

command Place this command in your .vimrc file To enable the syntax highlighter in

vim, use the command syntax

in

Place this command

in the .

vimrc, then save the file and exit After opening the file again, you will see the line numbers in vim.

Vim comes with many color schemes using the command:

color scheme COLOR_SCHEME_NAME

Replace COLOR_SCHEME_NAME with any color scheme between Default, blue, darkblue, delek, desert, elford, evening, industry, koehler, morning, murphy, pablo, peachpuff, ron, shine, slate, torte, zellner, then save the file and exit and reopen the file to see the changes.

color.png