Linux how to use vim

Getting Started with Vim Editor in Linux

Vim is an advanced and highly configurable text editor built 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 a programmer’s editor. We can use its plugin based on our needs

Installation and Configure vim in Our Linux System

To install vim on Debian based Linux like ubuntu run the command:

To install vim on an arch-based distro run the following command:

Now vim will be installed on your system.

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

There are some commands given to use the vim editor. You can see all commands, and it’s documentation by help command as follows:

Now to exit type this command

Now, Let’s Start to use Vim

To open a file in vim editor just write the file name after the vim command in the terminal as follows:

Then the file will be opened.

Write into file

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

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

Save and Exit:

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

Exit without saving the file:

To exit from the file without saving the file just use the command q! As follows

Vim also comes with its own tutorial. You can see this tutorial by command vimtutor into the terminal .

Now vim tutorial will be opened as follows:

Moving the Cursor:

Till now, we are using the arrow keys to move the cursor into a file, but it is not recommended to use arrow keys in vim. Vim provides the special key to move the cursor in the vim editor following are the keys used to move into a file.

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

Exiting Vim:

To exit without doing anything go into command mode by pressing the Esc key and type the following command.

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

To quit and save changes type the following command

Text Editing: Deletion

We provide x key in command mode to delete the character under the cursor. Move the cursor to the character which has to delete and press Esc key and then press the x key

The character under the cursor will be deleted.

Text Editing: Insertion

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

Motion: Motions provide context to your Operators. These execute the action in a particular way.

Here is a list of some motions

We can use motion with the d key and with many more keys

Count: Count is the number for which replete the motion for count number. Here is a demonstration of the use of count and motion

To move courser 2 words forward use the following command

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

To move the cursor 4 lines forward use the following command

Deletion Commands: Always use the Esc key to go into normal mode and use the insertion, deletion keys, and other keys.

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

Читайте также:  Как сбросить ошибки на принтере epson l210

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

To delete 2 words use the command

To delete the line move cursor to the beginning of the line and use d$ command in normal mode. The line under the cursor will be deleted.

Undo and Redo:

As we are programmers most time we are using undo and redo .vim to provide these to both features in it. To undo press u key in normal mode

To redo use the ctrl+r key in normal mode in vim

Replace :

To replace the character under the cursor use rx command where ‘x’ is a character is to replace.

Change Operator:

In vim c is used as a change operator. To replace the word use ce command

To replace the line use c$ command

This command will delete the contains from the cursor to the end of the line. Then this will automatically go into insert mode then you can put anything in 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 big file vim provide more commands to move into the file.

To move the cursor at the start of the file use the gg command

To move the cursor at the bottom of the file use the G command

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

In place of 32 use can put any line number.

Search:

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

Use n to move on next matching word

Use N to move on previous matching word

Search and Replace:

To replace the word in file use s/ command in vim like

To do replace all occurrence of word use g

This command will replace the word globally.

To confirm before replacements of words use gc

To use this command in the whole file use % before the command

Vim configuration:

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

You can put your all configurations in this file.

You can set line number by using a simple command

Put this command in your .vimrc file

To enable syntax highlighter in vim use command

Put this command in .vimrc file then save file and exit after opening file again you will see the line numbers in vim.

Vim comes with many color schemes using the command:

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 file and exit then again open file to see changes.

Источник

How to use vim editor – Complete Guide

This post will help us to know how we can use a vim editor for editing different files.

Comparison of Vim with Nano editor

Nano editor is the default editor in the Linux distributions whereas the Vim editor is mostly pre-installed in some distributions of Linux. There are some features on the basis of which it is more popular than nano and those features are:

Vim Editor Nano Editor
It is slightly complex for a beginner It is simple to understand for a beginner
Supports programming languages Does not support programming languages
It is a mode-based It is modeless
Improved version of Vi editor Improved version of Pico editor
Advanced editor with many tools Simple editor

Modes of Vim

Vim has two different types of modes, as

Command-line mode : When you open any file with vim, you are in the command mode by default. In command mode, you can perform different tasks by using the commands for example to delete a line, to copy the line, and to navigate the cursor in any specified position of the file. If for any reason you are not in the command mode, simply press the ESC key, to enter in the command mode.

Insert mode : To insert something, you have to choose the insert mode, for this purpose, simply press the I key to enter the insert mode. In this mode, you can write anything and can add anything to the file. Once you are done by inserting, press ESC key from keyboard and switch insert mode to command-line mode.

Читайте также:  Linux bash do continue

Installation of Vim

In some of the distributions of Linux, vim is pre-installed but if it is not installed by default you can install it by two different methods either using the apt command or from the snap utility store. To find out, vim is installed by default or does not execute the following command in the terminal.

The output is showing it is not installed, so we will first install it using the apt command.

To install it from the snap utility store, first, install snap utility.

Now installing the vim editor by the snap utility store.

Vim commands

Vim has been installed in the Linux distribution. Now we will go ahead and learn about the commands in Vim. It has more than 1000 commands to perform different tasks efficiently. Different types of commands are explained with examples.

Basic Commands

Some basic commands of Vim editor are

Open a file : You can open a file with the vim editor by using the keyword “vim”. For understanding, we want to open a file, named file.txt using vim editor.

The file has been opened in the vim editor.

Help command : If you need any type of help regarding any command, type :help [command keyword] in the command mode, the list of the help will be displayed. For example, we find help regarding the “copy” command.

The output will be displaying a file containing all the relevant help regarding the keyword “copy”.

Open a code file : Now if you want to open any other file, you can open it by typing :e [file name] in the command mode. For understanding, we will open a file, named, code2.php using “:e code2.php”.

The specified file will be open as output.

Quit vim without saving a file : We can exit the editor without saving the changes we have done, by typing :qa or :q! and then press ENTER key. For example, we quit the file:

After hitting the ENTER, you will be back to the terminal.

Quit vim by saving the files : We can quit the file by saving it. To do so type :wq and press ENTER.

Save the file : While working if we want to save the changes, we can do it by typing :w and hitting the ENTER key.

Save a file by renaming : We can save a file by renaming it by typing “:w [file name]”:

Cursor navigation commands

In vim editor, there is no use of the mouse as the cursor is moved with the help of keys. We will discuss some keys and shortcuts to navigate in the vim editor. There is a list of keys and their purposes, use them by pressing semicolon “:” and then the specific key.

Commands Actions
h To move the cursor to the left position
l To move the cursor to the right position
j To move the cursor to the down position
k To move the cursor to the up position
M To move the cursor directly to the middle of the screen
L To move the cursor directly to the bottom of the screen
H To move the cursor directly to the top of the screen
e Places the cursor at the end of the word
b Places the cursor at the start position of the previous word
w Places the cursor at the start position of the next word
$ Places the cursor at the end position of the line
Places the cursor at the start position of the line
> Takes the cursor to the start position of the next block or next paragraph
< Takes the cursor to the start position of the previous block or previous paragraph
) Moves the cursor directly to the start position of the next sentence
( Moves the cursor directly to the start position of the previous sentence
G Places the cursor at the end of the file
gg Places the cursor at the start of the file
# To go on a specific line, type the number of lines next to #
CTRL + b Moves the cursor to one page back
CTRL + f Moves the cursor to one page forward

Editing commands

If we want to edit the text, first go to the insert mode by pressing the “I/i” key, then type the text. For editing purposes, there are some commands which can help in editing like copy, paste, delete and undo commands. We can discuss all these commands:

Читайте также:  Как снять галочку на принтере использовать по умолчанию

Copy commands : In vim, the copy word is derived from the word “yank” so it will use copy commands with a representation of yw.

Commands Actions
yy It is used to copy a line
yw It is used to copy a word
y$ It is used to copy from the present position of the cursor to the end of the line

Paste command : In vim, the copied text can be pasted by simply typing “p” after the semi-colon.

Undo command : In vim, if mistakenly or unintentionally any action has been performed, we can undo that action by typing “u” after the semi-colon. We made a blank line in the text as shown in the image below:

Now we will press the “u” key, by entering the command mode, by pressing ESC key after semicolon”:”,

Redo command : To redo any action in the vim, type “r” in the command mode (command mode can be opened by pressing ESC key after typing semicolon)

Delete commands : For the deletion of words or sentences, we use the commands described in the table.

Commands Actions
dd To delete a line
d To delete the selected portion of a line
dw To delete a word
D To delete a line from the present position of the cursor to the end of the line
dG To delete a line from the present position of the cursor to the end of the file
dgg To delete a line from the present position of the cursor to the start of the file

On pressing dd, the entire line has been deleted. For example, we open a file, named file.txt.

Now we will press dd in the command mode.

On pressing dw, “is” word has been deleted. For example, our cursor is on the word “is” in the first line.

On pressing D, the line is deleted from the cursor position:

On pressing dG, deleted all the lines from the starting position of the cursor:

On pressing dgg, previous lines from the cursor has been deleted:

The output, will be

Selection commands : For the selection or highlighting of the text, the following commands are used.

Commands Actions
v To highlight a character
V To highlight a line

Show number against lines

Showing the numbers with every line, make it easy for us to determine which line we are and on which line we have to go for editing. To display the numbers along with the lines of text we can use any of the commands displayed in the table.

:set number
:set nu!
:set number!

Searching commands

We can search specific words in vim also like the other editors. The commands for searching are:

Command Actions
/ [enter the word ] Finds out the entered word from the entire file
? [enter the word] To search previous text from the entered word
n To search your word again in any direction
N To search the word again in the opposite direction

Word count commands

Like other editors, we can also count the words and characters in vim. For this, there are two ways, pressing g and then CTRL + G.

Or use w, !w, and -w in the command mode:

Compare files

We can compare two files in vim. The general syntax of the command will be

For explanation, we compare two files, file.txt and newfile using the vimdiff command.

The output is showing differences like in the first line, letter “l” in first file is in upper case while in other file it is in lower cas and the last two lines are missing in the second file.

Conclusion

Vim editor is the command-line editor, which is much more versatile as it contains all the functions which are needed by the beginner as well as an expert and is popular for its different features. It is the advanced form of the Vi editor and it can be used to open programming files of different languages. In this article, we have discussed the installation and different commands of the vim editor which is used to handle the vim editor. We also tried our best to explain the use of commands with the help of examples.

About the author

Hammad Zahid

I’m an Engineering graduate and my passion for IT has brought me to Linux. Now here I’m learning and sharing my knowledge with the world.

Источник

Поделиться с друзьями
КомпСовет
Adblock
detector