How to edit files in linux: the text editor vi is your best friend
Table of contents:
- The Vi Text Editor
- Vi modes
- Vi survival guide
- Basic Commands
- Editing Commands
- Search for and replace
- Copy and paste
- Cut and paste
Vi, from the word Visual, is a program cataloged as a text editor since, unlike those categorized as a word processor, it does not offer tools to visualize the final result of the document at the time of printing it. In other words, it lacks options to center or justify text, but it does allow the basic activities such as copying, pasting, moving or deleting characters versatilely. Often these types of programs are used by programmers for the development of source code.
Surely, you are wondering why we should know about Vi?, the main reason is because it is found in all Linux distributions and in emergency situations it may be the only editor available to solve some problem of system corruption, boot errors or other catastrophe. However, the required resources are low and it is ideal for system file management.
The Vi Text Editor
Vi was created by taking ed and ex resources, two publishers for Unix. Originally written by Bill Joy in 1976. There is an improved version called Vim, but because Vi is in almost all distributions, it is necessary to know its rudiments for emergency operations.
The Vi edit is a text editor that can be used in all types of terminal, its execution is in full screen, it is capable of handling the text of an entire file in memory and a few keys are enough to perform necessary operations.
Vi modes
The operation of vi depends on three states or modes:
- The command or regular mode : it is the default mode of vi, where the keys allow you to perform actions to move the course, navigate the file, handle the text, or simply quit editing. The second, the insert or text mode.: the keys enter characters in the text. And lastly, last line mode or ex: where the keys are used to write commands at the bottom of the screen, on the last line.
Vi survival guide
The syntax to run Vi from your terminal is as follows:
I saw 'file name'
Once the file has been displayed you can move either with the arrow cursors or with the keys: h, j, k, l in case you do not have any arrow cursor.
There are also other ways to invoke vi. For example:
If you just want to open the edit window with no files, use:
saw
In the case of the common syntax, if the 'file name' does not exist, vi generates a file with the indicated name.
You can open vi with multiple files at once:
saw file1 file2
In the same way it allows us to open the file by positioning the cursor on a specific line, at the end of the file or according to the occurrence of a keyword. Below are the examples respectively:
vi +45 file1 vi + $ file1 vi + / There was file1
You may be interested in reading: LyX: Advanced document processor in LaTeX for Ubuntu
Basic Commands
With a few basic commands, you can now work on your vi file.
Command | Description |
---|---|
: q | It is to exit the editor (without saving the information) |
: q! | It is a forced way to exit the editor without saving the information (even if changes have already been made to the file) |
: wq | Save the file and close the editor |
: file name | Save the file with the specified name |
Editing Commands
Command | Description |
---|---|
x | Used to delete the character that is currently under the cursor |
dd | It is used to delete the line that is currently under the cursor. |
d x d | It is used to remove x number of lines from the file, counting from the one that is currently under the cursor. |
n x | It is used to delete n characters counting from the cursor at that moment. |
x >> | It is used to identify x lines to the right starting from the cursor. |
x << | It is used for the indentation of x lines to the left starting from the cursor. |
Search for and replace
To perform word searches, we do it from the regular or command mode. It is as simple as entering the symbol “ / ” followed by the sequence of characters to search for. Press the Enter key for confirmation. To navigate between occurrences we use the n key.
If what we need is to replace a particular character sequence, the syntax to use is the following:
To do it in a line
: s / string to replace / replacement string /
To make the replacement in the whole document
Replacement can be done throughout the document with the following syntax:
% s / string to replace / replacement string /
The best thing about this method is that it is extremely effective for searching through the use of regular expressions.
Copy and paste
The Vi editor also gives us the ability to copy and paste a selection of lines. The process is simple, we introduce the following command:
nyy
Where, n represents the number of lines that I want to copy.
For example, if the command I run is this:
18yy
The result will be, 18 lines copied to the clipboard. To paste the selection we just enter the letter p .
Cut and paste
This process is similar to the previous one, but we replace the command with:
ndd
Similarly, n represents the number of lines to cut and finally to paste we use the p key .
Has life saved you? Did I ever see you? ? Tell us about your experience in the comments.
Qnap launches qfiling: automates the organization of your files and boosts your productivity
Qfiling allows us to automate and manage all our files, improving our productivity and, above all, the time we send them.
How to adjust the size of text and bold text on your iphone or ipad
In this short tutorial we will learn to adjust the size of the text and set the text in bold quickly and easily on our iPhone or iPad
.Dat files - what are these files and how do I open them?
If you do not know how to react to .dat files, here we will explain what they are, how to open them and some ways to see this data.