en
  English
Phone :  +370 (5) 204-1903
Email: sales@1gbits.com
  1. Dedicated server
  2. Blog
  3. Vi Text Editor

Vi Text Editor

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

Linux Tutorials Jan 28, 18 by Mery 8 min Read
Vi Text Editor

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

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

In this tutorial, we will guide you on how to use Vi text editor in a Linux environment. 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

Using the following command, you can create a new file and enter the environment.

Command syntax:

or filename

for example:

vi 1gbits.txt

In the above 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 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, this means that 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. You save changes, run commands, move the cursor, cut, copy, paste the text and lines, find and replace words in this mode. Any word that 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 that 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:

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

Exiting Vi

To exit from Vi editor, 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 in command mode.

You can save your changes using the: w command.

You can combine the save and exit command together using: wq command. You can use the ZZ key combination instead of: wq command.

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

: 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 commands 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 also 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 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 file 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
  • control-f scroll forward one screen
  • control-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 VI, 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. 

Conclusion

So this is how you can easily use the Vi 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 that can help you to edit files easily.  If you are facing any problem with the installation, feel free to comment here. We will help you to solve the issue.

author img

Mery

Leave A Comment