How to Open a File at Specific Line Number Using Vi and Vim Editors
This tutorial demonstrates the method to open a plain text file with a specific line number with vi
or vim
editors using the Bash command-line interface (CLI).
First, we will briefly introduce the vi
editor and its successor vim
editor. Then, we will move right away to their use for opening a file with the cursor pointing to a particular line number.
Introduction to vi
and vim
Editors in Linux
The vi
editor is the most classic text editor available along almost all the Linux distributions. Therefore, it doesn’t require any extra packages to be installed before its use.
Moreover, the vi
is an easy-to-use CLI-based editor used for any plain-text file. Consequently, it is lightweight and fast.
The vim
editor is a successor to the vi
editor and stands for Vi IMproved
. It has added new functionalities and improved many primitive vi
editors.
It follows POSIX 1003.2-1 standard, which makes it compatible, virtually, with all the Unix and Linux distributions including MS-DOS, Macintosh, Amiga, VMS, BeOS, RISC OS, IBM OS/390, Atari MiNT, Windows 95, and Windows NT.
Moreover, it is compatible with all the vi
versions (i.e., upwards compatibility). The most interesting addition to classic vi
implementation other than improved compatibility is the inclusion of support for multi-level undo, a graphical user interface.
It also has multiple tab pages, an emulator terminal window for the Shell command, syntax highlighting, spell checking, improved code indenting, and multi-language support.
A complete list of differences or additions and their comprehensive guide can be found using the following steps.
- Open the terminal, type
vim
, and press enter. The following screen shall appear.
- Now, type
:help vi_diff.txt
. This will open thevim_diff.txt
file containing a comprehensive comparison betweenvi
andvim
. A snapshot for thevim_diff.txt
file is as under.
Create a New File With the vi
or vim
Editors
Open the terminal and type the following.
vi [fileName]
If the fileName
is a valid name of an existing file, the vi
editor will open that file. If there is no file, in the current directory, with the fileName
, then a new file is created with the name same as fileName
.
Insert Text in a File and Save the Modifications
Assuming you want to create a new file named sample.txt
and insert Hello World
into it. Follow the following steps to do the operation and save the changes.
- Open the file.
- Once the file is opened in the
vim
editor, pressi
to enter the insertion mode. The~
sign at the start of each line indicates that the line is just empty and hasn’t been used earlier.
- Start typing desired text or content. In our case, start typing
Hello World
.
- The changes can be saved by entering the command mode by pressing the Esc button, typing
-wq
(to save the file and quit the editor), and pressing the Enter key.
Open File at Specific Line Number in vi
or vim
The aforementioned method can be used in the same way to open an existing file and modify it.
Whenever an existing file is opened, the blinking cursor always points to the start 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 may get to know that there is a syntax error at line number 563. You open the file and go to line 563 using the arrow keys.
This may take time and slower your debugging task. Therefore, both the vi
and the vim
editors provide functionalities 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 particular term. Follow the syntax below.
vim [+/searchTerm] fileName
Please ensure that all the above commands work perfectly fine with the vi
editor. We need to replace vim
with vi
in the above commands.
Example: Assuming you want to open a file named program.c
at line number 56. Just type the following in the bash terminal to do that.
vim +56 program.c
OR
vim +/56 program.c
The above command shall output as:
We can see the cursor pointing at the beginning of line 56.
The vim
editor does not display the line numbers by default. We can show the line numbers using the set
command in the vim
interface.
:set number
vi
/vim
interface, press the Esc button to enter the command mode, type lineNumber
, and press Shift+G to jump the desired line number.Husnain is a professional Software Engineer and a researcher who loves to learn, build, write, and teach. Having worked various jobs in the IT industry, he especially enjoys finding ways to express complex ideas in simple ways through his content. In his free time, Husnain unwinds by thinking about tech fiction to solve problems around him.
LinkedIn