Skip to main content
2 of 4
added 284 characters in body
SergioAraujo
  • 1.2k
  • 11
  • 13

I thin you could use the function setline() in conjunction with substitute().

In neovim I am using something like this:

M.changeheader = function()
    -- We only can run this function if the file is modifiable
    local bufnr = vim.api.nvim_get_current_buf()
    if not vim.api.nvim_buf_get_option(bufnr, "modifiable") then
        require("notify")("Current file not modifiable!")
        return
    end
    -- if not vim.api.nvim_buf_get_option(bufnr, "modified") then
    --     require("notify")("Current file has not changed!")
    --     return
    -- end
    if vim.fn.line("$") >= 7 then
    os.setlocale("en_US.UTF-8") -- show Sun instead of dom (portuguese)
    local time = os.date("%a, %d %b %Y %H:%M:%S")
    local l = 1
    while l <= 7 do
        vim.fn.setline(l, vim.fn.substitute(vim.fn.getline(l), 'last (change|modified): \\zs.*', time , 'gc'))
        l = l + 1
    end

        require("notify")("Changed file header!")
    end
end

A pure vimscript would be:

fun! SubstiTute()
    let l = 1
    let limit = line('$')
    while l < limit
        call setline(l, substitute(getline(l), 'this', 'that' , 'gc'))
        let l = l + 1
    endwhile
endfun
SergioAraujo
  • 1.2k
  • 11
  • 13