In this function I would like to substitute everything within (" and ") with emptiness

function! ListJoinFolds() abort
    let bufnr = bufnr('%')
    let items = []
    while 1
        let start = search('^{{{', 'cW')
        if !start
        let end = search('^}}}', 'W')
        let lines = getline(start, end)
        let lines = map(lines, 'trim(v:val)')
        call add(items, {
            \ 'bufnr': bufnr,
            \ 'lnum': start,
            \ 'text': join(lines, ' '),
        \ })
        " credits to: https://www.reddit.com/r/vim/comments/akv0wl/function_sort_output_alphabetically/ef8nlrr
        call sort(items, {a, b -> a["text"] == b["text"] ? 0 : a["text"] > b["text"] ? 1 : -1})
    call setloclist(winnr(), items)

I've added the following nearly every thinkable order I could think of, but my intelligence couldn't find the right place to succeed for the attempted success.

let lines = substitute(items, '(".\{-}")', '', 'g')

Hopefully someone could assist me with this.

Thank you.


I've also posted this question to:

reddit.com/r/vim (link)

If question solved, I will post it here, and vice versa.

  • You might want to update vi.stackexchange.com/questions/18733/… now that you've solved that problem.
    – user4879
    Feb 9 '19 at 6:19
  • Braces can be embedded. Even with a correct syntax, we can have things like f(g(), h(y())).. And unfortunately, regexes are really bad with recursive patterns. searchpair() could have helped, but it works on buffers, not on strings. May be there is a Python library somewhere that can help, in that case it may be better to delegate your problem to that library from your vim function. Jul 9 '19 at 17:25

substitute() only works on a string. You appear to have been trying it with the items list. Does something like this work for you (as a modification of the part where you add the text to the items list)?

    call add(items, {
        \ 'bufnr': bufnr,
        \ 'lnum': start,
        \ 'text': substitute(join(lines, ' '), '(".\{-}")', '', 'g'),
    \ })

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.