Converting N character followed by line numbers

I manually edit text files with Gcode CNC and need a way of referencing file locations and toolpaths. I want to change each line in a text file so that it starts with an uppercase character N, followed by line numbers that increase by tens for each consecutive line, then a space, followed by the original text file. Can I do this in vi?

+4
source share
5 answers

I'm not sure about vi, but (since you use the vim tag) Vim allows you to complete your task as follows:

  • Adjust the first line manually (enter N10 at the beginning of the line), then place the cursor at the beginning of the next line.

  • Press qb to start recording the macro ( b refers to the register used to store the macro, feel free to use a different letter - and definitely use a different letter if you have something useful hidden in b ).

  • Move the cursor up to the beginning of the previous line (which you manually adjusted). Press v to start the visual selection mode, then f to move the cursor to the next place in the line (if you use one space as a space separator, that is, adjust this step if you use a tab or several spaces).

  • Press y to remove the selected text. It will also remove the visual selection.

  • Move the cursor to the beginning of the next line. Press P to insert previously pulled text before the cursor, that is, at the very beginning of the line.

  • Move the cursor to the numeric part of the row header. Press 10 Ca (1, 0, control + A) to increase this number by 10.

  • Move the cursor to the beginning of the next line. Press q to stop recording the macro.

  • Press 10000000 @b to execute the macro 10,000,000 times or until it reaches the end of the file. This should be enough to take care of all the lines of your file, unless it is really huge, in which case use a larger number.

... or use Vim to write a simple script to complete the task in any other language you like best, then run it from the terminal (or using Vim with something like :!./your-script-name ) .; -)

+2
source

There are two possible ways to do this without using external tools: macro or Vim-script. In my opinion, the first method is a bit cumbersome (and probably not as effective as the solution given below).

The second method can be implemented as follows (put the code in your .vimrc or send it in another way):

 function! NumberLines(format) range let lfmt = (empty(a:format) ? 'N%04d' : a:format[0]) . ' %s' for lnum in range(a:firstline, a:lastline) call setline(lnum, printf(lfmt, lnum, getline(lnum))) endfor endfunction command! -range=% -nargs=? NumberLines <line1>,<line2>call NumberLines([<f-args>]) 
Function

NumberLines lists all the lines of the file in the specified range and adds to each line its number in accordance with the specified printf-format (default is "N% 04d"). To simplify the use of this function, I am creating a new command here that accepts a range of lines for processing (by default, the entire file) and an optional format for the line number argument.

0
source

The deceptive answer:

 :%!awk '{print "N" NR "0", $0}' 
0
source

The following command will add 'N < line number * 10 >' for each line.

  : g / ^ / exe 'normal!  0iN '.  (line ('.') * 10).  ''
0
source

You can do this easily in Vim with this:

 :%s/^/\=line(".")*10 . " "/ 

This replaces the beginning of each line with the result of an expression that gives a line number ten times, followed by a space.

I did not time it, but I suspect that it is noticeably faster than other Vim solutions.

0
source

Source: https://habr.com/ru/post/1307387/


All Articles