There are various tools and methods in Linux operating systems to edit text files. Linux's most popular text editor tools are Vi, Nano, and VIM. Vi Linux is one of the most commonly used text editors in Linux because: 

  • It is available on all the Linux operating systems.
  • Users can easily install and use it in their system.
  • It needs a few resources to work properly. 

This tutorial will guide you on how to use Vim Linux text editor. You can use it to create a new file, edit an existing file, and read the existing file.

Create a New File in Vi Text Editor

You can create a new file and enter the environment using the following Vim command.

Command syntax:

or filename

For example:

vi 1gbits.txt

In the above vi command, we created a file called 1gbits.txt. If the file name you provide after this editor exists, it will open the file; otherwise, it will create a file with a specified name. The output of the above command will be like the below:

You will see the "~" character at the beginning of each line. This character shows a new line. If you face a line that is not started with the "~" character and has no content, you have used invisible characters such as Space, Tab, etc. Up to here, you can open a file with this editor. Now we will explain its basic concepts. 

Vi Operation Modes

To work as a VI editor, you need to understand its operating modes. They can be divided into two main parts. 

Command Mode

This mode allows you to do management tasks. In this mode, you can save changes, run commands, move the cursor, cut, copy, paste, and find and replace words. Any word you write in this mode will be interpreted as a command. 

Insert Mode

This mode is for inserting text into the file. Once you are in the Insert mode, any key would be taken as an input for the file you are currently working on. The default mode for Vi is command mode. You can switch to the Insert mode from the command mode by pressing 'i' on the keyboard. To return to the command mode and save the changes you've made, you need to press the Esc key. You will see "Insert" at the bottom of the editor when you are in Insert mode. See the following image:

Note: If you do not know which mode you are in, you can press the ESC key twice, which will take you to Command mode.

Exiting Vi (Exit vim)

To exit Vi, you must enter command mode using the ESC key and type: q. If you have made changes in the file and want to ignore them, you must use q! Command to exit vim without saving.

You can save your changes using the: w command.

You can combine the following commands together using the wq command in Vim to save and exit the file. You can also use the ZZ key combination in vim to save and quit instead of the wq command.

To save a file with another name, you can specify a new file name after the w command as below:

: w azar2.txt

Note: Press Enter key after save and exit commands.

Moving within a File

You need to be in the command mode to move in a file. The default keys for navigation are mentioned below:

Keystroke use

  • K move cursor one line up
  • j move cursor one line down
  • h move cursor one character to left
  • l Move the cursor one character to the right

Note: Commands are case-sensitive. You should use the right letter case.

You can specify times of command execution using a number before the command. For example, you can use the 2j command to move the cursor one line down from the current line. 

You can use the below commands to move around in files as well:

$ move cursor to end of line

0 or | move cursor to the beginning of line

W moves cursor to next word

b move cursor one word to left

) Move cursor to the beginning of next sentence (delimeted by ".", "?" or "!")

( Move cursor to the beginning of current sentence

} Move cursor to the beginning of the next paragraph

{ Move the cursor to the beginning of the current paragraph

G Moves the cursor to the end of the file            

1G Moves the cursor to the first line of the file

nG Moves the cursor to the nth line

.: Moves the cursor to line N.

H Move cursor to the top of page

nH Moves the cursor to the line from the top of the current page

M Moves the cursor to the middle of the screen

L Moves the cursor to the bottom of the page

n Moves to the end of the blank line delimited word

fc Move the cursor to next c character You can replace any character with c

Fc moves cursor back to c

File editing in Vi

To edit files, you must enter Insert mode. There are different methods to enter Insert mode:

Command explanation

  • I Insert before line
  • A Append after the cursor
  • A Append after line
  • O Open a new line after the current line

You can also use the below Vim commands in Linux to edit the file:

CTRL + d Move forward ½ screen

CTRL + f Move forward one fullscreen

CTRL + u Move backwards ½ screen

CTRL + b Move backwards one fullscreen

CTRL + e moves the screen up one line

CTRL + y Moves the screen down one line

Deleting Text in Vi

Use the following commands to delete lines and words in a file:

Command explanation

  • X Delete characters before the cursor
  • dw delete a word 
  • dd Delete current line 
  • D Delete

As mentioned above, you can specify how many times you want the commands to run. For example, the 2x command removes two characters from the cursor. The 2dd command removes two lines from the current location of the cursor.

Copy and Paste Commands

You can copy lines and words and paste them somewhere else using the below commands:

Command Explanation

  • yy Copy current line into "unnamed" storage buffer.
  • P Paste the unnamed storage buffer after the current line.
  • P Paste unnamed storage buffer before current line.

Advanced Commands in Vi

There are advanced Vi commands in Linux that make editing easier and allow for the effective use of vi:

Command Explanation

  • J Join lines and Undo last change
  • G ^ G key along with the CTRL key shows the current file name and its status.
  • ~ Toggle upper and lower case
  • : f Rename the current file to file
  • : show your position in the file and file name.
  • : w filename writes the file in a file named filename
  • : e filename opens another file named filename
  • : cd dirname changes the current directory to dirname
  • nG move cursor to line n
  • CTRL-f scroll forward one screen
  • CTRL-b scroll backward one screen  
  • i insert to left of current cursor position (end with ESC);  
  • append to right of current cursor position (end with ESC);
  • dw delete current word (end with ESC);  
  • cw change current word (end with ESC);  
  • change current character
  • ~ change case (upper-, lower-) of the current character   
  • dd delete current line
  • D delete part of the current line to the right of the cursor 
  • x delete current character
  • ma current position dump out in the current place your last deletion (`` paste '')  
  • u undo the last command
  • J combine (`` join '') next line with this one  
  • : w write file to disk, stay in vi  
  • : q! quit VIM, do not write the file to disk,  
  •  ZZ write file to disk, quit vi
  • : r filename read in a copy of the specified file to the current buffer 
  • / string search for the string (end with enter)
  • string search backward for string (end with Enter)
  • n repeat the last search (`` next search '')
  • : s / s1 / s2 replace (`` substitute '') (the first) s1 in this line by s2

Congratulations! You have learned how to use the VI text editor. 


So this is how you can easily use the Vi Linux text editor to create and edit your text files in Linux without having any troubles. As we have mentioned earlier, Vi text editor is the most commonly used text editor. That's why we have explained everything about Vi text editor, which can help you edit files easily. 

When you buy linux server, it is important to consider factors such as the level of technical support provided, the security measures in place, and the cost of the hosting plan. we provide the best in the market.

If you are facing any problems with the installation, feel free to comment here. We will help you to solve the issue.

People also read: