Open a file at a specific line number using Vi and Vim editors
This tutorial demonstrates how to use the Bash command-line interface (CLI) to open a plain text file with a specific line number using the vi
or vim
editor.
First, we will briefly introduce vi
the editor and its successor vim
, the editor. Then, we will immediately move on to their use for opening a file when the cursor is at a specific line number.
vi
The editor is the most classic text editor available in almost all Linux distributions. Hence, it does not require any additional packages to be installed before using it.
Moreover, vi
is an easy to use CLI based editor for any plain text file. Hence, it is lightweight and fast.
vim
The editor is vi
the successor to the editor, Delegate Vi IMproved
. It adds new features and improves many of the original vi
editor.
It follows the POSIX 1003.2-1 standard, which makes it compatible with almost all Unix and Linux distributions, including MS-DOS, Macintosh, Amiga, VMS, BeOS, RISC OS, IBM OS/390, Atari MiNT, Windows 95, and Windows NT.
Furthermore, it is compatible with all vi
versions (i.e. upward compatible). Besides the improved compatibility, vi
the most interesting addition to the classic implementation is the inclusion of support for multi-level undo, a graphical user interface.
It also features multiple tabs, an emulator terminal window for shell commands, syntax highlighting, spell checking, improved code indentation, and multi-language support.
A complete list of differences or additions and their comprehensive guide can be found using the following steps.
Open a terminal and enter the following.
vi [fileName]
If fileName
is a valid name of an existing file, vi
the editor opens that file. If there is no file with in the current directory , a new file with the same fileName
name is created .fileName
Suppose you want to create a sample.txt
new file named and Hello World
insert into it. Follow the steps given below and save the changes.
The above method can open existing files and modify them in the same way.
Whenever you open an existing file, the blinking cursor always points to the beginning of the first line. However, sometimes it is convenient to open a file with the cursor pointing to a specific line number in the file.
For example, after compiling a long code file, you might learn that there is a syntax error on line 563. You open the file and use the arrow keys to go to line 563.
This can take some time and slow down your debugging tasks. Therefore, both the vi
and vim
editors provide the ability to open an existing file at a specific line number.
vim [+command OR +lineNumber] fileName
To find a specific term, highlight and go to that specific term. Follow the syntax below.
vim [+/searchTerm] fileName
Please make sure all the above commands work fine in the editor. We need to replace vi
with in the above commands .vi
vim
Example: Suppose you want to open a file called at line 56. program.c
Just type the following in your bash terminal.
vim +56 program.c
or
vim +/56 program.c
The above command should output:
We can see that the cursor is pointing to the beginning of line 56.
vim
The editor does not display line numbers by default. We can use the command vim
in the interface set
to display line numbers.
:set number
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
How to decompress x.tar.xz format files under Linux
Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM
-
A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr
Summary of vim common commands
Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM
-
In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme
Detailed explanation of command return value $? in Linux
Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM
-
? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re
Common judgment formulas for Linux script shell
Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM
-
In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –
Shell script programming practice - specify a directory to delete files
Publish Date:2025/04/08 Views:98 Category:OPERATING SYSTEM
-
Usually, in Linux system we need to frequently delete some temporary files or junk files. If we delete them one by one manually, it will be quite troublesome. I have also been learning shell script programming recently, so I tried to write
Use of Linux command at - set time to execute command only once
Publish Date:2025/04/08 Views:158 Category:OPERATING SYSTEM
-
This article mainly involves a knowledge point, which is the atd service. Similar to this service is the crond service. The functions of these two services can be similar to the two functional functions of javascript. Those who have learned
Use of Linux command crontab - loop execution of set commands
Publish Date:2025/04/08 Views:170 Category:OPERATING SYSTEM
-
Compared with at , which executes a command only once, crontab, which we are going to talk about in this article, executes the set commands in a loop. Similarly, the use of crontab requires the support of the crond service. The service is s
Linux practice - regularly delete files under the directory
Publish Date:2025/04/08 Views:198 Category:OPERATING SYSTEM
-
Since we want to delete the files under the directory regularly, we need to use the Linux crontab command. And the content format of each work routine is also introduced in the format of each crontab work. Similarly, we need to use shell sc
How to use the Linux file remote copy command scp
Publish Date:2025/04/08 Views:151 Category:OPERATING SYSTEM
-
Scp copies files between two hosts over the network, and the data is encrypted during transmission. Its underlying layer uses ssh for data transmission. And it has the same authentication mechanism and the same security level as ssh. When u