Can I use variable in pattern of substitute() in script?

let row = getline(j) " 'trallala'
let rownext = getline(j+1) " 'bimbam'
let row = substitute(row, "^\\\(.*\\\)$", "\\1 rownext", "g") "
'trallala bimbam'

"yes" is the short answer, though you have to

1) concat it (if there's something to which it must be added...i.e. it must not be within a string) and
2) escape it if there are any metachars in it...something like

  let row=substitute(row, '$', ' '.escape(rownext, '\\*.'), 'g')

If you use the single quotes rather than the double-quotes, you'll have less backslash escaping to do.

This doesn't actually change the line in your document. You can do that, however, by doing something like

  :s/$/\=' '.getline(line('.')+1)

This can be handily combined with a :g command to do things like

  :g/regexp/s/$/\=' '.getline(line('.')+1)

which will find every line containing "regexp" and append a space in addition to a copy of the following line.

If you just want to join the two lines, you can just use the "J" command...or if you want to join the two variables, you can just concat them:

  let row = row.' '.rownext

but your example may have been a simplified version of things for which this wouldn't work.

You can read more at

        :help expr
        :help sub-replace-\=
        :help J
        :help expr-.    

Just a few ideas...HTH,

-tim



Reply via email to