Skip to content

jnsp/nvim-config

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 

Repository files navigation

My Neovim Config

This is my Neovim setting for Python development on macOS.

Install

Neovim

The easiest way to install Neovim is Homebrew. Other options are here.

brew install neovim

vim-plug

Vim itself is not very useful. We need more plugins for Vim. There are many options for installing vim plugin, but vim-plug is the simplest.

The install directory is different between Vim and Neovim. You can read this for Neovim.

sh -c 'curl -fLo "${XDG_DATA_HOME:-$HOME/.local/share}"/nvim/site/autoload/plug.vim --create-dirs \
       https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'

init.vim

init.vim is the configuration file of Neovim. It has a different path from Vim. The config path of Neovim is ~/.config/nvim/init.vim If you can't find this file, make it manually.

mkdir ~/.config/nvim/
touch ~/.config/nvim/init.vim

How to install a plugin in Neovim using vim-plug

If you want to install Nerdtree, you should write the URL of the repository in init.vim file. It looks like below.

call plug#begin('~/.vim/plugged')
  Plug 'preservim/nerdtree'
call plug#end()

This is vim script, and you should write your plugin between call plug#begin() and call plug#end(). Then open Neovim, write command :PlugInstall.

:PlugInstall

This is how it works. There are other commands for vim-plug here.

Plugin

I introduce some useful plugins with my taste. You can select some of them. But don't install every plugin without understanding what they do for you. There are so many vim plugins, and many people just installed them. Please install one of them carefully, watching the difference between before and after the plugin. It's like festive shopping.

Nerdtree

The NERDTree is a file system explorer. It makes a visual file tree in the Vim editor. The NERDTree has no shortcuts originally. It recommends some shortcuts to add on init.vim like below.

nnoremap <leader>n :NERDTreeFocus<CR>
nnoremap <C-n> :NERDTree<CR>
nnoremap <C-t> :NERDTreeToggle<CR>
nnoremap <C-f> :NERDTreeFind<CR>

This script adds shortcuts, for example <C-n> means ctrl-n and it toggles the file tree on the editor.

commentary.vim

This is the Vim plugin artist Tim Pope's commentary.vim. Just use gcc to comment out some codes. I used to do visual-block mode when I commented out without commentary.vim. gcc is just much easier.

Tim Pope did great jobs for vim users. You can look around his jobs here.

vim-polyglot

vim-polyglot brings better syntax highlighting than basic Vim, not only for Python. Enable all syntax highlighting features you can add the following command to init.vim.

let g:python_highlight_all = 1

onedark.vim

onedark.vim is one of the popular color scheme for Vim editor and my favorite. With installation, set color scheme on init.vim.

syntax on
colorscheme onedark

vim-airline

vim-airline offers a nice status line on the bottom of vim windows. It has its theme plugin, vim-airline-themes. This theme plugin has to be installed too.

vim-airline can use onedark theme installed above. Set airline_powerline_fonts = 1 to use powerline symbols on init.vim.

let g:airline_theme = 'onedark'
let g:airline_powerline_fonts = 1

ALE

ALE (Asynchronous Lint Engine) is a manager for all kinds of linter and fixer(sometimes called formatter). It's not a linter or fixer itself, but it controls how the specific linters and fixers behave. You can choose which linter or fixer is applied to your editor.

I use flake8 and pylint as my linter.

" ale linters
let g:ale_linters = {'python': ['flake8', 'pylint']}

And yapf is the fixer.

" ale fixers
let g:ale_fixers = {
\   '*': ['remove_trailing_lines', 'trim_whitespace'],
\   'python': ['yapf'],
\}

ALE is very powerful, but it has a lot of options. It would help if you read the doc for this plugin. There are some options for me below.

For example, You can fix your file every time you save it.

" ale - fix files when you save them.
let g:ale_fix_on_save = 1

You can use different linters simultaneously, but it is sometimes confusing which one is warning. You need kinder messages from your linters.

" ale - kinder message
" [flake8] E302: expected 2 blank lines, found 1 [E]
let g:ale_echo_msg_error_str = 'E'
let g:ale_echo_msg_warning_str = 'W'
let g:ale_echo_msg_format = '[%linter%] %s [%severity%]'

Linters make new space to specify the position of warning or error in your code. It could be annoying to flash the gutter space. It would be best if you kept the gutter space at all times.

" ale - keep the sign gutter open at all times
let g:ale_sign_column_always = 1

pylint is sometimes too strict. You need to set pylintrc configuration file to control it or use plugins for pylint. let g:alie_python_pylint_options is available on init.vim to load specific pylint plugin to ALE. I usually load pylint_flask and pylint_flaks_sqlalchemy plugin on init.vim. There are so many people frustrated by too kind pylint, so you can find every kinds of pylint plugin you need.

" ale - load pylint plugins
let g:ale_python_pylint_options = '--load-plugins pylint_flask pylint_flask_sqlalchemy'

You need to move the cursor quickly between warnings and errors by ctrl-j and ctrl-k.

