Align less row numbers to the left

Source: Internet
Author: User

Use Vim to display row numbers, and use less to display row numbers. it is too troublesome to find something without a line number. you do not know whether to display the row number. after reading the man manual, I found that-N can be used, but the speed will be slightly slowed down when opening large files. it's okay. therefore, "alias less = less-n" is set ". then I found another problem. this line number is too awkward. there is a blank in front of the boss, followed by a line number, and then a space is followed by the body, which is easy to confuse. so I want to separate the line numbers. It would be better if we could color them.

I checked man and didn't see anything about the color except the ineffective-R. after checking on the internet,-R is used to parse ANSI color code, rather than less can use color. as for alignment, I don't know how to check it ......

So I thought about the open source. Change the source code. download the latest less version from GNU, 418. the version 394 was used on the machine and was slightly upgraded. decompress the package and you will see a bunch of files. then start searching. I don't know which part of the file is used to print the row number. I posted a post on linuxsir, but no answer is provided yet. fortunately, after reading several files, it was a coincidence that I turned to line. c. the key point was found just a few lines. the key part is written as follows:

 1 if (linenums == OPT_ONPLUS) 2         {     3                 char buf[INT_STRLEN_BOUND(pos) + 2]; 4                 int n; 5   6                 linenumtoa(linenum, buf); 7                 n = strlen(buf); 8                 if (n < MIN_LINENUM_WIDTH) 9                         n = MIN_LINENUM_WIDTH;10                 sprintf(linebuf+curr, "%*s ",n, buf);11                 n++;  /* One space after the line number. */12                 for (i = 0; i < n; i++) 13                         attr[curr+i] = AT_NORMAL;14                 curr += n;15                 column += n;16                 lmargin += n;17         }

The main points are in the sentence "sprintf (linebuf + curr," % * s ", N, Buf. the row number is printed out, and a blank space is provided for the place with the minimum length min_linenum_width. if I want to remove these blank spaces, I just need to judge the entire condition n = min_linenum_width, and then change it to sprintf (linebuf + curr, "% s" BUF );". in this way, when the row number is changed from one digit to two digits, and from two to three digits, the entire body will be offset by one character, which looks very bad. therefore, we still need to keep those blank spaces. At most, we can reduce min_linenum_width. however, right alignment is really intolerable. so I finally changed it to "sprintf (linebuf + curr," %-* s ", N, Buf);" Add a "-" in front to align it to the left.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.