I'm editing DocBook XML files and sometimes I need to format the using an external xmlformat command. it is easy to run do this on the whole buffer of text, but how can i achieve the same only for a visually selected lines of text?

EDIT1: simply running !xmlformat on selected lines works. the problem is that i need to do more things to the lines, such as get the number of indent of the first line, do the formatting, then shift the block to the indent size so that it aligns with the rest of the text.. It's summarized in https://github.com/tbazant/vim-daps/issues/38 BTW

EDIT2: the function in my plugin should be defined the way that it can handle either no input (run xmlformat on the whole file/buffer) or range of selected lines

  • 1
    Does simply typing !xmlformat work? If not, maybe you could edit the question to explain why. :help v_!
    – Rich
    Mar 25 at 14:22
  • To more precisely I think Rich means using :'<,'>!xmlformat after a visual selection: '< and '> are marks which refer to the last visually selected text.
    – statox
    Mar 25 at 14:34
  • 1
    Question header is quite different to the body I would say. There is a way to run a vimscript function on visually selected lines but here it is about filtering of visual selection using external tool.
    – Maxim Kim
    Mar 25 at 14:52
  • 1
    @statox: Yes, but you don't need to type the colon or the range, simply pressing ! directly from the visual selection will add both automatocally. (I know you know this already.)
    – Rich
    Mar 25 at 15:13
  • Right @Rich I was just giving more details as I think it can be confusing for someone who doesn't know that :) But your point about editing the question if that doesn't work is still completely relevant
    – statox
    Mar 25 at 15:18

Your Answer

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

Browse other questions tagged or ask your own question.