1

I have some specific parts of files with unformated xml code. I need to write a vimscript function that selects the text and calls xmllint over it.

I know I can do this on the command line :'<,'>!xmllint --format -

But I really need to do the same in a vimscript function and I don't know how to make something like normal! call for visual.

I tried this but it does not work correctly :

function! MyFormat()
    ... stuff done here
    let startl = line("'<")
    let endl = line("'>")
    let line = getline(startl, endl)
    let r = system('echo "' . join(line, "") . '" | xmllint --format -')

    call setline('.', r)
endfunction
3
  • You need to or you want to? The command you gave seems to do the job fine so what problem, exactly, are you trying to solve and how do you plan to use that function? Commented Mar 30, 2021 at 17:20
  • And your question lacks the level of detail necessary for it to be answered properly, most importantly, what you have tried. Commented Mar 30, 2021 at 18:26
  • I have updated my question. I think maybe I could have an issue with quotes in string injecte in the system function Commented Mar 30, 2021 at 19:50

1 Answer 1

2

Every line in a Vim script is an Ex command. Since you already have a working Ex command, you might as well use it.

function! MyFormat()
    " ... stuff done here
    '<,'>!xmllint --format -
    " ... more stuff done here
endfunction

But, again, data is missing so this might work… or not, be sufficient… or not, etc.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.