JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Open a file at a specific line number using Vi and Vim editors

Author:JIYIK Last Updated:2025/04/06 Views:

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 vior vimeditor.

First, we will briefly introduce vithe 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.

viThe 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, viis an easy to use CLI based editor for any plain text file. Hence, it is lightweight and fast.

vimThe editor is vithe successor to the editor, Delegate Vi IMproved. It adds new features and improves many of the original vieditor.

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 viversions (i.e. upward compatible). Besides the improved compatibility, vithe 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.

VIM

VIM2

Open a terminal and enter the following.

vi [fileName]

If fileNameis a valid name of an existing file, vithe editor opens that file. If there is no file with in the current directory , a new file with the same fileNamename is created .fileName

Suppose you want to create a sample.txtnew file named and Hello Worldinsert into it. Follow the steps given below and save the changes.

VIM3

VIM4

VIM5

VIM6

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 viand vimeditors 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 viwith in the above commands .vivim

Example: Suppose you want to open a file called at line 56. program.cJust type the following in your bash terminal.

vim +56 program.c

or

vim +/56 program.c

The above command should output:

VIM7

We can see that the cursor is pointing to the beginning of line 56.

vimThe editor does not display line numbers by default. We can use the command vimin the interface setto 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.

Article URL:

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 –

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

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial