Wikia

Vim Tips Wiki

Changes: User:Ipkiss/Using vim to edit tips

Edit

Back to page

(Replaced content with "<li>[http://www.chanluubracelets2012.com chan luu bracelets 2012]")
m (Reverted edits by 120.32.111.185 (talk | block) to last version by JohnBeckett)
 
Line 1: Line 1:
<li>[http://www.chanluubracelets2012.com chan luu bracelets 2012]
+
=Edit tips with Vim=
  +
  +
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 [http://en.wikipedia.org/wiki/Help:External_editors explanation]. See also [[User:Heptite|this page]] for a user script providing an external editor tab after the watch/unwatch one.
  +
  +
*If you use Firefox, you can use the [https://addons.mozilla.org/en-US/firefox/addon/394 ViewSourceWith] add-on or the [http://mozex.mozdev.org/ Mozex] one.
  +
  +
*[https://addons.mozilla.org/en-US/firefox/addon/4125 It's All Text ] for Firefox is also fairly stable.
  +
  +
*If you are on Windows, see also [[VimTip805]].
  +
  +
* For Linux users: there is a ''wikipedia-filesystem''. One can mount it, and edit and copy Wikipedia articles directly: [http://wikipediafs.sourceforge.net/ WikipediaFS]
  +
  +
  +
=Syntax highlighting=
  +
  +
Follow the instructions there: [http://en.wikipedia.org/wiki/Wikipedia:Text_editor_support#Vim 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 &lt;pre> and &lt;/pre> tags with vim syntax highlighting:
  +
<pre>
  +
" add Vim syntax highlighting to &lt;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="&lt;pre>" end="&lt;/pre>" contains=@VimL
  +
let b:current_syntax=s:current_syntax
  +
</pre>
  +
  +
=Meta-tips=
  +
  +
==Trailing spaces==
  +
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 abbreviations==
  +
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:
  +
  +
<pre>
  +
imap QH {{Help\|id=}}<left><left>
  +
imap QS {{Script\|id=}}<left><left>
  +
</pre>
  +
  +
==Restoring the original indent==
  +
  +
===Problem statement===
  +
On vim.org, the indentation of source code was respected by transforming spaces at the beginning of lines into as many &amp;nbsp; characters. During the migration process, consecutive &amp;nbsp; characters have been merged into one space character, thus breaking indentation completely. For example, if a user originally entered:
  +
<pre>
  +
void foo()
  +
{
  +
if (foo)
  +
bar;
  +
}
  +
</pre>
  +
after the wiki migration it is:
  +
<pre>
  +
void foo()
  +
{
  +
if (foo)
  +
bar;
  +
}
  +
</pre>
  +
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 &lt;pre> &lt;/pre> tags, restoring the original indent is painful.
  +
  +
===Solution===
  +
To help surrounding code with &lt;pre> &lt;/pre> tags, you can use the surround.vim plugin ({{script|id=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 [[User:Ipkiss/Restoring part of an original tip|Restoring part of an original tip]]. This works for almost any text in the tip, but has other limitations.
  +
  +
====Comments====
  +
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 &lt;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.
  +
  +
<pre>
  +
" 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>','','&lt;/pre>','bWnm') > 0
  +
return GetVimIndent()
  +
else
  +
return -1
  +
endif
  +
endfunc
  +
</pre>
  +
  +
--[[User:Fritzophrenic|Fritzophrenic]] 20:40, 14 December 2007 (UTC)
  +
  +
==Restoring the original line spacing==
  +
  +
===Problem statement===
  +
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 &lt;br> tags). The problem is that the migration script turned both &lt;br> and \r into newlines, which created an empty line after each original line. For example, if the user entered:
  +
<pre>
  +
Some
  +
text.
  +
  +
More text.
  +
</pre>
  +
it appears in the wiki as:
  +
<pre>
  +
Some
  +
  +
text.
  +
  +
  +
  +
More text.
  +
  +
</pre>
  +
  +
This causes additional work to the people who review migrated tips.
  +
  +
===Solution===
  +
A first solution is to use J or gJ where appropriate.
  +
  +
Another solution is to retrieve the linespacing from the original tip, see [[User:Ipkiss/Restoring part of an original tip|Restoring part of an original tip]].

Latest revision as of 10:05, September 24, 2012

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