Wikia

Vim Tips Wiki

Changes: Text template with placeholders

Edit

Back to page

(Fixed and removed Review.)
(formatting and some rewording)
 
Line 3: Line 3:
 
|previous=320
 
|previous=320
 
|next=323
 
|next=323
|created=August 29, 2002
+
|created=2002
 
|complexity=basic
 
|complexity=basic
 
|author=lars_e_krueger
 
|author=lars_e_krueger
Line 14: Line 14:
   
 
==Code==
 
==Code==
 
 
In this tips code block below, all template mappings are insert-mode only, while the "skip to next placeholder" (mapped as ;;) is both insert and normal mode enabled.
 
In this tips code block below, all template mappings are insert-mode only, while the "skip to next placeholder" (mapped as ;;) is both insert and normal mode enabled.
   
 
The following is an example code template for a "for loop" statement in C++ with 5 custom text input locations. When added to the vimrc file, it allows the user to type ";fo" in insert mode and add a "for loop" block into their code quickly.
 
The following is an example code template for a "for loop" statement in C++ with 5 custom text input locations. When added to the vimrc file, it allows the user to type ";fo" in insert mode and add a "for loop" block into their code quickly.
+
<pre>
:imap <buffer> ;fo <C-O>mzfor( %%%; %%%; %%%)<CR>{ // %%%<CR>%%%<CR>}<CR><C-O>'z;;
+
:imap <buffer> ;fo <C-O>mzfor( %%%; %%%; %%%)<CR>{ // %%%<CR>%%%<CR>}<CR><C-O>'z;;
  +
</pre>
   
 
Add the following to your vimrc file to add the "skip to next placeholder" functionality which will move the cursor to the next %%% section:
 
Add the following to your vimrc file to add the "skip to next placeholder" functionality which will move the cursor to the next %%% section:
+
<pre>
:imap <buffer> ;; <C-O>/%%%<CR><C-O>c3l
+
:imap <buffer> ;; <C-O>/%%%<CR><C-O>c3l
:nmap <buffer> ;; /%%%<CR>c3l
+
:nmap <buffer> ;; /%%%<CR>c3l
  +
</pre>
   
 
==Usage==
 
==Usage==
 
 
Whenever a C++ for block is needed type ";fo" and the following code will be placed in the file. The "_" signifies where the cursor will be in insert mode.
 
Whenever a C++ for block is needed type ";fo" and the following code will be placed in the file. The "_" signifies where the cursor will be in insert mode.
 
 
<pre>
 
<pre>
 
for( _; %%% ; %%%)
 
for( _; %%% ; %%%)
Line 38: Line 35:
   
 
The user may now type in some statement where the first "%%%" was, like i=0.
 
The user may now type in some statement where the first "%%%" was, like i=0.
 
 
<pre>
 
<pre>
 
for( i=0_; %%% ; %%%)
 
for( i=0_; %%% ; %%%)
Line 47: Line 43:
   
 
The user would then type ;; to place the cursor at the next "%%%" location, removing the "%%%" and putting Vim into insert mode.
 
The user would then type ;; to place the cursor at the next "%%%" location, removing the "%%%" and putting Vim into insert mode.
 
 
<pre>
 
<pre>
 
for( i=0; _ ; %%%)
 
for( i=0; _ ; %%%)
Line 56: Line 51:
   
 
==See also==
 
==See also==
 
 
*{{script|id=222}} - Mu-Template, a script which has support for placeholders in templates.
 
*{{script|id=222}} - Mu-Template, a script which has support for placeholders in templates.
 
*{{script|id=336}} - A script which defines C/C++ abbreviations with placeholders.
 
*{{script|id=336}} - A script which defines C/C++ abbreviations with placeholders.

Latest revision as of 07:15, January 22, 2011

Tip 322 Printable Monobook Previous Next

created 2002 · complexity basic · author lars_e_krueger · version 6.0


A number of scripts allow for text and code templates, even some which allow the user to put in placeholders. However, a script is not always needed to allow the user to quickly add a common code block or text block with place holding inserts. This template for inserts a code block with placeholders that are triple percent signs (%%%) where the user wants to enter custom text. The ;; command is mapped to find the next %%% and allow the user to change them to the custom text.

CodeEdit

In this tips code block below, all template mappings are insert-mode only, while the "skip to next placeholder" (mapped as ;;) is both insert and normal mode enabled.

The following is an example code template for a "for loop" statement in C++ with 5 custom text input locations. When added to the vimrc file, it allows the user to type ";fo" in insert mode and add a "for loop" block into their code quickly.

:imap <buffer> ;fo <C-O>mzfor( %%%; %%%; %%%)<CR>{ // %%%<CR>%%%<CR>}<CR><C-O>'z;;

Add the following to your vimrc file to add the "skip to next placeholder" functionality which will move the cursor to the next %%% section:

:imap <buffer> ;; <C-O>/%%%<CR><C-O>c3l
:nmap <buffer> ;; /%%%<CR>c3l

UsageEdit

Whenever a C++ for block is needed type ";fo" and the following code will be placed in the file. The "_" signifies where the cursor will be in insert mode.

for( _; %%% ; %%%)
{ // %%%
 %%%
}

The user may now type in some statement where the first "%%%" was, like i=0.

for( i=0_; %%% ; %%%)
{ // %%%
 %%%
}

The user would then type ;; to place the cursor at the next "%%%" location, removing the "%%%" and putting Vim into insert mode.

for( i=0; _ ; %%%)
{ // %%%
 %%%
}

See alsoEdit

  • script#222 - Mu-Template, a script which has support for placeholders in templates.
  • script#336 - A script which defines C/C++ abbreviations with placeholders.

CommentsEdit

Around Wikia's network

Random Wiki