Skip to content

Instantly share code, notes, and snippets.

@mojavelinux
Created December 5, 2012 08:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mojavelinux/4213909 to your computer and use it in GitHub Desktop.
Save mojavelinux/4213909 to your computer and use it in GitHub Desktop.
AsciiDoc insert-mode mapping helpers for VIM
let mapleader=";"
inoremap <Leader><Leader> <Leader>
if has("autocmd")
au BufRead,BufNewFile *.asciidoc
\ imap <buffer> <C-]> <C-o>B<C-o>E<Right><Space>|
\ imap <buffer> <C-i> <C-o>$<CR><CR>|
\ imap <buffer> <C-j> <Down><C-o>o<CR>|
\ imap <buffer> <Leader>h1 = |
\ imap <buffer> <Leader>h2 == |
\ imap <buffer> <Leader>h3 === |
\ imap <buffer> <Leader>h4 ==== |
\ imap <buffer> <Leader>h5 ===== |
\ imap <buffer> <Leader>qb ____<CR>____<Esc>O|
\ imap <buffer> <Leader>eb ====<CR>====<Esc>O|
\ imap <buffer> <Leader>cb ----<CR>----<Esc>O|
\ imap <buffer> <Leader>lb ....<CR>....<Esc>O|
\ imap <buffer> <Leader>pb ++++<CR>++++<Esc>O|
\ imap <buffer> <Leader>sb ****<CR>****<Esc>O|
\ imap <buffer> <Leader>/b ////<C-o>o<C-o>0<C-o>d$////<C-o>O<C-o>0<C-o>d$|
\ nmap <buffer> <Leader>qb 0O____<C-o>0<Down><C-o>o____<Up><Esc>$|
\ nmap <buffer> <Leader>eb 0O====<C-o>0<Down><C-o>o====<Up><Esc>$|
\ nmap <buffer> <Leader>cb 0O----<C-o>0<Down><C-o>o----<Up><Esc>$|
\ nmap <buffer> <Leader>lb 0O....<C-o>0<Down><C-o>o....<Up><Esc>$|
\ nmap <buffer> <Leader>pb 0O++++<C-o>0<Down><C-o>o++++<Up><Esc>$|
\ nmap <buffer> <Leader>sb 0O****<C-o>0<Down><C-o>o****<Up><Esc>$|
\ nmap <buffer> <Leader>/b 0O////<C-o>0<Down><C-o>o////<Up><Esc>$|
\ nmap <buffer> <Leader>// gI// <Esc>$|
\ imap <buffer> <Leader>r '''<CR><CR>|
\ imap <buffer> <Leader>n. NOTE: |
\ imap <buffer> <Leader>nb <Leader>[NOTE<C-o>o<Leader>eb|
\ imap <buffer> <Leader>t. TIP: |
\ imap <buffer> <Leader>tb <Leader>[TIP<C-o>o<Leader>eb|
\ imap <buffer> <Leader>!. IMPORTANT: |
\ imap <buffer> <Leader>!b <Leader>[IMPORTANT<C-o>o<Leader>eb|
\ imap <buffer> <Leader>w. WARNING: |
\ imap <buffer> <Leader>wb <Leader>[WARNING<C-o>o<Leader>eb|
\ imap <buffer> <Leader>$. CAUTION: |
\ imap <buffer> <Leader>$b <Leader>[CAUTION<C-o>o<Leader>eb|
\ imap <buffer> <Leader>i. image:<Leader>[<Left>|
\ imap <buffer> <Leader>ib image::<Leader>[<Left>|
\ imap <buffer> <Leader>l. link:<Leader>[<Left>|
\ imap <buffer> <Leader>* **<Left>|
\ imap <buffer> <Leader>' ''<Left>|
\ imap <buffer> <Leader>" ""<Left>|
\ imap <buffer> <Leader>` ``<Left>|
\ imap <buffer> <Leader>+ ++++++<Left><Left><Left>|
\ imap <buffer> <Leader>< <><Left>|
\ imap <buffer> <Leader>: ::<Left>|
\ imap <buffer> <Leader>[ []<Left>|
\ imap <buffer> <Leader>( (())<Left><Left>|
\ nmap <buffer> <Leader>q* hEa<Space><Esc><Left>ciW*<C-o>P*<Esc>lx|
\ nmap <buffer> <Leader>q' hEa<Space><Esc>BviW:s/\%V'\%V/\\'/ge<CR>:nohls<CR>gv<C-c>viWc'<C-o>P'<Esc>lx|
\ nmap <buffer> <Leader>q" hEa<Space><Esc><Left>ciW"<C-o>P"<Esc>lx|
\ nmap <buffer> <Leader>q` hEa<Space><Esc><Left>ciW`<C-o>P`<Esc>lx|
\ nmap <buffer> <Leader>q+ hEa<Space><Esc><Left>ciW+++<C-o>P+++<Esc>lx
endif

AsciiDoc shorthand in VIM

The .vimrc file in this gist contains insert-mode mappings that will save you even more typing when writing in AsciiDoc (as if switching from DocBook wasn’t already an improvement :)).

The mappings leverage the "leader" variable, a special string that is used to indicate that a command is incoming. This configuration uses a semi-colon, ;, as the leader. That means in insert mode you will type a semi-colon immediately followed by one of the defined keystroke combinations (e.g., ;qb). After a brief timeout, the semi-colon will be inserted if you didn’t type any of the mapped keystrokes.

To use these mappings, copy the lines from the .vimrc file into your .vimrc file (or simply move this one into place). The mappings are configured to apply locally to a buffer holding an asciidoc file (existing or new).

Here’s how you use the various commands, most of which apply to insert mode. In each result, the pipe indicates the location of the cursor.

Headings

To insert a heading in insert mode, type:

;h2

Result:

== |

Blocks

To insert a quote block in insert mode, type:

;qb

Result:

____
|
____

Now just type between the lines. When you are done, you can jump below the bottom of the block by typing <C-j> (from insert mode).

You can also setup a quote block after the fact from normal mode. While on the line you want to quote, type ;qb as you did in insert mode.

The other blocks follow the same pattern. For instance, to insert a listing block in insert mode, type:

;cb

Result:

----
|
----

Admonitions

You can insert a single-line admonition in insert mode by typing:

;n.

Result:

NOTE: |

You setup a multi-line admonition in insert mode just like you did a block:

;nb

Result:

[NOTE]
====
|
====

This works for all admonition types, where the first character after the leader is selected as follows: (NOTE ⇒ n, TIP ⇒ t, IMPORTANT ⇒ !, WARNING ⇒ w, CAUTION ⇒ $).

Text formatting

Support for text formatting is also provided using the leader prefix. The mapping sets up the quotes and positions the cursor between them. Here’s how you would prepare to make bold text in insert mode:

;*

Result:

*|*

Once you are done typing the bold text, you can jump outside of the quotes by typing <C-]>.

This works for *, ', ", `, + and :.

There are also shortcuts for setting up brackets in insert mode, such as square brackets:

;[

Result:

[|]

This works for [, < and (.

You can format after the fact. In normal mode, place the cursor over the word and type ;q* for strong, ;q' for emphasis and so on. (Note that nasty looking mapping for adding emphasis. That’s escaping single quotes already present in the string…​it was quite a chore to get it working).

Miscellaneous

You can insert a horizontal rule in insert mode by typing:

;r

Result:

'''

You can setup an image in insert mode by typing:

;i.

Result:

image:|[]

You can setup a link (w/ the link prefix) in insert mode by typing:

;l.

Result:

link:|[]

When you get to the end of typing the image file name, type <C-o>a to advance the cursor between the square brackets to type the alt text.

The double carriage returns (hitting enter twice) is mapped to <C-i> in insert mode. This makes it easy to advance to a new paragraph in a single keybinding.

Typing first paragraph.<C-i>

Typing second paragraph.

Happy documenting!

@LightGuard
Copy link

Should put this in a plugin, perhaps along with some other stuff. Or a listing of other plugins that help with editing asciidoc files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment