Vim Tips Wiki
Advertisement

Proposed tip Please edit this page to improve it, or add your comments below (do not use the discussion page).

Please use new tips to discuss whether this page should be a permanent tip, or whether it should be merged to an existing tip.
created May 13, 2008 · complexity basic · author Jerojasro · version 7.0

This tip describes how to configure syntax highlighting and folding for MoinMoin files. It is quite useful with editmoin (a tool allowing use of Vim to edit MoinMoin wiki pages).

Syntax highlighting

Install the moin.vim script by copying the file to ~/.vim/syntax/moin.vim on Unix-based systems, or to $HOME/vimfiles/syntax/moin.vim on Windows systems.

You may need to create the .vim (or vimfiles) directory, and you may need to create the syntax subdirectory. In Vim, your home directory is specified with ~ on Unix systems, and $HOME on Windows systems. You can see what directories to use by entering commands like the following in Vim:

:echo expand('~')
:echo expand('~/.vim/syntax/moin.vim')
:echo $HOME
:echo expand('$HOME/vimfiles/syntax/moin.vim')

Folding

This will allow you to fold wiki sections that start with a header like ==section== or ===subsection===, etc. It is done through expression folding, so you have to define the folding expression and set the proper fold method (expr).

Create file ~/.vim/ftplugin/moin.vim (Unix) or $HOME/vimfiles/ftplugin/moin.vim (Windows). The contents of the file should be:

" Settings for editing wiki files.
setlocal encoding=utf-8
setlocal expandtab
setlocal matchpairs+=<:>
setlocal nomodeline
setlocal modelines=0

" Define folding based on wiki headings; start with all folds open.
setlocal foldlevel=20
setlocal foldmethod=expr
setlocal foldexpr=HeadingLevel(v:lnum)
if !exists("*HeadingLevel")
  function HeadingLevel(lnum)
    " n = number of consecutive '=' at start of line
    let n = strlen(substitute(getline(a:lnum), '[^=].*', '', ''))
    return (n == 0) ? '=' : '>' . n
  endfunction
endif

Explanation

  • Telling Vim to use UTF-8 character encoding is essential to avoid corrupting Unicode characters.
  • The optional expandtab setting expands any tab characters that you insert to an equivalent number of spaces.
  • For security, it is highly desirable to switch modelines off to avoid applying settings or executing code that someone may have inserted in a modeline on the wiki.
  • The optional foldlevel=20 setting means that all folds will initially be open so you can see everything (you can close folds if wanted). If you prefer all folds to be closed initially, omit this line. :help folding

File type detection

It is necessary to tell Vim to associate MoinMoin files with the moin filetype so you don't need to manually enter :setlocal filetype=moin when editing a wiki file. The following will identify all files with extension .moin as MoinMoin files.

Create file ~/.vim/filetype.vim (Unix) or $HOME/vimfiles/filetype.vim (Windows) with the following contents. If the file already exists, you only need to copy in the au...setf moin line. :help new-filetype

if exists("did_load_filetypes")
  finish
endif
augroup filetypedetect
  au! BufNewFile,BufRead *.moin	setf moin
augroup END

Comments

Advertisement