How to display line numbers in Vim / Vi in

Vim / Vi is a lot of software developers and system administrators choose Linux text editor.

By default, Vim does not display line numbers, but you can easily open them. Vim supports three rows numbering scheme to help you browse the files. In addition to standard absolute line number, Vim also supports mixed relative row and row number mode.

In this guide, we'll show you how to display or hide line numbers in Vim / Vi text editor.

In addition to helping navigating through code, the row number in other cases (e.g., pair programming, debugging script code inspection, refer to a specific row, etc.) are also useful.

Absolute line number

Absolute line number is the standard line number, it displays the appropriate line numbers next to each line of text.

To activate the row number, set the digital signs:

  1. Press Esc to switch to the command mode.
  2. Press :( colon), the cursor will move to the lower left corner of the screen. Enter the set number or set nu and click Enter.

    :set number
  3. The line number will be displayed on the left side of the screen:

To disable the absolute line number, run: set nonumberor set nonu command:

:set nonumber

You can also use: set number or:! Set nu switch!:

:set number!

Relative line numbers

If enabled relative row number, line number of the current line is shown as 0, and the lines above and below the current row number increments (1, 2, 3 ... etc.).

Line mode is relatively easy, since many operations Vim (e.g., up / down and deleting rows) are carried out on the opposite line number.

For example, to delete the ten lines below the cursor, you can use d10j command. After enabling the relative line numbers, you will have a more intuitive understanding of the code.

To enable the relative row number, switch to command mode, then type: set relativenumber or: set rnu:

:set relativenumber

To disable the relative row number, enter: set norelativenumber or set nornu:

:set nonumber

To switch the relative line number, use: set relativenumber or:! Set rnu command!:

:set number!

Mixed line number

In Vim 7.4 and later, enable both absolute and relative line number line number will set the blending mode.

混合行编号与相对行编号相同,唯一的区别是当前行(未显示) 0 显示了其绝对行号。

要打开混合线路编号,请同时运行 number 和 relativenumber 命令:

:set number relativenumber

可以通过逐个运行命令来实现相同的目的:

:set number :set relativenumber

要禁用混合模式,您需要同时关闭绝对编号和相对编号。

永久设定

如果希望每次启动 Vim 时都显示行号,请将适当的命令添加到您的 Vim 配置文件 .vimrc 中。例如,要启用绝对行编号,应添加以下内容:

vim ~/.vimrc
:set number

总结

要在 Vim 中显示行号,请使用 :set number 命令显示绝对行号,使用 :set relativenumber 显示相对行号。如果同时启用了绝对行号和相对行号,则 Vim 会切换到混合行号模式。

Guess you like

Origin www.linuxidc.com/Linux/2019-10/160979.htm