Vim Tips Wiki
(Changed o<c-u> to A)
(Add Tim's substitute from vim_use)
Line 9: Line 9:
 
|rating=26/18
 
|rating=26/18
 
}}
 
}}
This tip takes a line of text like this:
+
This tip takes a line of text like:
   
 
<pre>
 
<pre>
A Very Important Tip!!!
+
A Very Important Tip!
 
</pre>
 
</pre>
   
Line 18: Line 18:
   
 
<pre>
 
<pre>
A Very Important Tip!!!
+
A Very Important Tip!
-----------------------
+
---------------------
 
</pre>
 
</pre>
   
 
This is particularly useful to highlight headings.
 
This is particularly useful to highlight headings.
   
Add this to your vimrc file:
+
Add this to your [[vimrc]] file:
   
 
<pre>
 
<pre>
 
" Underline the current line with dashes in normal mode
 
" Underline the current line with dashes in normal mode
nn &lt;F5&gt; yyp&lt;c-v>$r-
+
nnoremap &lt;F5&gt; yyp&lt;c-v>$r-
   
 
" Underline the current line with dashes in insert mode
 
" Underline the current line with dashes in insert mode
ino &lt;F5&gt; &lt;esc>yyp&lt;c-v>$r-A
+
inoremap &lt;F5&gt; &lt;esc>yyp&lt;c-v>$r-A
 
</pre>
 
</pre>
   
Note that you can use anything you want instead of - (= and _ spring to mind).
+
Of course you can use anything you want instead of <tt>-</tt> (<tt>=</tt> and <tt>_</tt> spring to mind).
   
In case you want to get a line above AND below the target text, do this in the normal mode:
+
In case you want to get a line above ''and'' below the heading, do this in normal mode:
   
 
<pre>
 
<pre>
 
&lt;F5&gt;yykP
 
&lt;F5&gt;yykP
 
</pre>
 
</pre>
  +
  +
==Using substitute==
  +
You can use the '''global''' and '''substitute''' commands ({{help|:g}}, {{help|:s}}) to underline all headings matching a pattern. For example, the following adds a row of dashes under each line that starts with "Chapter":
  +
<pre>
  +
:g/^Chapter/t.|s/./-/g
  +
</pre>
  +
  +
It works by finding each matching line, then copying it ({{help|:t}}), then substituting each character (<tt>.</tt>) in the line.
   
 
==Comments==
 
==Comments==

Revision as of 06:04, 15 April 2008

Tip 750 Printable Monobook Previous Next

created June 18, 2004 · complexity intermediate · author Chris X Edwards · version 5.7


This tip takes a line of text like:

A Very Important Tip!

and changes it to two lines like this:

A Very Important Tip!
---------------------

This is particularly useful to highlight headings.

Add this to your vimrc file:

" Underline the current line with dashes in normal mode
nnoremap <F5> yyp<c-v>$r-

" Underline the current line with dashes in insert mode
inoremap <F5> <esc>yyp<c-v>$r-A

Of course you can use anything you want instead of - (= and _ spring to mind).

In case you want to get a line above and below the heading, do this in normal mode:

<F5>yykP

Using substitute

You can use the global and substitute commands (:help :g, :help :s) to underline all headings matching a pattern. For example, the following adds a row of dashes under each line that starts with "Chapter":

:g/^Chapter/t.|s/./-/g

It works by finding each matching line, then copying it (:help :t), then substituting each character (.) in the line.

Comments