In Vim, configure a carriage return expansion example for Markdown

Source: Internet
Author: User

It's been a long time since I wrote Jekyll blog on Vim, and it's been a hassle to write code blocks (Fence) and then "type", and manually write header information (Front matter). Harttle today uses the VIM keyboard mapping to provide carriage return support for these two syntaxes. The final effect is as follows:

Type code Fence ' and the Highlight language (| indicates where the cursor is located):

"' Javascript| '"
Type return at this time:

"JavaScript
|
```
Delimiter match

There are two kinds of code syntax in markdown: the inline code ' foo ' and the ' block of code Fence ' separated by ' '. We want vim to be automatically completed after the input ' and the cursor is still between the two. To do this you need to first install the Delimitmate plug-in:

Plugin ' Raimondi/delimitmate '
How do I install the vim plugin? Please refer to the creation of the VIM environment in front of the article.
Then configure a special ' delimiter for the markdown file in ~/.vim/after/ftplugin/markdown.vim, which does not exist, to create the path and file:

Let b:delimitmate_quotes = "" "" "
Let b:delimitmate_nesting_quotes = [']
Set ' Allow nesting is to be entered three ' when Vim can still match correctly.
Now open a *.md file and type ', and then automatically complete the following ':

```|```
Knock enter at this time:

```
|```
And what we want to do is:

```
|
```
This is the carriage return expansion that we discussed in the next section.

Carriage return expansion

The change in this section is the ~/.vim/after/ftplugin/markdown.vim file, which is a unique configuration of the markdown type file and does not affect other types of files. First create a keyboard mapping: Map carriage return to the expander () function call.

Vim uses the file suffix to identify the file type, just open the feature in ~/.VIMRC:: FileType on. If you want to customize the file type, you can add the file type identification rule in ~/.vim/after/filetype.vim. See also: Http://vim.wikia.com/wiki/Filetype.vim
Inoremap <expr> <CR> Expander ()
Where Inoremap is mapped under Insert mode (i) and is not a recursive (nore) mapping, that is, the result of the mapping is not mapped again;<cr> as a carriage return (carriage returns). Now it's up to the programmer's strengths: Write the expander function.

function! Expander ()
Let line = Getline (".")
Let col = col (".")-1

If Line[0:2] ==# "'" "&& line[col:col+2] ==#" "
Return "\<cr>\<esc>o"
endif

Return "\<cr>"
Endfunction
This function gets the row content of the current cursor, and the position of the cursor (the cursor is moved one after hitting the carriage return). Return "Enter, exit insert mode, insert new line up" when three "and start at the beginning of the cursor", otherwise return to normal "enter".

Note <expr> The function return value of the keyboard mapping, the key needs to be escaped. such as \<cr>.
Header information Expanded

When writing a Jekyll article, you first write the header information (Front matter):

---
Title:xxx
Tags:xxx
---
I would like to enter the---after the return will automatically fill the above content, you can continue to modify the above expander function:

function! Expander ()
Let line = Getline (".")
Let col = col (".")-1

If Line[0:2] ==# "'" "&& line[col:col+2] ==#" "
Return "\<cr>\<esc>o"
endif

If Line[0:2] ==# "---"
Return "\<cr>layout:blog\<cr>---\<esc>otitle:"
endif

Return "\<cr>"
Endfunction
Save the file and reopen a *.md file, and enter the following (| For the cursor position):

---|
Press ENTER, you can fill the full front matter:

---
Layout:blog
Title: |
---

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.