" ale - moving between warnings and errors quickly
nmap <silent> <C-k> <Plug>(ale_previous_wrap)
nmap <silent> <C-j> <Plug>(ale_next_wrap)

Etc.

Line numbers will be displayed with set number on init.vim. And a vertical line for PEP8 with set colorcolumn=79.

set number
set colorcolumn=79

When you search some keywords in vim, the search highlight never disappears until you command :noh. You can make shortcuts to clear the hightlight by pressing esc-esc or ctrl-l.

nnoremap <esc><esc> :noh<cr>
nnoremap <C-L> :noh<cr>

tmux

tmux is not about Vim. But if you do something with terminal, tmux is your best friend to split the window.

tmux plugin manager

tmux also has a lot of plugins. It would help if you had plugin manager to install them.

git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm

Don't forget to make a tmux configuration file, ~/.tmux.conf like below. Note that tmux-plugins/tpm and tmux-plugins/tmux-sensible is mandatory to use the tmux plugin manager.

# List of plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'

# Other examples:
# set -g @plugin 'github_username/plugin_name'
# set -g @plugin 'git@github.com:user/plugin'
# set -g @plugin 'git@bitbucket.com:user/plugin'

# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
run '~/.tmux/plugins/tpm/tpm'

It's easy to install new plugin.

  1. Add new plugin to ~/.tmux.conf with set -g @plugin '<PLUGIN>'
  2. Press prefix + I (capital i, as in Install) to fetch the plugin.

tmux themepack

The first reason I installed the tmux plugin manager is tmux themepack to be out of the green status bar of tmux. You have diffenrent options for color, not just ugly green here. I chose gray.

set -g @plugin 'jimeh/tmux-themepack'
set -g @themepack 'powerline/default/gray'

True color issue

When you use some colorthemes for Vim or tmux, you would face the true color issue in terminal. You can test your truecolor environment with this script.

curl -s https://raw.githubusercontent.com/JohnMorales/dotfiles/master/colors/24-bit-color.sh | bash

If you can see the beautiful gradation of colors, you're able to use true color in your Vim editor.

Although you have true color environment, you could have some problems with color. Then, add some extra setting like below on init.vim. I copied this code from onedark.vim install part. Be sure to read tmux-related notes in the firs few line.

"Use 24-bit (true-color) mode in Vim/Neovim when outside tmux.
"If you're using tmux version 2.2 or later, you can remove the outermost $TMUX check and use tmux's 24-bit color support
"(see < http://sunaku.github.io/tmux-24bit-color.html#usage > for more information.)
if (empty($TMUX))
  if (has("nvim"))
    "For Neovim 0.1.3 and 0.1.4 < https://github.com/neovim/neovim/pull/2198 >
    let $NVIM_TUI_ENABLE_TRUE_COLOR=1
  endif
  "For Neovim > 0.1.5 and Vim > patch 7.4.1799 < https://github.com/vim/vim/commit/61be73bb0f965a895bfb064ea3e55476ac175162 >
  "Based on Vim patch 7.4.1770 (`guicolors` option) < https://github.com/vim/vim/commit/8a633e3427b47286869aa4b96f2bfc1fe65b25cd >
  " < https://github.com/neovim/neovim/wiki/Following-HEAD#20160511 >
  if (has("termguicolors"))
    set termguicolors
  endif
endif

Mosh

Truecolor issue is also related with Mosh. When you connect to a server with mosh, open tmux, and code in Vim, the coloring is twisted so much.

Fortunately, complicated issues are almost resolved. But tricky thing is you should install the latest version of Mosh. (This is written in 2021-02-21.)

Install latest mosh on Mac with Homebrew.

brew install mosh --HEAD

Or you can compile from git

git clone https://github.com/mobile-shell/mosh
cd mosh
./autogen.sh
./configure
make
make install

In my case, I have no problem in the coloring of Vim editor through mosh and tmux after installing of the latest mosh. If you can't solve the problem, go with 256 color setting with mosh.

Something useful

These are very optional but I recommend it.

Oh My Zsh

Now that the default shell of macOS is zsh, Oh My Zsh is a viable option for cumtomizing the shell itself. Especially, the theme is convenient.

Install with one-line bash command.

sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

Powerlevel10k

Powerlevel10k is a zsh theme. It's easy to set and works fast. There are so many different ways to install. Select the proper one from the installation guide.

Managing configuration files with github and soft link

I include two configuration files in this repository. Every time I set a new environment, I just clone this repository and make soft links with these files linked to the right paths. There is no more copy and paste and twisted configs everywhere.

If I add some new settings, I have to edit it only in this directory and save it.

cd projects
git clone git@github.com:jnsp/nvim-config.git
ln -s ~/projects/nvim-config/init.vim ~/.config/nvim/init.vim
ln -s ~/projects/nvim-config/.tmux.conf ~/.tmux.conf

This whole document is just for me in the future. 😀

References

I got a help to configure my nvim settings from some excellent articles. Thanks to the authors for sharing their experience.

About

My Neovim config

Resources

Stars

Watchers

Forks