Vim Tips Wiki
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
FANDOM
Fan Central
BETA
Games
Anime
Movies
TV
Video
Wikis
Explore Wikis
Community Central
Start a Wiki
Don't have an account?
Register
Sign In
Sign In
Register
Vim Tips Wiki
1,649
pages
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
Editing
Multi-line abbreviations
Back to page
Edit
Edit source
View history
Talk (0)
Edit Page
Multi-line abbreviations
We recommend that you
log in
before editing. This will allow other users to leave you a message about your edit, and will let you track edits via your
Watchlist
.
Creating an account
is quick and free.
The edit appears to have already been undone.
Anti-spam check. Do
not
fill this in!
{{review}} {{TipImported |id=966 |previous=965 |next=967 |created=2005 |complexity=basic |author=Bernard Barton |version=5.7 |rating=57/23 |category1=Abbreviations |category2= }} 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: <pre> iab abtest \<CR>this is line one \<CR>this is line two \<CR>this is line three </pre> 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: <pre> E474: Invalid argument </pre> 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> :vmap <C-c> :s/^/\\\<\C\R\>/<CR>:nohlsearch<CR> :vmap <C-A-c> :s/\\<CR[>]//<CR>:nohlsearch<CR> </pre> 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: <pre> #include <stdio.h> int main(void) { printf("Hello World\n"); return 0; } </pre> *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: <pre> iab cmain \<CR>#include <stdio.h> \<CR> \<CR>int main(void) { \<CR> \<CR> printf("Hello World\n"); \<CR> \<CR> return 0; \<CR> \<CR>} </pre> 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: <pre> 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> ==Comments== A better procedure is to keep a clean template in a file, and include that file when required: <pre> map ,,, :r template<CR> </pre> ----You may need to add a blank space after the name of your abbreviation.
Summary:
Please note that all contributions to the Vim Tips Wiki are considered to be released under the CC-BY-SA
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Navigation
(
view source
)
Template:Review
(
view source
)
Template:TipImported
(
view source
)
Follow on IG
TikTok
Join Fan Lab