0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

neovimにcoc-snippetsをインストールしてスニペットを登録する

Posted at

インストール

nvimを開いて以下のコマンドを打つ

:CocInstall coc-snippets

このようなエラーが出てきた

Error on execute python script: request error nvim_command - Vim(pyxfile):E319: No "python3" provider found. Run ":checkhealth provider"

:checkhealth providerを実行するらしい

:checkhealth provider

## Python 3 provider (optional)
  - WARNING: No Python executable found that can `import neovim`. Using the first available executable for diagnostics.
  - ERROR: Python provider error:
    - ADVICE:
      - provider/pythonx: Could not load Python 3:
          /opt/homebrew/bin/python3 does not have the "neovim" module. :help provider-python
          python3.10 not found in search path or not executable.
          /opt/homebrew/bin/python3.9 does not have the "neovim" module. :help provider-python
          python3.8 not found in search path or not executable.
          python3.7 not found in search path or not executable.
          python3.6 not found in search path or not executable.
          /usr/bin/python is Python 2.7 and cannot provide Python 3.
  - INFO: Executable: Not found

今度は:help provider-pythonを実行

:help provider-python


PYTHON QUICKSTART ~

To use Python plugins, you need the "pynvim" module. Run |:checkhealth| to see
if you already have it (some package managers install the module with Nvim
itself).

For Python 3 plugins:
1. Make sure Python 3.4+ is available in your $PATH.
2. Install the module (try "python" if "python3" is missing): >
   python3 -m pip install --user --upgrade pynvim

For Python 2 plugins:
1. Make sure Python 2.7 is available in your $PATH.
2. Install the module (try "python" if "python2" is missing): >
   python2 -m pip install --user --upgrade pynvim

The pip `--upgrade` flag ensures that you get the latest version even if
a previous version was already installed.

今回はpython3のproviderがないというエラーだったので"For Python 3 Plugins"の項目をチェックする

  1. シェル上でecho $PATH → 大丈夫そう
  2. python3 -m pip install --user --upgrade pynvim を実行

再び:checkhealth providerを実行してみると

:checkhealth provider

## Python 3 provider (optional)
  - INFO: `g:python3_host_prog` is not set.  Searching for python3 in the environment.
  - INFO: Multiple python3 executables found.  Set `g:python3_host_prog` to avoid surprises.
  - INFO: Executable: /opt/homebrew/bin/python3
  - INFO: Other python executable: /usr/bin/python3
  - INFO: Python version: 3.9.5
  - INFO: pynvim version: 0.4.3
  - OK: Latest pynvim is installed.

スニペットを登録する

スニペットを登録したいファイルタイプのファイルを開いて以下のコマンドを打つ
今回はpythonのスニペットを登録してみることにするので、hoge.pyを開いてコマンドを打つ

:CocCommand snippets.editSnippets

すると~/.config/coc/ultisnips/python.snippetsが開かれる
~/.config/coc/ultisnips/[ファイルタイプ].snippetsにスニペットを登録するみたい
以下のようにスニペットを登録してみる

~/.config/coc/ultisnips/python.snippets
snippet fori "for (i=0; i<max; i++) ..."
	for (${1:Counter}=0; $1<${2:Max}; $1++):
		${3:Implementetion}
endsnippet

このように設定することで"fori"と打つとスニペット機能が選択できます
coc-snippetsのREADMEをみると、デフォルトではCtrl+jとCtrl+kで上記の$1とか$2とかに移動できるみたい
https://github.com/neoclide/coc-snippets

なのでこのスニペットを使うにはpythonファイルを開いてこのようにタイプしてみると
fori → Enter → a → Ctrl+j → MIN → Ctrl+j → hogehoge
以下のように補完される

hoge.py
    for (a=0; a<MIN; a++):
        hogehoge
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?