Skip to content

Vim Configuration Recommendations

Michael Goerz edited this page Dec 30, 2019 · 1 revision

Neovim

Neovim is a fork of vim. It originated the ability to run plugins asynchronously (in the background), which is essential for "linters" (programs that check your code as you type it, such as ale). Under pressure from this competition, vimcaught up with the ability in version 8. For the most part, neovim and vim behave identical, and you should be able to use the same configuration for either program.

Make sure you run either neovim or vim 8!

Full Vim Configuration

You may use this full vim configuration: goerz/vimrc.

However, generally, it is better to write your own configuration, and be selective about what you include. Don't put anything in your configuration that you don't understand. For "large" plugins (something like ale), use vim-pathogen and then copy the entire ale plugin folder into ~/.vim/bundle. For small plugins, you can just directly put them into the appropriate subfolder of ~/.vim/. This way, you can take ownership of them and adapt them to your own needs.

Persistent undo

This (in your vimrc) will make undo work even if you restart vim:

" persistent undo
if has('persistent_undo')
    set undodir=~/.vim/undo/
    set undofile
    augroup persistent_undo
        autocmd!
        autocmd BufWritePre /tmp/* setlocal noundofile
    augroup END
endif

Remove trailing whitespace automatically

You can tell vim to remove trailing whitespaces automatically when you save certain files. For example for *.py files:

if has('autocmd')
    augroup removetrailingspaces
        autocmd BufWritePre *.py normal m`:%s/\s\+$//e ``
        autocmd BufWritePre *.pl normal m`:%s/\s\+$//e ``
    augroup END
endif

ALE configuration

If you are using ale, you can put the following settings in your vimrc:

" ALE plugin
let g:ale_completion_enabled = 0
let g:ale_set_loclist = 1
let g:ale_set_quickfix = 0   " quickfix is better used for :make
let g:ale_sign_error = '✖'
let g:ale_sign_warning = '△'
let g:ale_set_highlights = 0  " these are highlights inside the buffer
let g:ale_warn_about_trailing_whitespace = 0  " I have my own way for dealing with this (in the statusline)
let g:ale_linter_aliases = {
\   'human': 'text',
\   'pandoc': 'markdown'
\}
let g:ale_linters = {
\   'python': ['flake8', 'pydocstyle', 'pylint'],
\}
let g:ale_rst_rstcheck_use_project_config = 0
" The sign-column highlights for ALE are best left unintrusive:
hi link ALEWarningSign SignColumn
hi link ALEErrorSign SignColumn

Typing mathematical unicode

Numerical code and documentation can be much more readable if you use unicode for Greek letters and mathematical symbols. Since the Julia language encourages unicode identifiers, the julia-vim plugin enables the ability to type out LaTeX commands followed by pressing the [tab] key in order to insert the equivalent unicode. For example:

\alpha[tab]                →       α
v\_0[tab]                  →       v₀
|\Psi[tab]\rangle[tab]     →       |Ψ⟩
\"o                        →       ö
H\hat[tab]                 →       Ĥ
U\tilde[tab]               →       Ũ

To make this feature available not just in Julia files, add the following to your vimrc:

" LaTeX to Unicode substitutions
"  This is mainly for Julia, but I also like to use it for Python and others
let g:latex_to_unicode_file_types = [
    \ 'julia', 'python', 'mail', 'markdown', 'pandoc', 'human']
noremap <silent> <leader>l :call LaTeXtoUnicode#Toggle()<CR>

The last line defines a shortcut for the leader key (backslash by default, but , is a better choice) followed by l to activate the feature in any file.

Note: In Python, keep unicode out of public APIs. You can use unicode symbols internally (sparingly!), and in comments or docstrings.