使用vscode编写markdown文件已经比较简单快捷了,只需要安装如下三个插件基本就已经可以很顺滑的使用markdown进行书行了:

不过有的时候还会涉及在linux终端下编写markdown的情况,又不想在对应的主机上安装linux版本的vscode(可以通过window下的vscode远程连接)。这时候可以选择在linux vim下简单安装几个插件来实现对应的需求。

安装vim-plug

使用curl命令下载vim-plug源码

1curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

vim-plug 做为vim常用的插件管理器,可以通过该插件实现对其他插件的自动安装。

vimrc配置

在~/.vimrc配置文件增加如下内容:

  1call plug#begin('~/.vim/plugged')
  2Plug 'SirVer/ultisnips'
  3Plug 'honza/vim-snippets'
  4Plug 'vim-airline/vim-airline'
  5Plug 'majutsushi/tagbar'
  6Plug 'terroo/vim-auto-markdown'
  7call plug#end()"
  8
  9" 高亮行和列
 10set cursorcolumn
 11set cursorline
 12hi CursorLine   cterm=NONE ctermbg=blue ctermfg=white
 13hi Cursorcolumn   cterm=NONE ctermbg=gray ctermfg=white
 14
 15" 设置自动折行
 16set wrap
 17
 18"启用相对行
 19"set relativenumber
 20set nu
 21set ruler
 22
 23" 代码补全
 24set completeopt=preview,menu
 25
 26" 自动缩进
 27set autoindent
 28
 29
 30" Tab键的宽度
 31set tabstop=2
 32
 33" 统一缩进为4
 34set softtabstop=4
 35set shiftwidth=4
 36
 37autocmd FileType yaml setlocal ai ts=2 sw=2 et
 38
 39autocmd FileType python setlocal et sta sw=2 sts=2
 40"编码设置
 41set enc=utf-8
 42set fencs=utf-8,ucs-bom,shift-jis,gb18030,gbk,gb2312,cp936
 43
 44" 我的状态行显示的内容(包括文件类型和解码
 45set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [POS=%l,%v][%p%%]\ %{strftime(\"%d/%m/%y\ -\ %H:%M\")}
 46
 47" 去掉输入错误的提示声音
 48set noeb
 49
 50
 51"语言设置
 52set langmenu=zh_CN.UTF-8
 53set helplang=cn
 54
 55" 当新建 .py .sh .md等文件时自动调用SetComment 函数
 56autocmd BufNewFile *.py  exec ":call SetCommentPy()"
 57autocmd BufNewFile *.sh  exec ":call SetComment_sh()"
 58autocmd BufNewFile *.md  exec ":call SetComment_md()"
 59
 60func SetCommentPy()
 61    call setline(1,          "#!/usr/bin/env python")
 62    call append(line("."),   "# coding=utf8")
 63    call append(line(".")+1, "# ===============================================================================")
 64    call append(line(".")+2, "#   Copyright (C) ".strftime("%Y")." www.361way.com site All rights reserved.")
 65    call append(line(".")+3, "#   ")
 66    call append(line(".")+4, "#   Filename      :".expand("%:t"))
 67    call append(line(".")+5, "#   Author        :yangbk <itybku@139.com>")
 68    call append(line(".")+6, "#   Create Time   :".strftime("%Y-%m-%d %H:%M"))
 69    call append(line(".")+7, "#   Description   :")
 70    call append(line(".")+8, "# ===============================================================================")
 71endfunc
 72
 73func SetComment_sh()
 74        call setline(1, "#!/bin/bash")
 75        call setline(2, "# ================================================================")
 76        call setline(3, "#   Copyright (C) ".strftime("%Y")." www.361way.com site All rights reserved.")
 77        call setline(4, "#   ")
 78        call setline(5, "#   Filename      :".expand("%:t"))
 79        call setline(6, "#   Author        :yangbk <itybku@139.com>")
 80        call setline(7, "#   Create Time   :".strftime("%Y-%m-%d %H:%M"))
 81        call setline(8, "#   Description   :")
 82        call setline(9, "# ================================================================")
 83        call setline(10, "")
 84                                :10
 85endfunc
 86
 87func SetComment_md()
 88                set paste
 89"        call setline(1,'---')
 90"        call append(line("."), '"title": ,')
 91"        call append(line(".")+1, '"tags": ["tag1", "tag2"],')
 92"        call append(line(".")+2, '"description": " ",')
 93"        call append(line(".")+3, '"author": "yangbk"')
 94"        call append(line(".")+4, '---')
 95"        call append(line(".")+5, "")
 96endfunc
 97
 98command! JsonFormat :execute '%!python -m json.tool'
 99
100"不显示文档总字数
101let g:airline#extensions#wordcount#enabled = 0
102"不显示文件编码(Windows系统)
103let g:airline#parts#ffenc#skip_expected_string='utf-8[dos]'
104"设置tagber对于markdown的支持
105let g:tagbar_type_markdown = {
106    \ 'ctagstype' : 'markdown',
107    \ 'kinds' : [
108        \ 'h:Chapter',
109        \ 'i:Section',
110        \ 'k:Paragraph',
111        \ 'j:Subparagraph'
112    \ ]
113\ }
114"取消显示warning部分
115let g:airline_section_warning = ''
116"取消显示section_b
117let g:airline_section_b = ''
118"section_x显示文件名
119let g:airline_section_x = '%{expand("%")}'
120"激活tagbar扩展
121let g:airline#extensions#tagbar#enabled = 1

在Vim的底行模式下执行PlugInstall命令进行安装。也可以执行PlugInstall 命令进行安装单个插件。这里主要的插件是vim-auto-markdown,其可以实现 bash h2 等这种格式的自动补全。如果想要支持代码块折叠,还需要安装 https://github.com/preservim/vim-markdown 插件。

代码块头内容自动补全

在创建.md文件时,如果想要自动插件对应的代码头,类似如下:

 1---
 2id: 4890
 3title: RH254小结(十)rhel7配置VNC-Server
 4date: '2015-09-11T20:25:21+08:00'
 5author: admin
 6layout: post
 7guid: 'http://www.361way.com/?p=4890'
 8url: /rh254-centos7-vnc/4890.html
 9categories:
10    - Linux/unix/mac
11tags:
12    - centos7
13    - RHCE
14    - vnc
15---

这个可以使用 autocmd BufNewFile *.md exec ":call SetComment_md()" 这样的配置实现,不过在Hugo\jekyll\mkdocs等不同的静态站管理程序上使用的内容会略有不同,也可以通过写一个脚本生成,比如gohugo的生成,我使用的自定义指定脚本内容如下:

 1$ cat /usr/local/bin/hugomd
 2#/bin/bash
 3year=`date +%Y`
 4month=`date +%m`
 5time=`date +"%Y-%m-%dT%H:%M:%S"`
 6file=`date +%F`-$1.md
 7
 8echo """---
 9title: 'xx'
10date: '$time+08:00'
11author: admin
12layout: post
13url: /$year/$month/$1.html
14categories:
15    - cloud
16tags:
17    - tag1
18    - tag2
19---
20
21""" >> $file