Wikia

Vim Tips Wiki

Ipkiss/Using vim to edit tips

Talk0
1,613pages on
this wiki

< User:Ipkiss

Revision as of 10:05, September 24, 2012 by JohnBeckett (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Edit tips with VimEdit

There are several methods to use (g)vim as editor for this wiki:

  • Copy-paste the contents of the edition field into vim, do the changes there, and copy-paste back into the field. Obvious, but really cumbersome.
  • In your wiki preferences, enable the "Use external editor by default" option. This requires configuring MIME types for your browser, see the explanation. See also this page for a user script providing an external editor tab after the watch/unwatch one.
  • For Linux users: there is a wikipedia-filesystem. One can mount it, and edit and copy Wikipedia articles directly: WikipediaFS


Syntax highlightingEdit

Follow the instructions there: http://en.wikipedia.org/wiki/Wikipedia:Text_editor_support#Vim.

Depending on the solution you chose to edit text in vim, the autodetection of the filetype described on the page may not work. If you chose to use the ViewSourceWith plugin, use the following commands:

filetype plugin on
au BufRead vim.wikia.* setf Wikipedia

Note that the Wiki syntax highlighting is not perfect (no highlighting for templates in particular), but it is better than nothing. The following can be placed in ~/.vim/after/syntax/Wikipedia.vim to highlight what is inside <pre> and </pre> tags with vim syntax highlighting:

" add Vim syntax highlighting to <pre> sections
let s:current_syntax=b:current_syntax
unlet b:current_syntax
syntax include @VimL syntax/vim.vim
syntax region vimSnip matchgroup=htmlTag start="<pre>" end="</pre>" contains=@VimL
let b:current_syntax=s:current_syntax

Meta-tipsEdit

Trailing spacesEdit

Most tips (if not all) on vim.org have a trailing space on each line, and these were kept during the migration to the wiki. To remove them:

:%s/\s\+$

You may want to add this line to the filetype plugin created above, to have it done automatically whenever you start editing a tip.

Useful abbreviationsEdit

Links to the help topics or to the external scripts are often needed. Wiki templates make them easier to use, but for even less typing, add the following mappings to the Wikipedia filetype plugin:

imap QH {{Help\|id=}}<left><left>
imap QS {{Script\|id=}}<left><left>

Restoring the original indentEdit

Problem statementEdit

On vim.org, the indentation of source code was respected by transforming spaces at the beginning of lines into as many &nbsp; characters. During the migration process, consecutive &nbsp; characters have been merged into one space character, thus breaking indentation completely. For example, if a user originally entered:

void foo()
{
    if (foo)
        bar;
}

after the wiki migration it is:

void foo()
{
 if (foo)
 bar;
}

which looks like this once interpreted by the wiki engine:

void foo() {

if foo()
bar;

}

This is obviously very bad. Even though it is quite easy to surround the code with the <pre> </pre> tags, restoring the original indent is painful.

SolutionEdit

To help surrounding code with <pre> </pre> tags, you can use the surround.vim plugin (script#1697).

To indent easily viml code snippets, create a Wikipedia.vim indent plugin copied from the original vim.vim indent file. Example On Unix:

cp /usr/share/vim/vim70/indent/vim.vim ~/.vim/indent/Wikipedia.vim

You will need the following in your .vimrc to activate indent plugins:

filetype indent on


To restore the original indent of a tip, see Restoring part of an original tip. This works for almost any text in the tip, but has other limitations.

CommentsEdit

I like this solution! But, I think it can be improved. As it is, it will indent anything in the buffer using Vim rules, when really only the stuff in <pre> sections should be indented.

Here's what I did, in indent/wikipedia.vim. I'm sure it could use some improvement, for example the use of GetVimIndent() is hard-coded rather than using vim.vim's indentexpr value.

" set up indent to the default (-1) unless within a <pre> block, when we
" should use vim indenting

if exists("b:did_indent")
  finish
endif

" load the vim indent rules
runtime! indent/vim.vim

let b:did_indent = 1

" override the vim indent expression (we'll call it ourselves)
setlocal indentexpr=GetWikiIndent()

" Only define the function once.
if exists("*GetWikiIndent")
  finish
endif

" wiki-indent will return vim indent inside a <pre> block,
" and return -1 if not inside a block to trigger auto-indent
function GetWikiIndent()
  if searchpair('<pre>','','</pre>','bWnm') > 0
    return GetVimIndent()
  else
    return -1
  endif
endfunc

--Fritzophrenic 20:40, 14 December 2007 (UTC)

Restoring the original line spacingEdit

Problem statementEdit

Many tips on vim.org embed \r (carriage return) characters (I suspect this corresponds to tips edited on windows, where the end of line is \r\n, and the php code converted only \n into <br> tags). The problem is that the migration script turned both <br> and \r into newlines, which created an empty line after each original line. For example, if the user entered:

Some
text.

More text.

it appears in the wiki as:

Some

text.



More text.

This causes additional work to the people who review migrated tips.

SolutionEdit

A first solution is to use J or gJ where appropriate.

Another solution is to retrieve the linespacing from the original tip, see Restoring part of an original tip.

Around Wikia's network

Random Wiki