Wikia

Vim Tips Wiki

Changes: Vim Tips Wiki:Code guidelines

Edit

Back to page

(Update.)
(remove typo and add note about special characters in pre)
 
(3 intermediate revisions by 2 users not shown)
Line 5: Line 5:
   
 
A simple alternative is to insert a space before each line in the block, but that method can give the problems discussed below.
 
A simple alternative is to insert a space before each line in the block, but that method can give the problems discussed below.
  +
  +
If you want syntax coloring, you can use <code><nowiki><source lang="vim"></nowiki></code> ... <code><nowiki></source></nowiki></code> instead of <code><nowiki><pre></nowiki></code>.
  +
  +
We use both <code><nowiki><pre></nowiki></code> and <code><nowiki><source></nowiki></code> blocks, without any strong preferences either way. Use whichever suits your taste, but please remain consistent with the rest of a tip when editing an existing one.
   
 
For inline code (for example, <code>:s/old/new/</code>), type <code><nowiki><code>:s/old/new/</code></nowiki></code>. See [[Template:code]] for more information.
 
For inline code (for example, <code>:s/old/new/</code>), type <code><nowiki><code>:s/old/new/</code></nowiki></code>. See [[Template:code]] for more information.
   
 
==Examples and explanations==
 
==Examples and explanations==
When you use <code><nowiki><pre></nowiki></code>''Some Text''<code><nowiki></pre></nowiki></code>:
+
When you use <code><nowiki><pre></nowiki></code>''Some Text''<code><nowiki></pre></nowiki></code> or <code><nowiki><source></nowiki></code>:
 
*''Some Text'' is not parsed for wiki code (it's as if you had used <code>&lt;nowiki&gt;</code>).
 
*''Some Text'' is not parsed for wiki code (it's as if you had used <code>&lt;nowiki&gt;</code>).
 
*''Some Text'' is taken as preformatted (so whitespace, including line breaks, is preserved).
 
*''Some Text'' is taken as preformatted (so whitespace, including line breaks, is preserved).
 
*''Some Text'' is displayed in a <code>fixed-width</code> font.
 
*''Some Text'' is displayed in a <code>fixed-width</code> font.
*Special characters (like <code>&amp;lt;</code> for <code>&lt;</code>) are processed normally.
+
*Special characters (like <code>&amp;lt;</code> for <code>&lt;</code>) are processed normally (but are usually not required, e.g. <code>getline("'<")</code> can be typed literally).
   
 
Simply prefixing each line with a space gives the same result as using the <code><nowiki><pre></nowiki></code> tag, except that in a line prefixed with a space, normal wiki processing rules apply (for example, <code><nowiki>''</nowiki></code> gives italics).
 
Simply prefixing each line with a space gives the same result as using the <code><nowiki><pre></nowiki></code> tag, except that in a line prefixed with a space, normal wiki processing rules apply (for example, <code><nowiki>''</nowiki></code> gives italics).
Line 19: Line 23:
 
Here is some sample wikitext that might be in a tip:
 
Here is some sample wikitext that might be in a tip:
 
<pre><nowiki>
 
<pre><nowiki>
&lt;pre&gt;
+
&lt;pre>
 
function MyFunction()
 
function MyFunction()
 
let newline = ''
 
let newline = ''
if getline("'&gt;") != getline("'&lt;")
+
if getline("'>") != getline("'<")
 
let newline = "\n"
 
let newline = "\n"
 
endif
 
endif
 
" More stuff would go here.
 
" More stuff would go here.
 
endfunction
 
endfunction
&lt;/pre&gt;
+
&lt;/pre>
 
</nowiki></pre>
 
</nowiki></pre>
   
Line 34: Line 38:
 
function MyFunction()
 
function MyFunction()
 
let newline = ''
 
let newline = ''
if getline("'&gt;") != getline("'&lt;")
+
if getline("'>") != getline("'<")
 
let newline = "\n"
 
let newline = "\n"
 
endif
 
endif
Line 40: Line 44:
 
endfunction
 
endfunction
 
</pre>
 
</pre>
  +
  +
Now the same, with syntax highlighting:
  +
<pre><nowiki>
  +
<source lang="vim">
  +
function MyFunction()
  +
let newline = ''
  +
if getline("'>") != getline("'<")
  +
let newline = "\n"
  +
endif
  +
" More stuff would go here.
  +
endfunction
  +
</source>
  +
</nowiki></pre>
  +
  +
Displayed as:
  +
<source lang="vim">
  +
function MyFunction()
  +
let newline = ''
  +
if getline("'>") != getline("'<")
  +
let newline = "\n"
  +
endif
  +
" More stuff would go here.
  +
endfunction
  +
</source>
   
 
==Problems using ''space'' prefix==
 
==Problems using ''space'' prefix==

Latest revision as of 14:52, August 20, 2013

Vim Tip Guidelines
Quick reference
General guidelines
Titles and renaming a tip
Comments
Categories
Discussion page
Merging similar tips
Deleting a tip
Templates
Formatting code blocks
Entities and other magic
Script comments

In shortEdit

When you need to display a block of code in a tip, it is best to use <pre> ... </pre> tags around the block.

A simple alternative is to insert a space before each line in the block, but that method can give the problems discussed below.

If you want syntax coloring, you can use <source lang="vim"> ... </source> instead of <pre>.

We use both <pre> and <source> blocks, without any strong preferences either way. Use whichever suits your taste, but please remain consistent with the rest of a tip when editing an existing one.

For inline code (for example, :s/old/new/), type <code>:s/old/new/</code>. See Template:code for more information.

Examples and explanationsEdit

When you use <pre>Some Text</pre> or <source>:

  • Some Text is not parsed for wiki code (it's as if you had used <nowiki>).
  • Some Text is taken as preformatted (so whitespace, including line breaks, is preserved).
  • Some Text is displayed in a fixed-width font.
  • Special characters (like &lt; for <) are processed normally (but are usually not required, e.g. getline("'<") can be typed literally).

Simply prefixing each line with a space gives the same result as using the <pre> tag, except that in a line prefixed with a space, normal wiki processing rules apply (for example, '' gives italics).

Here is some sample wikitext that might be in a tip:

<pre>
function MyFunction()
  let newline = ''
  if getline("'>") != getline("'<")
    let newline = "\n"
  endif
  " More stuff would go here.
endfunction
</pre>

This is how the above wikitext is displayed:

function MyFunction()
  let newline = ''
  if getline("'>") != getline("'<")
    let newline = "\n"
  endif
  " More stuff would go here.
endfunction

Now the same, with syntax highlighting:

<source lang="vim">
function MyFunction()
  let newline = ''
  if getline("'>") != getline("'<")
    let newline = "\n"
  endif
  " More stuff would go here.
endfunction
</source>

Displayed as:

function MyFunction()
  let newline = ''
  if getline("'>") != getline("'<")
    let newline = "\n"
  endif
  " More stuff would go here.
endfunction

Problems using space prefixEdit

Here is some text, using <pre> before the text, and </pre> after:

The first line looks good.
Here are some {{double braces}} in the text.
myvar = ''     " this comment explains that '' is an empty string
:nnoremap <F3> :let @+='http://www.example.com/?item='.expand('<cword>')<CR>

Here is exactly the same text, using the insert space before each line technique:

The first line looks good.
Here are some Template:Double braces in the text.
myvar =      " this comment explains that  is an empty string
:nnoremap <F3> :let @+='http://www.example.com/?item='.expand('<cword>')<CR>

The insert space before each line technique has these disadvantages:

  • Punctuation may be mistaken for wiki syntax, so the display may be incorrect.
  • Under some circumstances, if you select text from the spaced code block, you will find that each line has a trailing space (which is irritating if you want to paste the code into an example, and which can break some scripts).
  • On long code blocks, it is easier to insert <pre> before the block and </pre> after.

Around Wikia's network

Random Wiki