Wikia

Vim Tips Wiki

Changes: Vim as XML Editor

Edit

Back to page

(I have no idea what the edit was trying to do so I moved the actual content to the comments and restored the deleted stuff)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
" Vim syntax file
+
{{review}}
" Language: Xdebug trace files (context or unified)
+
{{TipImported
" Maintainer: Derick Rethans <derick@xdebug.org>
+
|id=583
" Last Change: 2010 Jun 06
+
|previous=582
  +
|next=584
  +
|created=2003
  +
|complexity=intermediate
  +
|author=Tobi
  +
|version=6.0
  +
|rating=548/162
  +
|category1=XML
  +
|category2=
  +
}}
  +
If Vim is your main text editor, and if you do a lot of XML editing, then this mini how-to might help you make that even more fun: [http://www.pinkjuice.com/howto/vimxml Vim as XML Editor].
   
" For version 5.x: Clear all syntax items
+
==Comments==
" For version 6.x: Quit when a syntax file was already loaded
+
There's a very helpful script at {{script|id=301|text=xmledit}}, but remember to add "syntax on" to your .vimrc.
if version < 600
 
syntax clear
 
elseif exists("b:current_syntax")
 
finish
 
endif
 
   
syn match begin "^TRACE START"
+
Overall, the above tutorial is good. I'd probably give it a 9 out of 10. For users that are already familiar with Linux style commands the tutorial is extremely good.
syn match end "^TRACE END"
 
syn match date "\[.*\]"
 
   
syn match min_memory "+\d\+"
+
I know that you say you abhor associating file extensions with content-types (which makes sense given your background). However, in a Windows environment I find it very convenient to use file extensions and most Windows users are probably used to this idea anyways... .doc files are Word, .xls is Excel, etc.
syn match pls_memory "-\d\+"
 
syn match nll_memory "+0"
 
   
syn match level "->"
+
Since you know the command line argument it isn't too hard to create the file associations. To set up (for example) an association for xmlstar on Windows 2000 you do the following:
syn match lineno ":\d\+$"
+
* Open up "My Computer"
  +
* Go to Tools -> Folder Options -> (tab) File Types
  +
* Find XML in the list (you can substitute any extension here)
  +
* Under "Details" in the box below see if "Advanced" is an option. If it is not, then write down what the current program name is and hit "Restore"
  +
* Under "Details" choose "Advanced"
  +
* Select the "New" button
  +
* In the "Action" box this is what shows up in the context menu. A good name here is XMLSTAR.
  +
* In the "Application used to perform this action" box you type the following:
  +
cmd.exe /k "C:\path\to\xmlstar\xml.exe -flags_go_here %1" but you replace the flags_go_here with whatever flags you want.
   
syn match result ">=>.\+"
+
'''Note:''' The variable %1 in the file types menu is going to be the name of the XML file and AFAIK you can't pass any other command line options this way. But, this gives you a quick and easy way to open up a file with XMLSTAR. You can of course modify what I've said above to enable VIM for .txt files, or xmllint for XML files, or ...
syn match assignment "=> \$[a-z]\+\(->[a-z]\+\)*\ .*="
 
syn match assignment "=> \$[a-z]\+\['\([a-z]\+\)*'\]\ .*="
 
   
syn match methodcall "\k\+->"
+
----
syn match staticcall "\k\+::"
+
If you want to just check the wellformedness of the xml document then from the vi editor you can use
syn match functionb "\k\+("
 
syn match functione ") "
 
   
syn match main "{main}()"
+
!xmllint --noout %
syn match include "include\(_once\)\=('.\+')"
 
   
" Define the default highlighting.
+
for more on this command just give
" For version 5.7 and earlier: only when not done already
 
" For version 5.8 and later: only when an item doesn't have highlighting yet
 
if version >= 508 || !exists("did_diff_syntax_inits")
 
if version < 508
 
let did_diff_syntax_inits = 1
 
command -nargs=+ HiLink hi link <args>
 
else
 
command -nargs=+ HiLink hi def link <args>
 
endif
 
   
HiLink begin Label
+
:!xmllint
HiLink end Label
 
HiLink date Label
 
HiLink assignment Label
 
   
HiLink level SpecialChar
+
on opening an xml file using vi or vim editors
HiLink result Constant
 
   
HiLink min_memory Constant
+
----
HiLink pls_memory Structure
+
To set up syntax folding automatically for XML files put the following lines in your .vimrc
HiLink nll_memory Comment
+
let g:xml_syntax_folding=1
+
au FileType xml setlocal foldmethod=syntax
HiLink main Structure
+
Hint: If no folds are found, make sure syntax highlighting is on.
HiLink include Structure
+
----
+
script at {{script|id=301|text=xmledit}}, but remember to add "syntax on" to your .viabhor associating file extensions with content-types (which makes sense given your background). However, in a Windows environment I find it very convenient to use file extensions and most Windows users are probably used to this idea anyways... .doc files are Word, .xls is Excel, etc.
HiLink lineno Delimiter
 
HiLink methodcall Function
 
HiLink staticcall Function
 
HiLink functionb Function
 
HiLink functione Function
 
 
delcommand HiLink
 
endif
 
 
let b:current_syntax = "xt"
 
 
:set foldmethod=expr
 
:set foldexpr=strlen(substitute(substitute(substitute(substitute(getline(v:lnum),'^TR.*$','',''),'\\s>=>','->',\"g\"),'^\\s.\\{20\\}\\(\\s\\+\\)\\?->.*$','\\1',''),'\\s\\s','\ ',\"g\"))-2
 
:set foldlevel=9999
 
 
" vim: ts=8 sw=2
 

Latest revision as of 17:40, December 22, 2013

Tip 583 Printable Monobook Previous Next

created 2003 · complexity intermediate · author Tobi · version 6.0


If Vim is your main text editor, and if you do a lot of XML editing, then this mini how-to might help you make that even more fun: Vim as XML Editor.

CommentsEdit

There's a very helpful script at xmledit, but remember to add "syntax on" to your .vimrc.

Overall, the above tutorial is good. I'd probably give it a 9 out of 10. For users that are already familiar with Linux style commands the tutorial is extremely good.

I know that you say you abhor associating file extensions with content-types (which makes sense given your background). However, in a Windows environment I find it very convenient to use file extensions and most Windows users are probably used to this idea anyways... .doc files are Word, .xls is Excel, etc.

Since you know the command line argument it isn't too hard to create the file associations. To set up (for example) an association for xmlstar on Windows 2000 you do the following:

  • Open up "My Computer"
  • Go to Tools -> Folder Options -> (tab) File Types
  • Find XML in the list (you can substitute any extension here)
  • Under "Details" in the box below see if "Advanced" is an option. If it is not, then write down what the current program name is and hit "Restore"
  • Under "Details" choose "Advanced"
  • Select the "New" button
  • In the "Action" box this is what shows up in the context menu. A good name here is XMLSTAR.
  • In the "Application used to perform this action" box you type the following:
cmd.exe /k "C:\path\to\xmlstar\xml.exe -flags_go_here %1" but you replace the flags_go_here with whatever flags you want.

Note: The variable %1 in the file types menu is going to be the name of the XML file and AFAIK you can't pass any other command line options this way. But, this gives you a quick and easy way to open up a file with XMLSTAR. You can of course modify what I've said above to enable VIM for .txt files, or xmllint for XML files, or ...


If you want to just check the wellformedness of the xml document then from the vi editor you can use

!xmllint --noout %

for more on this command just give

:!xmllint

on opening an xml file using vi or vim editors


To set up syntax folding automatically for XML files put the following lines in your .vimrc

let g:xml_syntax_folding=1
au FileType xml setlocal foldmethod=syntax

Hint: If no folds are found, make sure syntax highlighting is on.


script at xmledit, but remember to add "syntax on" to your .viabhor associating file extensions with content-types (which makes sense given your background). However, in a Windows environment I find it very convenient to use file extensions and most Windows users are probably used to this idea anyways... .doc files are Word, .xls is Excel, etc.

Around Wikia's network

Random Wiki