Advance

How to install and use vi as a full text editor

How to install and use vi as a full text editor
0
(0)

Since the Linux Foundation launched the LFCS certification, in this article, we are going to learn you how to install and use vi as a full text editor. The LFCS (Linux Foundation Certified Sysadmin) helps individuals from all over the world to verify they are capable of doing basic to intermediate system administration tasks on Linux systems: system support, first-hand troubleshooting, and maintenance, plus intelligent decision-making to know when it’s time to raise issues to upper support teams.

 

How to install and use vi as a full text editor

Let’s review the steps of this guide to learn the basic file editing operations and understanding modes in vi/m editor, that are required for the LFCS certification exam.

Recommended Article: How To Install Node.js On CentOS 7

Perform basic file editing operations using vi

Vi was the first full-screen text editor written and intended to be small and simple for Unix. It can be a bit challenging for people used exclusively to GUI text editors, such as NotePad++, or gedit, to name a few examples.

You will learn 3 methods of this powerful program to be able to use Vi and then about its powerful text-editing procedures.

Important: Most modern Linux distributions ship with a variant of vi known as vim (“Vi improved”), which supports more features than the original vi does. So we will use vi and vim interchangeably in this guide.

You can install vim if your distribution does not have installed it.

Ubuntu and derivatives: aptitude update && aptitude install vim

Red Hat-based distributions: yum update && yum install vim

openSUSE: zypper update && zypper install vim

 

Buy Linux Virtual Private Server

 

Why should I want to learn vi?

In the following, we mention the two good reasons to learn vi.

First, vi is always available since it is required by POSIX.

Second, vi does not consume a considerable amount of system resources and allows you to perform any imaginable tasks without lifting our fingers from the keyboard.

And it is worth to mention that vi has a very extensive built-in manual, which can be launched using the: help command right after the program is started. This built-in manual contains more information than vi/m’s man page.

 

Launching vi

Type vi in your command prompt, to launch vi

starting vi editor

 

You can press i to enter Insert mode, and you can start typing. But another way to launch vi/m is to use the below command which will open a new buffer named filename, which you can later save to disk.

vi filename

 

Understanding Vi modes

  1. When you are in command mode, vi allows the user to navigate around the file and enter vi commands, which are brief, case-sensitive combinations of one or more letters. Almost all of them can be prefixed with a number to repeat the command that number of times.
  2. In ex mode, you can manipulate files. To enter this mode, you must type a colon (:) from command mode, directly followed by the name of the ex-mode command that needs to be used. After that, vi returns automatically to command mode.
  3.  In insert mode, you simply enter text. Most keystrokes result in text appearing on the screen.

insert mode

 

Vi commands

Take a look at the below list which shows a summary of commonly used vi commands. File edition commands can be enforced by appending the exclamation sign to the command.

Key command: Description

h or left arrow: Go one character to the left

j or down arrow: Go down one line

k or up arrow: Go up one line

l (lowercase L) or right arrow: Go one character to the right

H: Go to the top of the screen

L: Go to the bottom of the screen

G: Go to the end of the file

W: Move one word to the right

b: Move one word to the left

0 (zero): Go to the beginning of the current line

^: Go to the first nonblank character on the current line

$: Go to the end of the current line

Ctrl-B: Go back one screen

Ctrl-F: Go forward one screen

i: Insert at the current cursor position

I (uppercase i): Insert at the beginning of the current line

J (uppercase j): Join current line with the next one (move next line up)

a: Append after the current cursor position

o (lowercase O): Creates a blank line after the current line

O (uppercase o): Creates a blank line before the current line

r: Replace the character at the current cursor position

R: Overwrite at the current cursor position

x: Delete the character at the current cursor position

X: Delete the character immediately before (to the left) of the current cursor position

dd: Cut (for later pasting) the entire current line

D: Cut from the current cursor position to the end of the line

yX: Give a movement command X, copy (yank) the appropriate number of characters, words, or lines from the current cursor position

yy or Y: Yank (copy) the entire current line

P: Paste after (next line) the current cursor position

p: Paste before (previous line) the current cursor position

. (period): Repeat the last command

u: Undo the last command

U: Undo the last command in the last line. This will work as long as the cursor is still on the line.

n: Find the next match in a search

N: Find the previous match in a search

:n: Next file; when multiple files are specified for editing, this commands loads the next file.

:e file: Load file in place of the current file.

:r file: Insert the contents of file after (next line) the current cursor position

:q: Quit without saving changes.

:w file: Write the current buffer to file. To append to an existing file, use :w >> file.

:wq: Write the contents of the current file and quit. Equivalent to x! and ZZ

:r! command: Execute command and insert output after (next line) the current cursor position.

 

Vi Options

You can add the following options in your ~/.vimrc file which may come in handy while running vim:

echo set number >> ~/.vimrc  echo syntax on >> ~/.vimrc  echo set tabstop=4 >> ~/.vimrc  echo set autoindent >> ~/.vimrc

And, in case you need to know more:

set number shows line numbers when vi opens an existing or a new file.

syntax on turns on syntax highlighting (for multiple file extensions) in order to make code and config files more readable.

set tabstop=4 sets the tab size to 4 spaces (default value is 8).

set autoindent carries over the previous indent to the next line.

 

Search and replace

As a new feature of this, we can mention that vi has the ability to move the cursor to a certain location based on searches. It can also perform text replacements with or without confirmation from the user.

1- Searching within a line: the f command searches a line and moves the cursor to the next occurrence of a specified character in the current line.

An example: the command fh would move the cursor to the next instance of the letter h within the current line. Note that neither the letter f nor the character you’re searching for will appear anywhere on your screen, but the character will be highlighted after you press Enter.

2- Searching an entire file: use the / command, followed by the word or phrase to be searched for. A search may be repeated using the previous search string with the n command, or the next one. This is the result of typing /Jane in command mode.

 

3- vi uses a command to perform substitution operations over a range of lines or an entire file. To change the word “old” to “young” for the entire file, we must enter the following command.

:%s/old/young/g   

Please Note The colon at the beginning of the command.

The colon (:) starts the ex command, s in this case, % is a shortcut meaning from the first line to the last line, old is the search pattern, while young is the replacement text, and g indicates that the substitution should be performed on every occurrence of the search string in the file.

To ask for confirmation before performing any substitution, a c can be added to the end of the command alternatively.

:%s/old/young/gc  
  1. y: perform the substitution (yes)
  2. n: skip this occurrence and go to the next one (no)
  3. a: perform the substitution in this and all subsequent instances of the pattern.
  4. q or Esc: quit substituting.
  5. l (lowercase L): perform this substitution and quit (last).
  6. Ctrl-eCtrl-y: Scroll down and up, respectively, to view the context of the proposed substitution.

 

 

Editing Multiple Files at a Time

Let’s type vim file1 file2 file3 in our command prompt.

vim file1 file2 file3  

you can see that, vim will open file1. But you need to use the :n command to switch to the next file (file2). And: N will do the job when you want to return to the previous file. Next, to switch from file1 to file3.

1- The :buffers command will show a list of the file currently being edited

:buffers  

2- The command: buffer 3 will open file3 for editing.

 

 

Temporary vi buffers

To copy a couple of consecutive lines into a temporary buffer named a and place those lines in another part of the file later in the current vi section:

1. Press the ESC key to be sure you are in vi Command mode.

2. Place the cursor on the first line of the text you wish to copy.

3. Type “a4yy” to copy the current line, along with the 3 subsequent lines, into a buffer named a. You can continue editing our file – you do not need to insert the copied lines immediately.

4. When you reach the location for the copied lines, use “a before the p or P commands to insert the lines copied into the buffer named a

  • Type “ap to insert the lines copied into buffer a after the current line on which the cursor is resting.
  • Type “aP to insert the lines copied into buffer a before the current lin.

 

In case, you prefer, you can repeat the above steps to insert the contents of buffer a in multiple places in our file. A temporary buffer, as the one in this section, is disposed of when the current window is closed.

Recommended Article: How to install and use vi as a full text editor

Good job! by finishing this tutorial, you learned How to install and use vi as a full text editor. And also you see, vi/m is a powerful and versatile text editor for the CLI.

 

Dear user, we hope you would enjoy this tutorial, you can ask questions about this training in the comments section, or to solve other problems in the field of Eldernode training, refer to the Ask page section and raise your problems in it.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

View More Posts
Marilyn Bisson
Content Writer
Eldernode Writer
We Are Waiting for your valuable comments and you can be sure that it will be answered in the shortest possible time.

Leave a Reply

Your email address will not be published. Required fields are marked *

We are by your side every step of the way

Think about developing your online business; We will protect it compassionately

We are by your side every step of the way

+8595670151

7 days a week, 24 hours a day