LoginSignup
1
4

init.vimからinit.luaへの移行手順

Posted at

まずは、init.luaを作成

上記を導入

require("lazy").setup(plugins, opts)

をinit.lua二追記

上記はあくまで関数のサンプルなので、
下記のように書く。

require("lazy").setup {
  {'preservim/nerdtree'},
  {'romgrk/barbar.nvim'},
}

のようにいれたいプラグインを書いていく。

オプション設定

vim.o.number = true

こんな感じで書く。

oはoptionの略で、設定値を代入する形式

vim.o.clipboard = 'unnamed'

stringとかは、上記のように代入する。
ちなみに、上記のオプションでVimでヤンクしたものが、
レジスタに送られるので、Macとかだったら、
OSのクリップボードとしても使える。
(windowsとかだと、unnamedplusじゃないと、
連携されないかも)

あと、Keymapについては、

local map = vim.api.nvim_set_keymap
local opts = { noremap = true, silent = true }
map('i', 'jj', '<ESC>', opts)

みたいな感じにする。

最後の'i'はインサートモードの略なので、
ノーマルモードとかなら'n'にする。
jjと入力したら、にするというキーマップ

NERDTreeのキーマップとかは、

map('n', '<C-t>', ':NERDTreeToggle<CR>', opts)

みたいにする。

ちなみに、ノーマルモードとビジュアルモードとか、
複数のモードにしたいときは、

map('', '<C-t>', ':pwd', opts)

みたいに、空ストリング''にすると
Vim scriptでのmapと同様のマッピングになる。

コマンドの作成は、

vim.api.nvim_create_user_command('Pwd', 'pwd', { nargs = 0 })

みたいな感じで、Pwdと入力したら、pwdになる。
nargsは引数かな?

ファイルを分ける

luaだと、プラグインやオプション、キーマップとかで
ファイルを分けるやり方が、ネット上だと多いので、
それにならおうと思います。

luaディレクトリをinit.vimと同階層に作成し、
その配下にplugins.luaというというファイルを作成します。

lua/plugins.lua
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"

if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({
    "git",
    "clone",
    "--filter=blob:none",
    "https://github.com/folke/lazy.nvim.git",
    "--branch=stable", -- latest stable release
    lazypath,
  })
end
vim.opt.rtp:prepend(lazypath)


require('lazy').setup {
  {'preservim/nerdtree'},
  {'ctrlpvim/ctrlp.vim'},
  {'akinsho/toggleterm.nvim'},
  {'romgrk/barbar.nvim',
    dependencies = {
      'lewis6991/gitsigns.nvim', -- OPTIONAL: for git status
      'nvim-tree/nvim-web-devicons', -- OPTIONAL: for file icons
    },
    init = function() vim.g.barbar_auto_setup = false end,
    opts = {
      -- lazy.nvim will automatically call setup for you. put your options here, anything missing will use the default:
      -- animation = true,
      -- insert_at_start = true,
      -- …etc.
    },
    version = '^1.0.0', -- optional: only update when a new 1.x version is released
  },
}

そして、init.luaの方で、このファイルを読み込みます。

init.lua
require("plugins")

カラースキーム

vim.cmd [[ colorscheme habamax ]]

プラグイン設定の切り出し

gitsigns.nvimとかだと、
細かい設定が多いので、そのまま書くと行数が膨れる。
そこで下記のように切り出す。

plugins.lua
require('lazy').setup {
  {'lewis6991/gitsigns.nvim',
    config = function()
      require('settings/gitsigns')
    end
  },
}

まず、lazy.nvimの記法で、configを指定して、
その中で、requireで該当ファイルを呼び出す。
私は、settingsディレクトリ配下のプラグイン名のファイルに書くことにしました。

settings/gitsigns.lua
require('gitsigns').setup {
  signs = {
    add          = { text = '+' },
    change       = { text = '│' },
    delete       = { text = '_' },
    topdelete    = { text = '‾' },
    changedelete = { text = '~' },
    untracked    = { text = '┆' },
  },
  signcolumn = true,  -- Toggle with `:Gitsigns toggle_signs`
  numhl      = false, -- Toggle with `:Gitsigns toggle_numhl`
  linehl     = false, -- Toggle with `:Gitsigns toggle_linehl`
  word_diff  = false, -- Toggle with `:Gitsigns toggle_word_diff`
  watch_gitdir = {
    follow_files = true
  },
  attach_to_untracked = true,
  current_line_blame = true, -- Toggle with `:Gitsigns toggle_current_line_blame`
  current_line_blame_opts = {
    virt_text = true,
    virt_text_pos = 'eol', -- 'eol' | 'overlay' | 'right_align'
    delay = 10,
    ignore_whitespace = false,
  },
  current_line_blame_formatter = '<author>, <author_time:%Y-%m-%d> - <summary>',
  sign_priority = 6,
  update_debounce = 100,
  status_formatter = nil, -- Use default
  max_file_length = 40000, -- Disable if file is longer than this (in lines)
  preview_config = {
    -- Options passed to nvim_open_win
    border = 'single',
    style = 'minimal',
    relative = 'cursor',
    row = 0,
    col = 1
  },
  yadm = {
    enable = false
  },
}

これで有効になるはず

キーマップとかも切り出す

init.lua
require("keymaps")
lua/keymaps.lua
local map = vim.api.nvim_set_keymap
local opts = { noremap = true, silent = true }
map('i', 'jj', '<ESC>', opts)
map('n', '<C-t>', ':NERDTreeToggle<CR>', opts)
map('', 'ss', '^', opts)
map('', ';;', '$', opts)
map('n', 'un', ':<C-u>Unite buffer<CR>', opts)
map('n', 'fgr', ':<cmd>Telescope live_grep<CR>', opts)
map('t', '<space>jj', '<C-\\><C-n>', opts)
map('n', 'gx', '<Plug>(openbrowser-smart-search)', opts)

coc.nvim周り

plugins.lua
require('lazy').setup {
  {'neoclide/coc.nvim',
    branch = 'release'
  },
}

普通にインストールして、
vimを開くと[coc.nvim] build/index.js not found, please compile coc.nvim by: npm ciのような
エラーが出る。

vimで

:call coc#util#install()

実行したら消える。

あとは、公式にあるコマンドを実行する

:CocInstall coc-json coc-tsserver
1
4
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
1
4