Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Vim uses the method of relative line number

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you the method of Vim using relative line numbers, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Preface

As we all know, one of the advantages of using Vim is that it is highly customizable, and if you encounter any pain points that make you uncomfortable, you can solve it by configuring or even developing a plug-in. After I started using Vim for a while, I found a very "anti-human" place: many of the commands in Vim need to be checked. For example, "delete a function body", you need to count how many lines the function occupies (for example, 5 lines), and then press 5dd. If you use the mouse, you don't have to count, you can drag it directly, but the efficiency of the mouse is too low. Now that you've learned Vim, don't let your hands leave the keyboard.

I have summed up a scheme that almost all operations can be done without counting.

Vim supports two line numbers, relative line number and absolute line number. The relative line number shows how many lines are separated from the line of the cursor, and the absolute line number is the serial number of each line displayed sequentially.

Vim uses relative

Set nu opens the line number display, set rnu opens the relative line number display (more information can be seen: help nu and: help rnu.

The effect is as follows:

Turn on the line number display

Turn on the display of relative line numbers

Open both relative and absolute line numbers

As you can see, if you open both at the same time, the absolute line number is displayed on the current line, and the distance from the current line is displayed.

I've found for a while that in normal mode, I often operate on multiple lines, so if you use absolute line numbers, you have to subtract or count to determine the number of lines. So the relative line number should be used in normal mode. 0 has no effect on us, so the current line should show the line number.

Then some common operating habits should be changed accordingly:

Delete n lines. 3dd is deleted, including the current line, a total of 3 lines, but because the display is the relative line number, we want to "delete until the target line", or unconsciously + 1, such as in the last picture above, to delete until the "terminal color matching installation" content, press 4dd. Is there any way to avoid calculation and WYSIWYG? Yes, d3j will be fine. Command+ until the target line number + direction. For example, delete until the above "I am right." The content is d4k. The rest of the operation is similar, such as moving to the next four lines, using either 4gg or g4j. Others include yank operation and so on. The replacement range could have been an absolute line number, for example, to replace 10-20 lines, that is: 10Magne20sCharger xxxUniGbut because our normal mode shows a relative line number, then we have to operate according to the relative line number::. + 2grade.account12sUniple xxxxhand yycharg (assuming it is currently on line 8). . To represent the current line in Vim, be good at using it. Because we show the relative line number, basically all the operations related to the line number need to be calculated. Add / subtract the relative line number displayed (WYSIWYG)

In insert mode, we usually edit and do not judge multiple lines, so we may be more concerned about the absolute line number (how many lines we have written, etc.). So you can listen for events, use absolute line numbers when entering edit mode, and relative line numbers when exiting edit mode and entering normal mode.

The settings are as follows:

Set nuaugroup relative_numbser autocmd! Autocmd InsertEnter *: set norelativenumber autocmd InsertLeave *: set relativenumberaugroup END

Set nu is because I usually open a file not necessarily for editing purposes, browsing the absolute line number is more convenient. This setting allows you to use the absolute line number when you enter the file for the first time, and display the relative line number when you enter edit mode and then exit to normal mode.

After using this for a period of time, I feel that I have pressed more keys, but I need much less calculation in my mind, and it is not easy to make mistakes.

Finally, my .vimrc is attached for reference. Blind copying is not recommended.

Related problems

In versions prior to Vim 7.4, when the relative line number is enabled, the current line number will become 0, and the normal line number cannot be displayed. In Vim 7.4, the current line number can be displayed normally and the relative line number can be displayed at the same time. Here are two pictures to compare.

Vim 7.3

Vim 7.4

From the picture above, when the relative line number is enabled, you can directly see the number of lines above / below the current line distance.

You can enable this feature using the following instructions

Set relativenumber

After enabling the relative line number, you no longer have to bother to count in order to copy multiple lines.

The above is all the content of Vim using the relative line number method, thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 263

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report