(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= |
+ | |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 ( |
+ | 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 |
iab abtest |
||
− | \ |
+ | \<CR>this is line one |
− | \ |
+ | \<CR>this is line two |
− | \ |
+ | \<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 \ |
+ | 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 |
+ | :vmap <C-c> :s/^/\\\<\C\R\>/<CR>:nohlsearch<CR> |
− | :vmap |
+ | :vmap <C-A-c> :s/\\<CR[>]//<CR>:nohlsearch<CR> |
</pre> |
</pre> |
||
Line 39: | Line 41: | ||
<pre> |
<pre> |
||
− | #include |
+ | #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 \ |
+ | *Press Ctrl-C. This places the \<CR> in front of each line. |
− | *Add "iab cmain " (no quotes) just above the |
+ | *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 |
iab cmain |
||
− | \ |
+ | \<CR>#include <stdio.h> |
− | \ |
+ | \<CR> |
− | \ |
+ | \<CR>void main(void) { |
− | \ |
+ | \<CR> |
− | \ |
+ | \<CR> printf("Hello World\n"); |
− | \ |
+ | \<CR> |
− | \ |
+ | \<CR>} |
</pre> |
</pre> |
||
− | Now simply source the file, and type < |
+ | 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 |
iab abperl |
||
− | \ |
+ | \<CR>############################################################################### |
− | \ |
+ | \<CR># |
− | \ |
+ | \<CR># File: |
− | \ |
+ | \<CR># |
− | \ |
+ | \<CR># Date: |
− | \ |
+ | \<CR># |
− | \ |
+ | \<CR># Description: |
− | \ |
+ | \<CR># |
− | \ |
+ | \<CR># Syntax: |
− | \ |
+ | \<CR># |
− | \ |
+ | \<CR># Author: |
− | \ |
+ | \<CR># |
− | \ |
+ | \<CR># Copyright (c) |
− | \ |
+ | \<CR># |
− | \ |
+ | \<CR># |
\############################################################################### |
\############################################################################### |
||
</pre> |
</pre> |
||
Line 90: | Line 92: | ||
<pre> |
<pre> |
||
− | map ,,, :r template |
+ | map ,,, :r template<CR> |
</pre> |
</pre> |
||
Revision as of 05:59, 13 July 2012
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>