Neovimのメモ

作成: 更新:
heroimage

はじめに

普段は前のように Changelog(もはや Markdown) にメモしているがとりあえずまとめておこうと思い書いた。メモなので項目もバラバラで訳には立たない。

とりあえず init.vim

"Vim Plug """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
call plug#begin('~/.local/share/nvim/plugged')

Plug 'dense-analysis/ale'
Plug 'ayu-theme/ayu-vim'
Plug 'rbgrouleff/bclose.vim'
Plug 'neoclide/coc.nvim', {'branch': 'master'}
Plug 'junegunn/fzf', {'dir': '~/.fzf_bin', 'do': './install --all'}
Plug 'yuki-yano/fzf-preview.vim', { 'branch': 'release/rpc' }
Plug 'lambdalisue/fern.vim', {'branch': 'main'}
Plug 'lambdalisue/gina.vim'
Plug 'sainnhe/gruvbox-material'
Plug 'itchyny/lightline.vim'
Plug 'yegappan/mru'
Plug 'nvim-treesitter/nvim-treesitter', {'do': ':TSUpdate'}
Plug 'nvim-lua/plenary.nvim'
Plug 'mechatroner/rainbow_csv'
Plug 'nvim-telescope/telescope.nvim'
Plug 'osyo-manga/vim-anzu'
Plug 'airblade/vim-gitgutter'
Plug 'bronson/vim-trailing-whitespace'
Plug 'prettier/vim-prettier'

call plug#end()
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" SETTINGS

" control
set autochdir
set backspace=eol,indent,start
set belloff=all
set clipboard=unnamedplus
set hidden
set history=5000
set mouse-=a
set wildmenu

" files
set noswapfile
set nobackup
set noundofile


" format
set formatoptions+=mM
set noautoindent
set smartindent
set textwidth=0

" search
set hlsearch
set incsearch
set ignorecase
set smartcase
set wrapscan

" display
"set ambiwidth=double
set cmdheight=2
set colorcolumn=80
set cursorline
set laststatus=2
set nolist
set noshowmode
set number
set ruler
set signcolumn=yes
set showtabline=2
set shortmess+=c
set showmatch
set showcmd
set termguicolors
set title
set wrap

" TAB
set expandtab
set tabstop=2
set shiftwidth=2
set smarttab
set softtabstop=2

" color
"colorscheme gruvbox-material
"colorscheme ayu
colorscheme desert

"ESCを二回押すことでハイライトを消す
nmap <silent> <Esc><Esc> :nohlsearch<CR>

" vを二回で行末まで選択
vnoremap v $h

" TABにて対応ペアにジャンプ
nnoremap <Tab> %
vnoremap <Tab> %

" j, k による移動を折り返されたテキストでも自然に振る舞うように変更
nnoremap j gj
nnoremap k gk
nnoremap <down> gj
nnoremap <up> gk

" H, L で先頭と最後に移動
noremap H 0
noremap L $

" 検索後にジャンプした際に検索単語を画面中央に持ってくる
nnoremap n nzz
nnoremap N Nzz
nnoremap * *zz
nnoremap # #zz
nnoremap g* g*zz
nnoremap g# g#zz

" ショートカット ,q バッファを閉じる
nnoremap  <silent>,q :bd<CR>

" バッファー操作
nnoremap <silent>> :bnext<CR>
nnoremap <silent>< :bprevious<CR>
nnoremap <silent>bb :b#<CR>
nnoremap <silent>bc :Bclose<CR>

" タブ操作
nnoremap <silent><C-t> :tabnew <Enter>
nnoremap <silent>tc :tabnew<CR>
nnoremap <silent>tp :tabprevious<CR>
nnoremap <silent>tn :tabnext<CR>
nnoremap <silent>tx :tabclose<CR>
nnoremap <C-q> :tabclose<CR>

" ターミナル表示
nnoremap  <silent>,t :T<CR>

" ターミナルの設定
:tnoremap <Esc> <C-\><C-n>
command! -nargs=* T split | wincmd j | resize 10 | terminal <args>
autocmd TermOpen * startinsert

""" <Tab>で候補をナビゲート
function! s:check_back_space() abort
  let col = col('.') - 1
  return !col || getline('.')[col - 1]  =~ '\s'
endfunction

inoremap <silent><expr> <Tab>
      \ pumvisible() ? "\<C-n>" :
      \ <SID>check_back_space() ? "\<Tab>" :
      \ coc#refresh()

""" <Tab>で次、<S+Tab>で前
inoremap <expr> <Tab> pumvisible() ? "\<C-n>" : "\<Tab>"
inoremap <expr> <S-Tab> pumvisible() ? "\<C-p>" : "\<S-Tab>"

