How to replace outsoles with numeric numbers starting with 1 at the beginning of each line in vim

I want to replace each profile with a line with an incremental index starting with 1 at the beginning of each line, so the replacement lines will be varargin{1} , varargin{2} , varargin{3} ... For large numbers, the number line should naturally be more than 1 character, for example: ... varargin{9} , varargin{10} ... Input is MATLAB code, sample inputs and required outputs are presented below. I am primarily looking for a vim solution, but other ways to do this are also welcome.

The expression below creates indexes starting with 1 , but they change only for each row.

 :let @a=1 | %s/\v.*'\zs.*\ze\);/\=substitute(submatch(0), '\s[a-zA-Z0-9{}_.]*', ' varargin{'.(@a+setreg('a',@a+1)).'}', 'g')/g 

My question is:

How can I reset index 1 at the beginning of each line and increment the index by 1 between each send?

The above code is a modified version of the Replace with Ascending Numbers example provided at http://vim.wikia.com/wiki/Substitute_with_incrementing_numbers :

 :let @a=1 | %s/abc/\='xyz_'.(@a+setreg('a',@a+1))/g 

Input Example # 1:

 messages.msg1.English = xprintf('analysis directory is on %s\n', analysis_dir); 

The required output, for example, input No. 1:

 messages.msg1.English = xprintf('analysis directory is on %s\n', varargin{1}); 

Input Example # 2:

 messages.msg15.English = xprintf('the following sessions (%d pcs) have been approved: %s', handling_struct.n_of_accepted, handling_struct.accepted_sessions_vector); 

The required output, for example, input No. 2:

 messages.msg15.English = xprintf('the following sessions (%d pcs) have been approved: %s', varargin{1}, varargin{2}); 

Input Example # 3:

 messages.msg19.English = xprintf('looking for files ''%s'' in %d separate dirs', give_file_struct.regex, number_of_dirs); 

The required output, for example, input No. 3:

 messages.msg19.English = xprintf('looking for files ''%s'' in %d separate dirs', varargin{1}, varargin{2}); 
+4
source share
2 answers

In this case, I would use the following command.

 :g/^/let n=[0] | s/abc/\='xyz_'.map(n,'v:val+1')[0]/g 
+4
source

It might be a hacker solution, but if I wanted to do it quickly without a function, I would do something like this:

 :let i=1<cr> "set the index variable qbf lcEvarargin{<Cr>=i<cr>},<esc>:let i+=1<cr>q "record a macro u "undo those changes :%norm$F':let i=1<Cv><Cj> 1000@b <cr> "run the macro on each line :%s/,$/);/<cr> "clean up 

Note that I use <> notation from vim to denote special keys, so <cr> means carriage return and <Cr> means CTRL-R (see :h i_CTRL-R ). <Cv><Cj> places the literal character ^@ on the command line. This allows the normal mode to "press enter" without ending the command. I use :norm instead of a macro, because when :norm throws an error, it just moves on to the next line. When a macro throws an error (for example, it tries to do ft when there is no more t in the line), then it will just work completely. That way, I can run the macro 1000 times and just keep moving as soon as it fails. Again, perhaps not the most elegant solution, but it works, and my laziness usually wins.

+1
source

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


All Articles