Search code examples
vimneovim

How to run a substitute command with bufdo?


I searched the docs for a bit, but cannot seem to find a satisfying answer.

Basically I want to insert a new line at the end of every buffer and fill this line with the modified filename of the open buffer.

Example :

SomeText
\<old_eof\>

becomes

Some text
result of substitute(expand("%t"), "txt", "csv", "") and some wrapping text
\<new_eof\>

I already tried it with

:bufdo normal Go<C-R>=substitute(expand("%t"), "txt", "csv", "")

However, this solely outputs <C-R>=substitute(expand("%t"), "txt", "csv", "")

EDIT:

I found a partial solution by simply recording a macro and using :bufdo normal @a However, this is not my desired way to go.


Solution

  • How do you do that <C-R>? Do you press <, C, -, R, and > separately? Do you press Ctrl+R?

    Anyway, using a normal mode macro doesn't sound like a good way to do what you want. :help :put would be more appropriate:

    :bufdo $put=substitute(expand('%'), 'txt', 'csv', '')
    

    where $ is the address of the last line.

    Note that I replaced the double quotes with single quotes because, well… let's say that :put has its idiosyncrasies. I also removed the seemingly useless t in %t (or did you mean %:t?).

    --- EDIT ---

    This can be improved substantially by using :help filename-modifiers:

    :bufdo $put=expand('%:s/txt/csv/')
    

    or, if you meant %:t:

    :bufdo $put=expand('%:t:s/txt/csv/')