Vim 搭建与问题

Table of Contents

介绍

主要一些常用的插件和遇到的问题

Vim(pythonx):ModuleNotFoundError: No module named 'neovim'

安装自动完成插件deoplete.nvim后,启动vim每次都是这个错误。
或者vim中执行:echo neovim_rpc#serveraddr(),会报更详细的错误。

failed executing: pythonx import neovim
Vim(pythonx):ModuleNotFoundError: No module named 'neovim'

搜索网上。

大多数都是简易安装。如果是用python3的话,pip3 install pynvim就行。本人macos系统,目前的python版本系统很混乱,一个是使用pyenv管理python版本,一个是基于系统默认版本python3下利用python -m venv appenv来创建,一个是brew安装的python版本。利用默认python安装后还是vim失败。

  1. pyenv
    虽然使用了pyenv,但目前并没有启用,可以通过以下命令看出。
pyenv versions
* system (set by ~/.pyenv/version)
  3.5.7
  3.6.8
  3.7.3
$pyenv which python2 // 看下节命令行默认python的来源,可以看到这个python来自brew的Cellar安装的
/usr/local/bin/python2
$ pyenv which python3
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3
$ pyenv which python3.7
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 
其他python版本没有。

~/.bash_profile中设置pyenv启动,

  1. 命令行默认python的来源
~/.bash_profile中设置系统默认python PATH为/Library/Frameworks/Python.framework/Versions/3.7/bin 目前为python 3.7.1
$ which python2 //默认使用来自brew的python2,python2.7的也是如此来源。
/usr/local/bin/python2
$ ls -lah /usr/local/bin/python2
lrwxr-xr-x  1 jg  admin    39B Feb 19  2019 /usr/local/bin/python2 -> ../Cellar/[email protected]/2.7.15_3/bin/python2

$ which python3  默认使用的pyenv设置的系统python3.7.1 
/Users/jg/.pyenv/shims/python3
$ ls -lah /Users/jg/.pyenv/shims/python3
-rwxr-xr-x  1 jg  staff   415B Sep  6  2019 /Users/jg/.pyenv/shims/python3
$ python3 --version
Python 3.7.1
$ pyenv which python3  这个是3.7.1的系统默认版本,Library目录下只有这个3的版本。
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3
$ /Library/Frameworks/Python.framework/Versions/3.7/bin/python3 --version
Python 3.7.1
  1. macos系统自身的版本,默认不能访问
    macos 系统自身的python版本,python2 link住/System/目录,这个system下目录只有python2的包。,python3直接安装到此目录。
$ ls -lah /usr/bin/python2
lrwxr-xr-x  1 root  wheel    75B Nov 13 23:07 /usr/bin/python2 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
$ /usr/bin/python2 --version
Python 2.7.16

$ /usr/bin/python3 --version //who install????难道是pyenv安装的??
Python 3.7.3
$ ls -lah /usr/bin/python3
-rwxr-xr-x  1 root  wheel    31K Jan 23 22:13 /usr/bin/python3
  1. brew安装的版本
    不能直接默认访问。
$ ls /usr/local/Cellar/python*/
/usr/local/Cellar/python/:
3.7.4   3.7.5

/usr/local/Cellar/python3/:
3.7.4   3.7.5

/usr/local/Cellar/[email protected]/:
2.7.15_3

事实确认

本人安装deoplete.nvim插件,用的是从brew安装的macvim,不是neovim。

$ ls -lah `which vim`
lrwxr-xr-x  1 jg  admin    32B Nov 25 17:08 /usr/local/bin/vim -> ../Cellar/macvim/8.1-161/bin/vim
用的是哪的vim配置文件呢?通过以下命令确认
$ vim --version
VIM - Vi IMproved 8.1 (2018 May 18, compiled Oct 30 2019 23:05:13)
macOS version
Included patches: 1-2234
Compiled by Homebrew
   system vimrc file: "$VIM/vimrc"
     user vimrc file: "$HOME/.vimrc"
 2nd user vimrc file: "~/.vim/vimrc"
      user exrc file: "$HOME/.exrc"
  system gvimrc file: "$VIM/gvimrc"
    user gvimrc file: "$HOME/.gvimrc"
