Introduction

vim 作为Linux/Mac系统中的特色编辑器,可以只通过键盘操作来实现各种功能。但由于通过命令行操作,即时预览Markdown文件无法在vim中原生实现。
本文中将介绍一种Markdown Preview即时预览插件的安装和使用。

通过两个插件来实现Markdown的预览

vim-plug

vim-plug是一款用于管理vim插件的插件。
github项目地址:https://github.com/junegunn/vim-plug
使用命令一键安装:

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

安装后,修改.vimrc配置文件。

.vimrc是关于vim编辑器外观、功能等特性的配置文件,可以通过修改其参数改变编辑时的背景颜色、是否启用鼠标等功能。

.vimrc文件一般在家目录下,可以在命令行输入cdcd ~ls -a展示文件

cd # 进入家目录
# 或 cd ~
ls -a # 展示当前目录所有文件,包括隐藏文件、本级和上级目录

除了常规的文件Document Desktop 等,还有一系列命名以.开头的隐藏文件,其中就有.vimrc配置文件

vim .vimrc # 打开配置文件

i进入编辑模式,在配置文件中添加以下内容:推荐写在文件末尾

call plug#begin([PLUGIN_DIR]) # 括号内可以写插件文件夹位置,也可以留空
call plug#end()

两个语句之间的部分用于添加插件,例如:

/* 摘自github */
call plug#begin()
" The default plugin directory will be as follows:
"   - Vim (Linux/macOS): '~/.vim/plugged'
"   - Vim (Windows): '~/vimfiles/plugged'
"   - Neovim (Linux/macOS/Windows): stdpath('data') . '/plugged'
" You can specify a custom plugin directory by passing it as the argument
"   - e.g. `call plug#begin('~/.vim/plugged')`
"   - Avoid using standard Vim directory names like 'plugin'

" Make sure you use single quotes

" Shorthand notation; fetches https://github.com/junegunn/vim-easy-align
Plug 'junegunn/vim-easy-align'

" Any valid git URL is allowed
Plug 'https://github.com/junegunn/vim-github-dashboard.git'

" Multiple Plug commands can be written in a single line using | separators
Plug 'SirVer/ultisnips' | Plug 'honza/vim-snippets'

" On-demand loading
Plug 'scrooloose/nerdtree', { 'on':  'NERDTreeToggle' }
Plug 'tpope/vim-fireplace', { 'for': 'clojure' }

" Using a non-default branch
Plug 'rdnetto/YCM-Generator', { 'branch': 'stable' }

" Using a tagged release; wildcard allowed (requires git 1.9.2 or above)
Plug 'fatih/vim-go', { 'tag': '*' }

" Plugin options
Plug 'nsf/gocode', { 'tag': 'v.20150303', 'rtp': 'vim' }

" Plugin outside ~/.vim/plugged with post-update hook
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }

" Unmanaged plugin (manually installed and updated)
Plug '~/my-prototype-plugin'

" Initialize plugin system
" - Automatically executes `filetype plugin indent on` and `syntax enable`.
call plug#end()
" You can revert the settings after the call like so:
"   filetype indent off   " Disable file-type-specific indentation
"   syntax off            " Disable syntax highlighting

以上代码仅用于展示结构,并非用于修改配置文件
ESC退出编辑模式,输入:wq,回车,以保存并退出
至此,完成了vim-plug的初始设置

markdown-preview.nvim

vim 版本需要≥8.1 才可以正常使用此插件

markdown-preview.nvim是用于实现预览markdown文件的插件,通过上面安装的vim-plug插件管理软件进行安装
github项目地址:https://github.com/iamcco/markdown-preview.nvim

打开.vimrc: vim .vimrc
进入编辑模式:i
在添加插件部分插入代码
根据本地条件,两条代码二选一:

" If you don't have nodejs and yarn
" use pre build, add 'vim-plug' to the filetype list so vim-plug can update this plugin
" see: https://github.com/iamcco/markdown-preview.nvim/issues/50
Plug 'iamcco/markdown-preview.nvim', { 'do': { -> mkdp#util#install() }, 'for': ['markdown', 'vim-plug']}


" If you have nodejs and yarn
Plug 'iamcco/markdown-preview.nvim', { 'do': 'cd app && yarn install' }
如果你不知道本地符合上面哪一条(是否有nodejs和yarn),可以先随意选择一条加入.vimrc,遇到报错再解决即可

添加后,按esc退出编辑模式,输入:w保存配置文件,输入:source ~/.vimrc,回车
输入:PlugInstall
输入:call mkdp#util#install()
至此,完成了markdown-preview.nvim插件的安装

使用

通过vim打开.md文件后,以命令行模式输入:MarkdownPreview(区分大小写,可用tab补全),回车,即可在浏览器中预览。

最后修改:2023 年 04 月 05 日
如果觉得我的文章对你有用,请随意赞赏