Wednesday, May 19, 2021

using a variable in a substitute command

I have a bunch of headers of the form:

| # <header content>

... in a few files conveniently named: f01.md, f02.md... fnn.md and
I was looking for a way to number these headers and add a constant
literal so that after processing they would look something like this:

| # \textit{<number>: <header content>}

While editing the files, I ran a quick test issuing the following
ex-mode commands:

| :let g:counter =0
| :bufdo :g/^#/let g:counter = g:counter +1 | echom g:counter

As expected this causes vim to display something like:

| 1
| 2
| 3
| ...
| 44
| 45

So far so good.

Now I tried to replace the "echom g:counter" command by a :s(ubstitute)
command, e.g.

| :s/^# \(.*$\)/# \\textit{\=g:counter: \0}

Unfortunately this is what I got:

| # \textit{=g:counter: <header content>}
| ... i.e.
| # \textit{=g:counter: title of chapter #1}
| # \textit{=g:counter: title of chapter #2}
| # \textit{=g:counter: title of chapter #3}
| etc.

In other words what gets substituted is (among other things) the *name*
of the variable NOT its contents.

Can this be fixed (syntax problem?) or should I take an altogether
different approach?

CJ

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/YKWcXORgKMSJA5/b%40turki.local.

No comments: