Vim Tips Wiki
Register
(Use nbsp for 3 required trailing spaces so not accidentally removed)
(Change <tt> to <code>, perhaps also minor tweak.)
(4 intermediate revisions by 2 users not shown)
Line 4: Line 4:
 
|previous=965
 
|previous=965
 
|next=967
 
|next=967
|created=August 5, 2005
+
|created=2005
 
|complexity=basic
 
|complexity=basic
 
|author=Bernard Barton
 
|author=Bernard Barton
 
|version=5.7
 
|version=5.7
 
|rating=57/23
 
|rating=57/23
  +
|category1=Abbreviations
  +
|category2=
 
}}
 
}}
 
Vim supports multi-line abbreviations, but does not say how you can define them on multiple lines.
 
Vim supports multi-line abbreviations, but does not say how you can define them on multiple lines.
Line 14: Line 16:
 
Here is a way to define a long abbreviation over multiple lines.
 
Here is a way to define a long abbreviation over multiple lines.
   
You need a line-continuation character (\) and a carriage return (&lt;CR&gt;) at the ''beginning'' of each line. Here is an example:
+
You need a line-continuation character (\) and a carriage return (<CR>) at the ''beginning'' of each line. Here is an example:
   
 
<pre>
 
<pre>
 
iab abtest&nbsp;
 
iab abtest&nbsp;
\&lt;CR&gt;this is line one
+
\<CR>this is line one
\&lt;CR&gt;this is line two
+
\<CR>this is line two
\&lt;CR&gt;this is line three
+
\<CR>this is line three
 
</pre>
 
</pre>
   
Line 29: Line 31:
 
</pre>
 
</pre>
   
To simplify creating multi-line abbreviations, I've included two substitution commands which will add or remove the \&lt;CR&gt; to the beginning of each line. These are mapped in visual mode, and are bound to the Ctrl-C and Ctrl-Alt-C keys:
+
To simplify creating multi-line abbreviations, I've included two substitution commands which will add or remove the \<CR> to the beginning of each line. These are mapped in visual mode, and are bound to the Ctrl-C and Ctrl-Alt-C keys:
   
 
<pre>
 
<pre>
:vmap &lt;C-c&gt; :s/^/\\\&lt;\C\R\&gt;/&lt;CR&gt;:nohlsearch&lt;CR&gt;
+
:vmap <C-c> :s/^/\\\<\C\R\>/<CR>:nohlsearch<CR>
:vmap &lt;C-A-c&gt; :s/\\&lt;CR[&gt;]//&lt;CR&gt;:nohlsearch&lt;CR&gt;
+
:vmap <C-A-c> :s/\\<CR[>]//<CR>:nohlsearch<CR>
 
</pre>
 
</pre>
   
Line 39: Line 41:
   
 
<pre>
 
<pre>
#include &lt;stdio.h&gt;
+
#include <stdio.h>
 
