Reputation: 808
I am using vim with latex-suite plugin.
I have a piece of text, that I want to enclose in verbatim
environment. Let's say it looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<errors>
<error>Record not found</error>
</errors>
So I visually select text, press <F5>
, type verbatim
and press <CR>
. I get:
\begin{verbatim}
<?xml version="1.0" encoding="UTF-8"?>
<errors>
<error>Record not found</error>
</errors>
\end{verbatim}
And what I want to get is:
\begin{verbatim}
<?xml version="1.0" encoding="UTF-8"?>
<errors>
<error>Record not found</error>
</errors>
\end{verbatim}
Is there a way to prevent enclosing in environment using this mechanism from removing leading whitespaces? I have a lot xml samples, and I do not want to indent them or enclose in environment manually.
Upvotes: 1
Views: 212
Reputation: 56438
One way to do this is to embed XML syntax inside Tex syntax, then use the XML indentation rules on the XML code as determined by the syntax highlighting attribute of the lines in question.
With that you can just run the =
indentation over a region and it should do the right thing indentation wise. Or maybe even it'll indent correctly first time.
First, make a .vim/after/syntax/tex
directory and add the following code as .vim/after/syntax/tex/xmlembed.vim
:
if exists("b:current_syntax")
unlet b:current_syntax
endif
syn include @XML syntax/xml.vim
syn region XMLEmbedded matchgroup=XMLCommand
\ start=+\\begin{verbatim}\n\+<?xml+rs=s+16
\ skip=+\\$+
\ end=+\\end{verbatim}+ contains=@XML
syn cluster texZone add=XMLEmbedded
hi def link XMLCommand texZone
func! TexXML(lnum)
let name = tolower(synIDattr(synID(a:lnum, 1, 1), "name"))
if name =~ 'xml'
return XmlIndentGet(a:lnum, 1)
endif
return -1
endfunc
runtime indent/xml.vim
set indentexpr=TexXML(v:lnum)
What that does is include XML syntax highlighting as @XML and define a region bounded by the begin/end (and also matching \begin{verbatim} part but include the <?xml is perhaps overkill, but just in case you want other non-xml verbose regions it might be worth having.
After that this snippet imports the XML indent rules with runtime indent/xml.vim
. XML is indented by an expression, XmlIndentGet, but instead of that the indent expression is set to a wrapper function TexXML
. This checks if the current line "looks like xml", and if so indents it like XML, otherwise uses the current indentation. For a line to look like XML the function simply checks if the syntax highlighting matches "xml".
You need to have syntax highlighting on for this to work.
Upvotes: 1