0

I have following xml file:

<?xml version="1.0" encoding="UTF-8"?>
<graph id="G" edgedefault="undirected">
    <edge id="e6" source="n5" target="n4"> <data key="d1">1.1</data> <data key="d2">1.1</data> </edge>
</graph>

I have reconfigured formatprg for xml in ~/.vim/after/ftplugin/xml.vim:

setlocal formatprg=tidy\ -xml\ -quiet\ --indent\ auto\ --tidy-mark\ no\ --indent-spaces\ 2\ --vertical-space\ yes\ 

When I now press gqq on line 3, I get

<?xml version="1.0" encoding="UTF-8"?>
<graph id="G" edgedefault="undirected">
<edge id="e6" source="n5" target="n4">
  <data key="d1">1.1</data>
  <data key="d2">1.1</data>
</edge>
</graph>

I want to fix the indentation automatically.

My first try was :'[,']norm = which does not work as expected.

However, =at does.

Ideally, I want to extend gq<text object/motion> and gqq so re-indentation (=, indentexpr) is done on the newly formatted lines.

1 Answer 1

0

Linewise gqq with support for counts

I have come up with following solution for the linewise operator gqq by putting

nnoremap <buffer> gqq gqq$='[

into ~/.vim/after/ftplugin/xml.vim (the $ was important). Counts work already:

<?xml version="1.0" encoding="UTF-8"?>
<graph id="G" edgedefault="undirected">
    <edge id="e6" source="n5" target="n4"> <data key="d1">1.1</data> <data key="d2">1.1</data> </edge>
    <edge id="e7" source="n3" target="n4"> <data key="d1">1.1</data> <data key="d2">1.1</data> </edge>
</graph>

Again cursor on line 3 and then press 2gqq. The result is

<?xml version="1.0" encoding="UTF-8"?>
<graph id="G" edgedefault="undirected">
    <edge id="e6" source="n5" target="n4">
        <data key="d1">1.1</data>
        <data key="d2">1.1</data>
    </edge>
    <edge id="e7" source="n3" target="n4">
        <data key="d1">1.1</data>
        <data key="d2">1.1</data>
    </edge>
</graph>

Vim-operator-user re-mappings of gq<text object/motion> and {VISUAL}gq

Install the plugin vim-operator-user and put

map <buffer> gq <Plug>(operator-myformat)
call operator#user#define('myformat', 'Op_format_and_reindent')
function! Op_format_and_reindent(motion_wiseness)
  exe "normal! '[V']gq$='["
endfunction

into ~/.vim/after/ftplugin/xml.vim.

This should also work for other files with a 'formatprg' and the desire to reindent automatically afterwards.

Your Answer

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

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