Vim: Run multiple commands based on one: global command

Sorry if this has already been posted, because I can not find the answer even on the vim wiki.

Is there a way to run multiple commands in vim command line mode from a single :gsearch?

For instance,

:%g/foo/ s/bar/\=@a/g | exe "norm /cat\<enter>\"ayiw"

Which (for what I intend to do), in each line that matches foo, replace with the barcontents of the register aand then find the next iteration cat(even if there are many lines in front) and put the surrounding word in the register a.

Instead, this specific syntax completes the substitution command using the current contents of the source register a, and then executes the normal mode command on one line (after the replacement is complete).

This example is not my specific use case, but shows one instance where this functionality is useful. I understand that I could translate all this into one exe, i.e. %g/foo/exe "norm :s/bar/\\=@a/g\<enter>/cat\<enter>\"ayiw", but I would like to do this in the first way, since I believe that it is more flexible.


I would prefer to do this with vanilla vim, but if a plugin exists for this, this is a good alternative. Does anyone know if there is syntax for doing such a thing?

+4
source share
2 answers

, "" , ?

:let list = split(execute('g/cat/p'), '\n') | g/foo/ s/bar/\=matchstr(remove(list, 0), '\s\d\+\s\zs.*')/g

cat, . bar cat... ..

- matchstr. g//p , :

 1 cat
 2 cat
 3 cat
 ...

. , - ( vimscript, oneliner).

+1

( : s, : g). :

" SHORT STORY TITLES to single word of CapitalizedWords within <h3>s
.,$g/^\L\+$/s/[^A-Z0-9 ]\+//ge|s/\u\+/\L&/ge|s/\<\l\+\>/\u&/ge|s/ \+//ge|s/.*/<h3>&<\/h3>/
0

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


All Articles