2nd user gvimrc file: "~/.vim/gvimrc"
       defaults file: "$VIMRUNTIME/defaults.vim"
    system menu file: "$VIMRUNTIME/menu.vim"
  fall-back for $VIM: "/Applications/MacVim.app/Contents/Resources/vim"

看网上介绍,确实也在vimrc中设置了let g:python3_host_prog="PATH",但问题依旧。

反思

既然安装了,没有效果,可能是真的不是用的vimrc设置中的python。
这里vim --version起了很好的启示。其输出的最后有其编译时的link选项,可以看到其用的python库路径是/usr/local/opt/python/Frameworks/Python.framework/Versions/3.7。
因此,应该可以在这个python版本下来安装pynvim。在没有pip情况下,采用如下命令

/usr/local/opt/python/Frameworks/Python.framework/Versions/3.7/bin/python3  -m pip install pynvim

成功

这个问题在一年前升级系统后就一直存在着。让自己对vim的使用有了挫折感,基本停留在基本功能的使用上,而且也出现了一个很不好的体验:在这个bug存在下,一旦我打开vim然后退出vim。在iterm里往回滚动历史输出,会发现刚刚vim命令执行前的一些命令的输出几乎都乱了,无法查询。

虽然自己不乏解决问题的能力,但可怕的是遇到问题,自己没有去认真的去解决,而是接受现状,这不可取。

遗留问题

  • vim的启动/debug方法
  • 插件失败,导致iterm输出混乱的问题

rust插件

install vim-race for deoplete(vim)

  • 安装racer
https://github.com/racer-rust/racer
$ rustup toolchain add nightly
info: syncing channel updates for 'nightly-x86_64-apple-darwin'
464.0 KiB / 464.0 KiB (100 %) 199.5 KiB/s in  3s ETA:  0s
info: latest update on 2020-03-19, rust version 1.44.0-nightly (f509b26a7 2020-03-18)
info: downloading component 'cargo'
  3.8 MiB /   3.8 MiB (100 %) 128.0 KiB/s in 28s ETA:  0s
info: downloading component 'clippy'
  1.4 MiB /   1.4 MiB (100 %) 115.2 KiB/s in 12s ETA:  0s
info: downloading component 'rust-docs'
 12.1 MiB /  12.1 MiB (100 %)  22.4 KiB/s in  7m  6s ETA:  0s
info: downloading component 'rust-std'
 16.4 MiB /  16.4 MiB (100 %)  25.6 KiB/s in  7m 42s ETA:  0s
info: downloading component 'rustc'
 56.0 MiB /  56.0 MiB (100 %)   1.3 MiB/s in  5m  0s ETA:  0s
info: downloading component 'rustfmt'
  2.2 MiB /   2.2 MiB (100 %)   1.3 MiB/s in  1s ETA:  0s
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
 12.1 MiB /  12.1 MiB (100 %)   2.9 MiB/s in  3s ETA:  0s
info: installing component 'rust-std'
 16.4 MiB /  16.4 MiB (100 %)  15.5 MiB/s in  1s ETA:  0s
info: installing component 'rustc'
 56.0 MiB /  56.0 MiB (100 %)  10.0 MiB/s in  5s ETA:  0s
info: installing component 'rustfmt'

  nightly-x86_64-apple-darwin installed - rustc 1.44.0-nightly (f509b26a7 2020-03-18)

