Notes for vim

Some notes for vim, actually neovim.

markdown

There are 3 packages for markdown syntax in vim:

Finally, I chose vim-pandoc. It has many useful key mapping for editing markdown, which is missed in vim-markdown. As to vim-polyglot, although it use vim-markdown to provide markdown support, it can’t highlight fenced codeblocks, because of lacking ftplugin/markdown.vim.

Installation

1
2
3
4
5
6
call plug#begin('~/.local/share/nvim/plugged')

Plug 'vim-pandoc/vim-pandoc'
Plug 'vim-pandoc/vim-pandoc-syntax'

call plug#end()

Setting

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
let g:pandoc#folding#fdc = 0
let g:pandoc#folding#fold_div_classes = 1
let g:pandoc#folding#fold_yaml = 1
let g:pandoc#folding#fold_fenced_codeblocks = 1
let g:pandoc#toc#close_after_navigating = 0
let g:pandoc#syntax#codeblocks#embeds#langs = [
\ "python",
\ "bash=sh",
\ "shell=sh",
\ "sh",
\ "vim",
\ "toml",
\ "help"
\]

" set F4 to open TOC
nmap <F4> :TOC<CR>

Usage

  • :TOC to open TOC, When the user presses <CR> (the Enter key) in the TOC buffer, the cursor is moved in the source window to the position selected.
  • g:pandoc#toc#position sets the position of TOC (default right).
  • g:pandoc#toc#close_after_navigating controls the whether close the TOC buffer after navigating.

Available key mapping

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
- *<localleader>i*     toggles emphasis [vn]
- *<localleader>b* toggles bold [vn]
- *<localleader>`* toggles verbatim [vn]
- *<localleader>~~* toggles strikeout [vn]
- *<localleader>^* toggles superscript [vn]
- *<localleader>_* toggles subscript [vn]
- *<localleader>#* apply header (accepts a count) [n]
- *<localleader>hd* remove header [n]
- *<localleader>hn* move to next header [n]
- *<localleader>hb* move to previous header [n]
- *<localleader>hh* go to current header [n]
- *<localleader>hp* go to current header's parent [n]
- *<localleader>hsn* move to next sibling header [n]
- *<localleader>hsb* move to previous sibling header [n]
- *<localleader>hcf* move fo first child header [n]
- *<localleader>hcl* move to last child header [n]
- *<localleader>hcn* move to [count] nth child header [n]
- ]] [count] sections forward (like <localleader>hn) [n]
- [[ [count] sections backwards [n]
- ][ [count] move to the next section end [n]
- [] [count] move to the previous section end [n]
- aS select a section, including header [vo]
- iS select a section, excluding header [vo]
- *<localleader>nr* insert a ref definition after this paragraph [n]
- *<localleader>rg* go to reference definition [n]
- *<localleader>rb* go back to reference label [n]
- *<localleader>gl* go to current link in current window
- *<localleader>sl* go to current link in split window
- *<localleader>gb* go back from link
- *<localleader>gB* go back from link to the previous file
- *<localleader>ln* move to next list item [n]
- *<localleader>lp* move to previous list item [n]
- *<localleader>ll* move to the current list item [n]
- *<localleader>llp* move to parent of the current list item [n]
- *<localleader>lsn* move to the next list item sibling [n]
- *<localleader>lsp* move to the previous list item sibling [n]
- *<localleader>lcf* move to the first list item child [n]
- *<localleader>lcl* move to the last list item child [n]
- *<localleader>lcn* move to the [count] nth list item child [n]

Preview

1
2
3
4
5
6
7
Plug 'iamcco/mathjax-support-for-mkdp'
Plug 'iamcco/markdown-preview.vim'

nmap <silent> <F11> <Plug>MarkdownPreview " 普通模式
imap <silent> <F11> <Plug>MarkdownPreview " 插入模式
nmap <silent> <F12> <Plug>StopMarkdownPreview " 普通模式
imap <silent> <F12> <Plug>StopMarkdownPreview " 插入模式

vim-template

Use vim-template to open a template when creating a new file.

Installation

1
Plug 'aperezdc/vim-template'

Setting

1
2
3
4
5
" vim-template
let g:templates_directory = ['~/.local/share/nvim/templates']
let g:templates_search_height = 1
let g:email = 'qifei9@gmail.com'
let g:username = 'qifei9'

Customized templates

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © %YEAR% %USER% <%MAIL%>
"""
File: %FFILE%
Description: %HERE%
"""

import argparse


def main():
"""main"""

parse = argparse.ArgumentParser()

parse.add_argument('', help='')

args = parse.parse_args()


if __name__ == "__main__":
main()

Complete and snippets using ncm2 and ultisnips

Installation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
" ncm2 requires nvim-yarp
Plug 'roxma/nvim-yarp'
Plug 'ncm2/ncm2'
Plug 'ncm2/ncm2-ultisnips'
Plug 'ncm2/ncm2-jedi'
Plug 'ncm2/ncm2-bufword'
Plug 'ncm2/ncm2-path'
Plug 'ncm2/ncm2-tmux'
Plug 'filipekiss/ncm2-look.vim'
Plug 'ncm2/ncm2-syntax'
Plug 'Shougo/neco-syntax'
Plug 'ncm2/ncm2-neoinclude'
Plug 'Shougo/neoinclude.vim'
Plug 'ncm2/ncm2-markdown-subscope'
Plug 'ncm2/ncm2-github'
" snippets
Plug 'SirVer/ultisnips'
Plug 'honza/vim-snippets'
" CompleteParameter, it must be loaded after ultisnips
Plug 'tenfyzhong/CompleteParameter.vim'
" echodoc print doc of functions in cmdline after completion
Plug 'Shougo/echodoc.vim'

Configure and key mapping

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
" use Tab to cycle in the popmenu, enter to  complete or expand
inoremap <expr> <Tab> pumvisible() ? "\<C-n>" : "\<Tab>"
inoremap <expr> <S-Tab> pumvisible() ? "\<C-p>" : "\<S-Tab>"
imap <silent> <expr> <CR>
\ pumvisible() ?
\ (!empty(v:completed_item) ?
\ ncm2_ultisnips#expand_or("", 'n') :
\ "\<CR>\<c-r>=AutoPairsReturn()\<cr>") :
\ "\<CR>\<c-r>=AutoPairsReturn()\<cr>"
" alt-q to expand, alt-w to move forward, alt-e to move backward
imap <expr> <A-q> ncm2_ultisnips#expand_or("\<Plug>(ultisnips_expand)", 'm')
smap <A-q> <Plug>(ultisnips_expand)

" ncm2
autocmd BufEnter * call ncm2#enable_for_buffer()
au User Ncm2PopupOpen set completeopt=noinsert,menuone,noselect
au User Ncm2PopupClose set completeopt=menuone
set shortmess+=c
let g:ncm2#complete_length = 2
" for ncm2-look
"let g:python_highlight_all = 1
" ultisnips
let g:snips_author = "qifei9"
let g:snips_email = "qifei9@gmail.com"
let g:snips_github = "https://github.com/qifei9"
let g:UltiSnipsExpandTrigger = "<Plug>(ultisnips_expand)"
let g:UltiSnipsJumpForwardTrigger = "<a-w>"
let g:UltiSnipsJumpBackwardTrigger = "<a-e>"
let g:UltiSnipsRemoveSelectModeMappings = 0
" CompleteParameter
let g:complete_parameter_use_ultisnips_mapping = 1
" echodoc
set noshowmode
let g:echodoc_enable_at_startup = 1

Customized snippets

1
2
3
4
5
6
7
8
9
10
snippet ifmain "ifmain" b
def main():
"""main"""

${1:${VISUAL:pass}}


if __name__ == `!p snip.rv = get_quoting_style(snip)`__main__`!p snip.rv = get_quoting_style(snip)`:
main()
endsnippet

Complete and snippets using deoplete and neosnippet (Deprecated)

Deprecated because deoplete/Neosnippet doesn’t work well for completion of functions. For pri, it gets print rather than print(). Seems that neopairs.vim are needed to auto insert pairs when complete done.

Not using neosnippet-snippets as snippets source, because it’s not frequently updated.

Installation

1
2
3
4
5
6
7
Plug 'Shougo/deoplete.nvim', { 'do': ':UpdateRemotePlugins' }
Plug 'Shougo/neosnippet.vim'
Plug 'honza/vim-snippets'
" CompleteParameter, it must be loaded after ultisnips
Plug 'tenfyzhong/CompleteParameter.vim'
" echodoc print doc of functions in cmdline after completion
Plug 'Shougo/echodoc.vim'

Configure

1
2
3
4
5
6
7
8
9
10
11
12
" deoplete
let g:deoplete#enable_at_startup = 1
call deoplete#custom#option('smart_case', v:true)
" Enable snipMate compatibility feature.
let g:neosnippet#enable_snipmate_compatibility = 1
" Tell Neosnippet about the other snippets
let g:neosnippet#snippets_directory='~/.local/share/nvim/plugged/vim-snippets/snippets'
" CompleteParameter
let g:complete_parameter_use_ultisnips_mapping = 0
" echodoc
set noshowmode
let g:echodoc_enable_at_startup = 1

Key mapping

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
inoremap <expr> <Tab> pumvisible() ? "\<C-n>" : "\<Tab>"
inoremap <expr> <S-Tab> pumvisible() ? "\<C-p>" : "\<S-Tab>"

smap <expr> <TAB>
\ neosnippet#expandable_or_jumpable() ?
\ "\<Plug>(neosnippet_expand_or_jump)" :
\ (complete_parameter#jumpable(1) ?
\ "\<plug>(complete_parameter#goto_next_parameter)" :
\ "\<TAB>")

imap <expr> <CR>
\ pumvisible() ?
\ (neosnippet#expandable() ?
\ "\<Plug>(neosnippet_expand)\<c-r>=AutoPairsReturn()\<cr>" :
\ deoplete#close_popup()) :
\ "\<CR>\<c-r>=AutoPairsReturn()\<cr>"

vim-polyglot

sheerun/vim-polyglot, A collection of language packs for Vim.

This plugin acts weird. It changes the folding pattern of markdown/pandoc filetype, changes conceallevel in some filetype and overrides nmap <F3> in r. These cannot be disabled by g:polyglot_disabled. Considering remove it.

1
2
3
4
5
6
7
8
Plug 'sheerun/vim-polyglot'

" vim-polyglot
let g:polyglot_disabled = [
\ 'markdown',
\ 'pandoc',
\ 'python'
\]

python

1
2
3
4
5
6
7
Plug 'vim-python/python-syntax'
Plug 'tmhedberg/SimpylFold'

" python-syntax
let g:python_highlight_all = 1
" SimpylFold
let g:SimpylFold_docstring_preview = 1

Indent guide

1
2
3
4
5
6
7
8
Plug 'thaerkh/vim-indentguides'

"indentguides
let g:indentguides_firstlevel = 1
let g:indentguides_ignorelist = ['text', 'rmd', 'markdown', 'tex', 'rmarkdown', 'pandoc', 'nerdtree']
let g:indentguides_spacechar = '¦'
let g:indentguides_tabchar = '¦'
let g:indentguides_conceal_color = 'ctermfg=238 ctermbg=234 guifg=#4e4e4e guibg=NONE'

vim-gitgutter

1
2
3
4
Plug 'airblade/vim-gitgutter'

" gitgutter
set updatetime=100

Usage

  • ]c jump to the next hunk
  • [c jump to the previous hunk

Fold

  • Konfekt/FastFold, speed up Vim by updating folds only when called-for. Use zuz in normal mode to update the fold.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Plug 'Konfekt/FastFold'

" FastFold
let g:fastfold_force = 1 "To prevent all fold methods
let g:markdown_folding = 1
let g:tex_fold_enabled = 1
let g:vimsyn_folding = 'af'
let g:xml_syntax_folding = 1
"let g:javaScript_fold = 1 " uncommenting this will interrupt markdown/pandoc fold
let g:sh_fold_enabled= 7
let g:ruby_fold = 1
let g:perl_fold = 1
let g:perl_fold_blocks = 1
let g:r_syntax_folding = 1
let g:rust_fold = 1
let g:php_folding = 1

Distraction-free writing

1
2
3
4
5
6
Plug 'junegunn/goyo.vim'
Plug 'junegunn/limelight.vim'

nmap <F10> :Goyo<CR>
autocmd! User GoyoEnter Limelight
autocmd! User GoyoLeave Limelight!

Undo tree

1
2
3
4
5
6
7
8
Plug 'simnalamburt/vim-mundo'

" mundo
nnoremap <F5> :MundoToggle<CR>
let g:mundo_right = 1
" Enable persistent undo so that undo history persists across vim sessions
set undofile
set undodir=~/.local/share/nvim/undo

Rainbow Parentheses

1
2
Plug 'luochen1990/rainbow'
let g:rainbow_active = 1

CN doc

1
2
" cn doc
Plug 'yianwillis/vimcdoc'

The doc is for vim, not neovim. Removed.

Shell

1
2
Plug 'Shougo/vimproc.vim', {'do' : 'make'}
Plug 'Shougo/deol.nvim'

Match-up

1
2
3
4
5
6
7
8
9
10
11
12
Plug 'andymass/vim-matchup'

" Use compact syntax for prettified multi-line comments
let g:NERDCompactSexyComs = 1
" Align line-wise comment delimiters flush left instead of following code indentation
let g:NERDDefaultAlign = 'left'
" Allow commenting and inverting empty lines (useful when commenting a region)
let g:NERDCommentEmptyLines = 1
" Enable trimming of trailing whitespace when uncommenting
let g:NERDTrimTrailingWhitespace = 1
" Enable NERDCommenterToggle to check all selected lines is commented or not
let g:NERDToggleCheckAllLines = 1

clever-f.vim

1
Plug 'rhysd/clever-f.vim'

File-explorer

vimfiler does not respect set splitright. if set 'direction' : 'rightbelow', although the split window will appear on right, the vimfiler window will also on right. Considering change to NERDTree.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Plug 'Shougo/unite.vim'
Plug 'Shougo/vimfiler.vim'

" vimfiler
let g:webdevicons_enable_vimfiler = 1
let g:vimfiler_marked_file_icon = '*'
let g:vimfiler_as_default_explorer = 1
let g:vimfiler_ignore_pattern = [
\ '^\.git$',
\ '^\.DS_Store$',
\ '^\.init\.vim-rplugin\~$',
\ '^\.netrwhist$',
\ '\.class$',
\ '^\.'
\]
call vimfiler#custom#profile('default', 'context', {
\ 'explorer' : 1,
\ 'winwidth' : 30,
\ 'winminwidth' : 30,
\ 'toggle' : 1,
\ 'auto_expand': 1,
\ 'parent': 0,
\ 'status' : 1,
\ 'safe' : 1,
\ 'split' : 1,
\ 'hidden': 1,
\ 'no_quit' : 1,
\ 'force_hide' : 0,
\ })
nnoremap <F3> :VimFiler<cr>
autocmd FileType vimfiler nmap <buffer> s <Plug>(vimfiler_split_edit_file)

Neovim configure

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
" enable true color support
set termguicolors
set guicursor=

set nu "show line number
set fdm=indent " fold
syntax enable
set clipboard+=unnamedplus
set showmatch

" tab length setting
set tabstop=4
set expandtab
set shiftwidth=4
set softtabstop=4

set cursorline " 高亮光标行
if has('mouse')
set mouse=a
endif

set ignorecase
set smartcase

set smartindent

set scrolloff=5

Tips

  • nvim -u NONE will run neovim without plugins and init.vim, nvim -u NORC will run neovim without init.vim (with plugins from neovim). These are useful for troubleshooting.

  • Tab setting:1

与缩进相关的参数有shiftwidth、tabstop、softtabstop、expandtab。

shiftwidth reindent 操作(<<和>>)时缩进的列数(这里的一列相当于一个空格)
tabstop 一个tab键所占的列数,linux 内核代码建议每个tab占用8列
softtabstop 敲入tab键时实际占有的列数。
expandtab 输入tab时自动将其转化为空格

softtabstop大于tabstop时,且没有设置expandtab时,例如:softtabstop=12,tabstop=8,那么当输入一个tab时(softtabstop:实际占用的是12列),最后会变成一个tab(tabstop)加4个空格(8+4),输入两个tab(2个softtabstop:24列)会变成3个tab(tabstop),也就是说vim或用tabstop+空格来表示,最终你能看到的缩进的列数一定是softtabstop*按的tab键次数。(ps::set list 可以查看tab符号)

softtabstop小于tabstop且没有设置expandtab时,如果softtabstop=4,tabstop=8,输入一个tab(softtabstop),会变成4个空格(因为不够用一个tabstop表示),输入两个tab会变成一个tab(8列)。

如果softtabstop等于tabstop,而且expandtab没有设置,softtabstop与tabstop就没什么区别了。

如果设置的expandtab,输入一个tab,将被展开成softtabstop值个空格,如果softtabstop=4,那么一个tab就会被替换成4个空格。

References

1. http://liuzhijun.iteye.com/blog/1831548