" map prefix
let g:mapleader = "\<Space>"
nnoremap <Leader>    <Nop>
xnoremap <Leader>    <Nop>
nnoremap <Plug>(lsp) <Nop>
xnoremap <Plug>(lsp) <Nop>
nmap     m           <Plug>(lsp)
xmap     m           <Plug>(lsp)
nnoremap <Plug>(ff)  <Nop>
xnoremap <Plug>(ff)  <Nop>
nmap     ,           <Plug>(ff)
xmap     ,           <Plug>(ff)

" ightline """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
let g:lightline = {'colorscheme' : 'wombat'}
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" fern.vim """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
nnoremap <silent> <Leader>e <Cmd>Fern . -drawer<CR>
nnoremap <silent> <Leader>E <Cmd>Fern . -drawer -reveal=%<CR>
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" telescope """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Find files using Telescope command-line sugar.
nnoremap <leader>ff <cmd>Telescope find_files<cr>
nnoremap <leader>fg <cmd>Telescope live_grep<cr>
nnoremap <leader>fb <cmd>Telescope buffers<cr>
nnoremap <leader>fh <cmd>Telescope help_tags<cr>

" Using Lua functions
nnoremap <leader>ff <cmd>lua require('telescope.builtin').find_files()<cr>
nnoremap <leader>fg <cmd>lua require('telescope.builtin').live_grep()<cr>
nnoremap <leader>fb <cmd>lua require('telescope.builtin').buffers()<cr>
nnoremap <leader>fh <cmd>lua require('telescope.builtin').help_tags()<cr>
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" COC """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" coc.nvim
let g:coc_global_extensions = ['coc-tsserver', 'coc-eslint', 'coc-prettier', 'coc-git', 'coc-fzf-preview', 'coc-lists']

inoremap <silent> <expr> <C-Space> coc#refresh()

nnoremap <silent> K             <Cmd>call <SID>show_documentation()<CR>
nmap     <silent> <Plug>(lsp)rn <Plug>(coc-rename)
nmap     <silent> <Plug>(lsp)a  <Plug>(coc-codeaction-cursor)

function! s:coc_typescript_settings() abort
  nnoremap <silent> <buffer> <Plug>(lsp)f :<C-u>CocCommand eslint.executeAutofix<CR>:CocCommand prettier.formatFile<CR>
endfunction

augroup coc_ts
  autocmd!
  autocmd FileType typescript,typescriptreact call <SID>coc_typescript_settings()
augroup END

function! s:show_documentation() abort
  if index(['vim','help'], &filetype) >= 0
    execute 'h ' . expand('<cword>')
  elseif coc#rpc#ready()
    call CocActionAsync('doHover')
  endif
endfunction

" fzf-preview
let $BAT_THEME                     = 'gruvbox-dark'
let $FZF_PREVIEW_PREVIEW_BAT_THEME = 'gruvbox-dark'

nnoremap <silent> <Plug>(ff)l   :<C-u>FzfPreviewMruFilesRpc<CR>
nnoremap <silent> <Plug>(ff)F   :<C-u>FzfPreviewOldFilesRpc<CR>
nnoremap <silent> <Plug>(ff)B   :<C-u>CocCommand fzf-preview.AllBuffers<CR>

nnoremap <silent> <Plug>(ff)r  <Cmd>CocCommand fzf-preview.ProjectFiles<CR>
nnoremap <silent> <Plug>(ff)s  <Cmd>CocCommand fzf-preview.GitStatus<CR>
nnoremap <silent> <Plug>(ff)gg <Cmd>CocCommand fzf-preview.GitActions<CR>
nnoremap <silent> <Plug>(ff)b  <Cmd>CocCommand fzf-preview.Buffers<CR>
nnoremap          <Plug>(ff)f  :<C-u>CocCommand fzf-preview.ProjectGrep --add-fzf-arg=--exact --add-fzf-arg=--no-sort<Space>

nnoremap <silent> <Plug>(lsp)q  <Cmd>CocCommand fzf-preview.CocCurrentDiagnostics<CR>
nnoremap <silent> <Plug>(lsp)rf <Cmd>CocCommand fzf-preview.CocReferences<CR>
nnoremap <silent> <Plug>(lsp)d  <Cmd>CocCommand fzf-preview.CocDefinition<CR>
nnoremap <silent> <Plug>(lsp)t  <Cmd>CocCommand fzf-preview.CocTypeDefinition<CR>
nnoremap <silent> <Plug>(lsp)o  <Cmd>CocCommand fzf-preview.CocOutline --add-fzf-arg=--exact --add-fzf-arg=--no-sort<CR>
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""