info: checking for self-updates
$ cargo +nightly install racer
http_proxy='http://192.168.1.1:8123' https_proxy='http://192.168.1.1:8123' cargo +nightly install racer

    Updating crates.io index
  Downloaded racer v2.1.31
  Downloaded 1 crate (182.1 KB) in 22.27s
  Installing racer v2.1.31
  Downloaded derive_more v0.99.3
  Downloaded rls-span v0.5.2
  Downloaded lazycell v1.2.1
  Downloaded racer-cargo-metadata v0.1.1
  Downloaded env_logger v0.7.1
  Downloaded clap v2.33.0
  Downloaded rustc-ap-rustc_ast_pretty v642.0.0
  Downloaded rustc-ap-rustc_data_structures v642.0.0
  Downloaded rustc-ap-rustc_errors v642.0.0
  Downloaded rustc-ap-rustc_session v642.0.0
  Downloaded rustc-ap-rustc_span v642.0.0
  Downloaded rustc-ap-syntax v642.0.0
  Downloaded humantime v2.0.0
  Downloaded rustc-ap-rustc_parse v642.0.0
  Downloaded termcolor v1.1.0
  Downloaded racer-interner v0.1.0
  Downloaded ansi_term v0.11.0
  Downloaded termize v0.1.1
  Downloaded rustc-rayon-core v0.3.0
  Downloaded strsim v0.8.0
  Downloaded rustc-ap-graphviz v642.0.0
  Downloaded crossbeam-utils v0.6.6
  Downloaded rustc-hash v1.1.0
  Downloaded stable_deref_trait v1.1.1
  Downloaded rustc-ap-rustc_index v642.0.0
  Downloaded rustc-ap-serialize v642.0.0
  Downloaded rustc-ap-rustc_feature v642.0.0
  Downloaded libc v0.2.68
  Downloaded rustc-ap-rustc_fs_util v642.0.0
  Downloaded humantime v1.3.0
  Downloaded crossbeam-queue v0.1.2
  Downloaded serde v1.0.105
  Downloaded measureme v0.7.1
  Downloaded parking_lot v0.9.0
  Downloaded ena v0.13.1
  Downloaded rustc-ap-arena v642.0.0
  Downloaded jobserver v0.1.21
  Downloaded scoped-tls v1.0.0
  Downloaded rustc-ap-rustc_macros v642.0.0
  Downloaded rustc-ap-rustc_lexer v642.0.0
  Downloaded rustc-ap-rustc_attr v642.0.0
  Downloaded quick-error v1.2.3
  Downloaded crossbeam-deque v0.7.3
  Downloaded rustc-ap-rustc_target v642.0.0
  Downloaded synstructure v0.12.3
  Downloaded annotate-snippets v0.6.1
  Downloaded maybe-uninit v2.0.0
  Downloaded rustc-rayon v0.3.0
  Downloaded itertools v0.8.2
  Downloaded lock_api v0.3.3
  Downloaded serde_derive v1.0.105
  Downloaded memmap v0.7.0
  Downloaded parking_lot_core v0.6.2
  Downloaded rustc_version v0.2.3
  Downloaded crossbeam-epoch v0.8.2
  Downloaded crossbeam-utils v0.7.2
  Downloaded smallvec v0.6.13
  Downloaded either v1.5.3
  Downloaded scopeguard v1.1.0
  Downloaded semver v0.9.0
  Downloaded memoffset v0.5.4
  Downloaded semver-parser v0.7.0  
  .cargo/bin/racer
  
$ rustup component add rust-src
info: downloading component 'rust-src'
  2.2 MiB /   2.2 MiB (100 %) 108.8 KiB/s in 18s ETA:  0s
info: installing component 'rust-src'

add 
export RUST_SRC_PATH=~/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/src/rust/src
to vimrc 
source ~/.bashrc
$ racer complete std::io::B
测试成功
To complete names in external crates, Racer needs Cargo.lock. So, when you add a dependency in your Cargo.toml, you have to run a build command such as cargo build or cargo test, to get completions.
add plugin which i use vundle
Plugin 'racer-rust/vim-racer' to vimrc
set hidden
let g:racer_cmd = "/Users/jg/.cargo/bin/racer"
let g:racer_experimental_completer = 1
let g:racer_insert_paren = 1
augroup Racer
    autocmd!
    autocmd FileType rust nmap <buffer> gd         <Plug>(rust-def)
    autocmd FileType rust nmap <buffer> gs         <Plug>(rust-def-split)
    autocmd FileType rust nmap <buffer> gx         <Plug>(rust-def-vertical)
    autocmd FileType rust nmap <buffer> gt         <Plug>(rust-def-tab)
    autocmd FileType rust nmap <buffer> <leader>gd <Plug>(rust-doc)
