(Marked as a duplicate) |
(→Plugin solution: move plugin title into template) |
||
(13 intermediate revisions by 7 users not shown) | |||
Line 1: | Line 1: | ||
− | {{Duplicate|724}} |
||
{{TipImported |
{{TipImported |
||
|id=750 |
|id=750 |
||
|previous=749 |
|previous=749 |
||
|next=751 |
|next=751 |
||
− | |created= |
+ | |created=2004 |
|complexity=intermediate |
|complexity=intermediate |
||
|author=Chris X Edwards |
|author=Chris X Edwards |
||
− | |version= |
+ | |version=6.0 |
|rating=26/18 |
|rating=26/18 |
||
|category1=Automated Text Insertion |
|category1=Automated Text Insertion |
||
|category2= |
|category2= |
||
}} |
}} |
||
+ | This tip shows how to underline text document headings with, for example, a dashed line. |
||
⚫ | |||
+ | ==Using a mapping== |
||
⚫ | |||
<pre> |
<pre> |
||
A Very Important Tip! |
A Very Important Tip! |
||
</pre> |
</pre> |
||
− | + | the mapping below inserts a row of dashes like this: |
|
− | |||
<pre> |
<pre> |
||
A Very Important Tip! |
A Very Important Tip! |
||
Line 25: | Line 25: | ||
</pre> |
</pre> |
||
− | This is |
+ | This is useful to highlight headings. Add the following to your [[vimrc]]: |
− | |||
− | 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 |
||
− | nnoremap |
+ | nnoremap <F5> yyp<c-v>$r- |
" Underline the current line with dashes in insert mode |
" Underline the current line with dashes in insert mode |
||
− | inoremap |
+ | inoremap <F5> <Esc>yyp<c-v>$r-A |
</pre> |
</pre> |
||
− | Of course you can use |
+ | Of course you can use other characters instead of <code>-</code>, for example, <code>=</code> or <code>_</code>. |
In case you want to get a line above ''and'' below the heading, do this in normal mode: |
In case you want to get a line above ''and'' below the heading, do this in normal mode: |
||
− | |||
<pre> |
<pre> |
||
− | + | <F5>yykP |
|
</pre> |
</pre> |
||
Line 51: | Line 47: | ||
</pre> |
</pre> |
||
− | It works by finding each matching line, then copying it ({{help|:t}}), then substituting each character (< |
+ | It works by finding each matching line, then copying it ({{help|:t}}), then substituting each character (<code>.</code>) in the line. |
+ | ==Using a function== |
||
⚫ | |||
+ | The following code (for your [[vimrc]]) defines a user command to underline the current line. Examples: |
||
+ | {| class="cleartable |
||
+ | | <code>:Underline</code> || gives underlining like <code>--------------</code> (default). |
||
+ | |- |
||
+ | | <code>:Underline =</code> || gives underlining like <code>==============</code>. |
||
+ | |- |
||
+ | | <code>:Underline -=</code> || gives underlining like <code>-=-=-=-=-=-=-=</code>. |
||
+ | |- |
||
+ | | <code>:Underline ~+-</code> || gives underlining like <code>~+-~+-~+-~+-~+-~+-</code>. |
||
+ | |} |
||
+ | <pre> |
||
− | ---- |
||
+ | function! s:Underline(chars) |
||
+ | let chars = empty(a:chars) ? '-' : a:chars |
||
+ | let nr_columns = virtcol('$') - 1 |
||
+ | let uline = repeat(chars, (nr_columns / len(chars)) + 1) |
||
+ | put =strpart(uline, 0, nr_columns) |
||
+ | endfunction |
||
+ | command! -nargs=? Underline call s:Underline(<q-args>) |
||
+ | </pre> |
||
+ | |||
+ | ==Related plugins== |
||
+ | As an alternative, you could use a plugin: |
||
+ | *{{script|id=4739|text=extline}} : Plugin for extending lines (e.g., underlined titles) |
||
+ | |||
+ | ==See also== |
||
+ | *[[Create underlines, overlines, and strikethroughs using combining characters]] |
||
+ | |||
⚫ | |||
+ | <code>yypVr-</code> also works well. Great tip for Markdown and WikiTex users. |
||
+ | [[User:Linktohack|Linktohack]] 15:37, February 3, 2012 (UTC) |
||
+ | :Thanks. There is a subtle feature about the method in the tip. If you have the default 'virtualedit' setting (""), and if there is a tab in the line, using <code>Vr-</code> replaces each tab with a single dash, while the method in the tip uses the correct number of dashes depending on the width of the tab. [[User:JohnBeckett|JohnBeckett]] 06:50, February 4, 2012 (UTC) |
Latest revision as of 13:07, 4 October 2013
created 2004 · complexity intermediate · author Chris X Edwards · version 6.0
This tip shows how to underline text document headings with, for example, a dashed line.
Using a mapping[]
Starting with a line of text like:
A Very Important Tip!
the mapping below inserts a row of dashes like this:
A Very Important Tip! ---------------------
This is useful to highlight headings. Add the following to your vimrc:
" 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 other characters instead of -
, for example, =
or _
.
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.
Using a function[]
The following code (for your vimrc) defines a user command to underline the current line. Examples:
:Underline |
gives underlining like -------------- (default).
|
:Underline = |
gives underlining like ============== .
|
:Underline -= |
gives underlining like -=-=-=-=-=-=-= .
|
:Underline ~+- |
gives underlining like ~+-~+-~+-~+-~+-~+- .
|
function! s:Underline(chars) let chars = empty(a:chars) ? '-' : a:chars let nr_columns = virtcol('$') - 1 let uline = repeat(chars, (nr_columns / len(chars)) + 1) put =strpart(uline, 0, nr_columns) endfunction command! -nargs=? Underline call s:Underline(<q-args>)
Related plugins[]
As an alternative, you could use a plugin:
- extline : Plugin for extending lines (e.g., underlined titles)
See also[]
Comments[]
yypVr-
also works well. Great tip for Markdown and WikiTex users.
Linktohack 15:37, February 3, 2012 (UTC)
- Thanks. There is a subtle feature about the method in the tip. If you have the default 'virtualedit' setting (""), and if there is a tab in the line, using
Vr-
replaces each tab with a single dash, while the method in the tip uses the correct number of dashes depending on the width of the tab. JohnBeckett 06:50, February 4, 2012 (UTC)