void main(void) {
 
void main(void) {
 
printf("Hello World\n");
 
printf("Hello World\n");
Line 48: Line 50:
 
*Place the cursor on the first line and press Shift-V.
 
*Place the cursor on the first line and press Shift-V.
 
*Press the 'j' key until the entire code block is highlighted.
 
*Press the 'j' key until the entire code block is highlighted.
*Press Ctrl-C. This places the \&lt;CR&gt; in front of each line.
+
*Press Ctrl-C. This places the \<CR> in front of each line.
*Add "iab cmain " (no quotes) just above the abreviation, and ensure there's a space after cmain. It should look like this:
+
*Add "iab cmain " (no quotes) just above the abbreviation, and ensure there's a space after cmain. It should look like this:
   
 
<pre>
 
<pre>
 
iab cmain&nbsp;
 
iab cmain&nbsp;
\&lt;CR&gt;#include &lt;stdio.h&gt;
+
\<CR>#include <stdio.h>
\&lt;CR&gt;
+
\<CR>
\&lt;CR&gt;void main(void) {
+
\<CR>void main(void) {
\&lt;CR&gt;
+
\<CR>
\&lt;CR&gt; printf("Hello World\n");
+
\<CR> printf("Hello World\n");
\&lt;CR&gt;
+
\<CR>
\&lt;CR&gt;}
+
\<CR>}
 
</pre>
 
</pre>
   
Now simply source the file, and type <tt>cmain</tt> to expand the abbreviation.
+
Now simply source the file, and type <code>cmain</code> to expand the abbreviation.
   
 
Here is an example of an abbreviation for Perl programs that you may find useful:
 
Here is an example of an abbreviation for Perl programs that you may find useful:
Line 68: Line 70:
 
<pre>
 
<pre>
 
iab abperl&nbsp;
 
iab abperl&nbsp;
\&lt;CR&gt;###############################################################################
+
\<CR>###############################################################################
\&lt;CR&gt;#
+
\<CR>#
\&lt;CR&gt;# File:
+
\<CR># File:
\&lt;CR&gt;#
+
\<CR>#
\&lt;CR&gt;# Date:
+
\<CR># Date:
\&lt;CR&gt;#
+
\<CR>#
\&lt;CR&gt;# Description:
+
\<CR># Description:
\&lt;CR&gt;#
+
\<CR>#
\&lt;CR&gt;# Syntax:
+
\<CR># Syntax:
\&lt;CR&gt;#
+
\<CR>#
\&lt;CR&gt;# Author:
+
\<CR># Author:
\&lt;CR&gt;#
+
\<CR>#
\&lt;CR&gt;# Copyright (c)
+
\<CR># Copyright (c)
\&lt;CR&gt;#
+
\<CR>#
\&lt;CR&gt;#
+
\<CR>#
 
\###############################################################################
 
\###############################################################################
 
</pre>
 
</pre>
Line 90: Line 92:
   
 
<pre>
 
<pre>
map ,,, :r template&lt;CR&gt;
+
map ,,, :r template<CR>
 
</pre>
 
</pre>
   

Revision as of 05:59, 13 July 2012

Tip 966 Printable Monobook Previous Next

created 2005 · complexity basic · author Bernard Barton · version 5.7


Vim supports multi-line abbreviations, but does not say how you can define them on multiple lines.

Here is a way to define a long abbreviation over multiple lines.

You need a line-continuation character (\) and a carriage return (<CR>) at the beginning of each line. Here is an example:

iab abtest 
\<CR>this is line one
\<CR>this is line two
\<CR>this is line three

There must be a space following the abbreviation name ("abtest " in the first line above), in order to avoid this error when sourcing abbreviations defined like this:

E474: Invalid argument

To simplify creating multi-line abbreviations, I've included two substitution commands which will add or remove the \<CR> to the beginning of each line. These are mapped in visual mode, and are bound to the Ctrl-C and Ctrl-Alt-C keys:

:vmap <C-c> :s/^/\\\<\C\R\>/<CR>:nohlsearch<CR>
:vmap <C-A-c> :s/\\<CR[>]//<CR>:nohlsearch<CR>

To create a multi-line abbreviation using these mappings, follow these steps. This example creates a multi-line abbreviation for the proverbial "hello world" C program:

#include <stdio.h>
void main(void) {
  printf("Hello World\n");
}
  • Place the :vmap key mappings into a file and source it.
  • Place the cursor on the first line and press Shift-V.
  • Press the 'j' key until the entire code block is highlighted.
  • Press Ctrl-C. This places the \<CR> in front of each line.
  • Add "iab cmain " (no quotes) just above the abbreviation, and ensure there's a space after cmain. It should look like this:
iab cmain 
\<CR>#include <stdio.h>
\<CR>
\<CR>void main(void) {
\<CR>
\<CR> printf("Hello World\n");
\<CR>
\<CR>}

Now simply source the file, and type cmain to expand the abbreviation.

Here is an example of an abbreviation for Perl programs that you may find useful:

iab abperl 
\<CR>###############################################################################
\<CR>#
\<CR># File:
\<CR>#
\<CR># Date:
\<CR>#
\<CR># Description:
\<CR>#
\<CR># Syntax:
\<CR>#
\<CR># Author:
\<CR>#
\<CR># Copyright (c)
\<CR>#
\<CR>#
\###############################################################################

Comments

A better procedure is to keep a clean template in a file, and include that file when required:

map ,,, :r template<CR>