augroup END

C-x-C-o to search for completions and provides several <Plug> mappings for source code navigation.
与depolete的关系?

bugs

unexpected keyword argument 'encoding' when enter insert mode on opened rust file

https://github.com/Shougo/deoplete.nvim/issues/1071
https://github.com/Shougo/deoplete.nvim/pull/1070
vim vundle update all plugins
:PluginUpdate
and python install msgpack

how to use racer ?

macos darkmode conflict with the iterm color soloris mode. so first use the light mode for the macos.
Later setup the color scheme for the macos development.

Plugin 'rust-lang/rust.vim'

https://github.com/rust-lang/rust.vim
:h vundle
:help rust-syntastic
The installation of Tagbar along with Universal Ctags is recommended for a good Tagbar experience. For other kinds of setups, rust.vim tries to configure Tagbar to some degree

rls insall

https://github.com/rust-lang/rls#setup

rustup component add rls
error: toolchain 'stable-x86_64-apple-darwin' does not contain component 'rls' for target 'x86_64-apple-darwin'; did you mean 'rustc'?
rustup toolchain remove stable && rustup toolchain add stable
rustup uninstall stable
$ rustup install stable
rustup component add rls rust-analysis rust-src
https://rls.booyaa.wtf/(screen how)

Plug 'rust-lang/rust.vim'
Plug 'prabirshrestha/async.vim'
Plug 'prabirshrestha/vim-lsp'
Plug 'prabirshrestha/asyncomplete.vim'
Plug 'prabirshrestha/asyncomplete-lsp.vim'

https://hoverbear.org/blog/setting-up-a-rust-devenv/
good design toc and font style
https://github.com/Hoverbear/hoverbear.org/ jkikkwy
rust-clippy - A linter.
rustfmt - A code formatter.
racer - An autocompletion utility.
rls - A language server (for symbol search etc.)
cargo +nightly install clippy

cargo +nightly clippy
Excellent. You can see the different lints which clippy detects here. You can configure various lints in the clippy.toml according to their options listed in the wiki.

To disable (or warn instead of deny) various lints you can add the deny and allow flags to your crate attributes:

#![cfg_attr(feature = "cargo-clippy", deny(empty_enum))]
#![cfg_attr(feature = "cargo-clippy", warn(indexing_slicing))]
#![cfg_attr(feature = "cargo-clippy", allow(print_with_newline))]
cargo install rustfmt
t this point we can run cargo fmt to format a repository. It runs rustfmt in the 'replace' mode which creates backup files with a .bk extension. If our project is already in version control we may not want this. If that is the case we can edit rustfmt.toml to include the following:

write_mode = "overwrite"
The rustfmt.toml lets us configure the various options found in rustfmt --config-help. Now let's edit the main.rs we made later to have some obviously poor style:
good pratice
https://hoverbear.org/blog/setting-up-a-rust-devenv/
  • rename
  • hover look at proptye
  • go to define
  • find sysmbol
  • autocomplete
  • disgnaros
  • formating
  • codeaction

There are three choices of Vim plugins for language service protocol (LSP clients. Follow the respective link for information about how to register cquery server with one of them:

LanguageClient-neovim
vim-lsp
vim-lsc

vim tips

If :echo has("python3") returns 1, then you have python 3 support
pip3 install --user pynvim
:h vundle

vs code tips

cmd+p to pane
hover goto symbol goto define peek format docment come complete

Webmentions

Loading...

When you post a tweet with a link to this post it will automatically show up here! (refreshed every 30 minutes) 💯

A small favor

Was anything I wrote confusing, outdated, or incorrect? Please let me know! Just write a few words below and I'll be sure to amend this post with your suggestions.

Follow along

If you want to know about new posts, add your email below. Alternatively, you can subscribe with RSS.

More from 格物治用

实践、探索、思考.

View all posts