"""""" ALE """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" 各ツールをFixerとして登録
let g:ale_fixers = {
\   'typescript': ['prettier'],
\   'typescriptreact': ['prettier'],
\   'javascript': ['prettier'],
\   'javascriptreact': ['prettier'],
\   'css': ['prettier'],
\}
let g:ale_sign_error = 'P>'
let g:ale_sign_warning = 'P-'
let g:ale_echo_msg_format = '[%linter%] %code: %%s'
let g:ale_statusline_format = ['E%d', 'W%d', 'OK']

nmap <silent> <C-w>j <Plug>(ale_next_wrap)
nmap <silent> <C-w>k <Plug>(ale_previous_wrap)
nmap <silent>,x <Plug>(ale_fix)

let g:ale_fix_on_save = 1
let g:ale_javascript_prettier_use_local_config = 1
let g:ale_javascript_prettier_options = '--single-quote --trailing-comma all'
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

"" treesitter """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
" :TSInstall 言語名 でシンタックスハイライトファイルをインストール
"
lua <<EOF
require'nvim-treesitter.configs'.setup {
  auto_install = true,
  sync_install = false,
  highlight = {
    enable = true,
    disable = {
      'lua',
      'c_sharp'
    },
    ensure_installed = {
      "astro",
      "python",
      "go",
      "js",
      "ts",
      "json",
      "toml",
      "yml",
      "hcl"
    },
    indent = {
      enable  = true
    },
    autotag = {
      enable = true
    }
  }
}
EOF
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

""""""" markdown """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set syntax=markdown
au BufRead,BufNewFile *.md set filetype=markdown

let g:markdown_timeformat = "%Y-%m-%d %H:%M:%S"
if has("autocmd")
 autocmd FileType markdown
  \     map ,w :call InsertMarkdownEntry()<CR>a
endif

if has("autocmd")
    autocmd FileType markdown
  \     map ,o :call InsertChangeLogEntry(" ubanis")<CR>a
endif

function! InsertMarkdownEntry()
    if strpart(getline(1), 0, 10) == "---"
 else
 let s:header = strftime("%Y-%m-%d %H:%M:%S")
 execute "normal gg0i---\<CR>layout: \"@layouts\/BlogPost.astro\"\<CR>title: \"メッセージレス\"\<CR>date: \"" . s:header . "\"\<CR>tags: [\"メッセージレス\"]\<CR>description: \"description\"\<CR>featured: \"/img/res.jpg\"\<CR>heroImage: \"\"\<CR>---\<CR>\<CR>"
 endif
endfunction

function! InsertChangeLogEntry(name)
    if strpart(getline(1), 0, 13) == "## " . strftime("%Y-%m-%d")
 execute "normal ggo\<CR>\###  []\<ESC>3h"
    else
 let s:header = "## " . strftime("%Y-%m-%d")
 execute "normal ggi\<CR>\<CR>\<ESC>kki" . s:header . "\<CR>\<CR>\###  []\<ESC>3h"
    endif
endfunction
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

config.json

{
    "suggest.triggerAfterInsertEnter": true,
    "suggest.noselect": false,
    "suggest.minTriggerInputLength": 2,
    "suggest.acceptSuggestionOnCommitCharacter": true,
    "suggest.snippetIndicator": " ►",
    "suggest.enablePreview": true,
    "coc.preferences.hoverTarget": "float",
    "coc.preferences.formatOnType": true,
      "coc.preferences.formatOnSaveFiletypes": [
    "css",
    "html",
    "markdown",
    "typescriptreact",
    "typescript",
    "javascriptreact",
    "javascript",
    "json",
    "graphql"
  ],
    "tsserver.formatOnType": true,
    "eslint.autoFixOnSave": true,
    "eslint.filetypes": [
        "javascript",
        "javascriptreact",
        "typescript",
        "typescriptreact"
    ],
}

Fern の操作方法

現状では Space + e で開く。以下操作方法の一部

コマンド動作
lフォルダを開く
hフォルダを閉じる
Returnフォルダに潜る
Ctr-hフォルダを 1 階層上に戻る
oファイル若しくはフォルダを開く
?コマンドのヘルプを開く

nvim coc で yarn install しろとか言われた場合

Neovim の coc があるフォルダ ~/.local/share/nvim/plugged/coc.nvim 以下などに行き coc フォルダへ移動してから以下のコマンドを実行する。

npx yarn install --frozen-lockfile

windows 用 Neovim

powershell で以下を実行

iwr -useb https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim | ni "$(@($env:XDG_DATA_HOME, $env:LOCALAPPDATA)[$null -eq $env:XDG_DATA_HOME])/nvim-data/site/autoload/plug.vim" -Force

mkdir %USERPROFILE%\AppData\Local\nvim

以下のファイルを編集する

~/AppData/Local/nvim/init.vim

call plug#begin(’~/.vim/plugged’)

フォント設定

~/AppData/Local/nvim/ginit.vim

Guifont! Cica:h18

以下を init.vim に書いても良い

set guifont=Cica:h18

pipenv 内での pylint

neovim で pylint が no module を返すときは pipenv install pylint をしていないため なので pipenv install pylint をそのフォルダ内で行うこと