init.vimからinit.luaへ移行した
|
init.vimは元々はVimの設定ファイルでした。Neovimが登場してからNeovimの設定ファイルとしても使われるようになりますが、標準でサポートしているのがinit.luaのため、そのうちluaに書き換えないといけないなと思っていました。
先日Packer.nvimの挙動がおかしくなったのを期に設定ファイルの見直しを行い、init.vimをinit.luaへ置換しました。
事前準備
もともとinit.vimが設置されていた場所と同じ、~/.config/nvim/init.lua
に設置します。
init.vimが存在していると競合するため、init.vimを削除またはリネームするか、別の場所に移動します。
init.vimからinit.luaへ移行
init.vimとinit.luaでは書き方が異なるため、設定内容の書き換えが必要です。
文法や書き方についてはnvim-lua-guide-jaやlua-guide.jaxなどに記載されています。
ドキュメントがあるとはいえ、設定ファイルの書き換えは大変な作業です。
私のinit.vimは100行程度でしたがそれでも億劫に感じました。
そのため、上記の参考リンクをchatgptに渡して書き換えてもらいました🙂
2箇所ほど手動で修正しましたが、10min程度で書き換えが完了しました。
hodaのかつてのinit.vim
1" ----------------------------------------
2" Key bind and other setting.
3" ----------------------------------------
4set encoding=utf-8 " Prevent garbled characters
5set fileencoding=utf-8 " Setting for handling multi byte characters
6scriptencoding utf-8 " Setting for handling multi byte characters
7set number " Add row number
8set title " Add a filename to each tabs
9set cursorline " Add cursor line
10set tabstop=4 " Insert spaces when the tab key is pressed
11set shiftwidth=4 " Change the number of spaces inserted for indentation
12" set softtabstop=4 " Make spaces feel like real tabs
13set expandtab " Convert tabs to spaces
14set smartindent " Add a new line with autoindent
15set colorcolumn=120 " Add a color on 80'th column
16set hlsearch " Highlight searched characters
17set incsearch " Highlight when inputting chars
18set wildmenu " Show completion suggestions at command line mode
19set conceallevel=0 " Show double quatations in json file and so on.
20let g:mapleader = "\<Space>" " Set a space key to a leader.
21set mouse= " Don't use a mouse.
22set signcolumn=yes "Always show signcolumn to prevent rattling.
23
24" ----------------------------------------
25" Remove unnecessary spaces at the end of line.
26" ----------------------------------------
27augroup auto_remove_unnecessary_spaces_at_the_end_of_line
28 autocmd!
29 autocmd BufWritePre * :%s/\s\+$//ge "Auto remove unnecessary spaces at the end of line.
30augroup END
31
32" ----------------------------------------
33" Copy to the system clipboard.
34" ----------------------------------------
35if has('clipboard')
36 set clipboard=unnamed
37endif
38
39" ----------------------------------------
40" Remember a history of undo/redo.
41" ----------------------------------------
42if has('persistent_undo')
43 let undo_path = expand('~/.vim/undo/')
44 exe 'set undodir =' . undo_path
45 set undofile
46endif
47
48" ----------------------------------------
49" Settings for indent each files.
50" ----------------------------------------
51augroup html_css_js_and_others_indent
52 autocmd!
53 autocmd BufNewFile,BufRead *.yml,*.yaml,*.tmpl,*json :set tabstop=2 shiftwidth=2
54 autocmd BufNewFile,BufRead *.html,*.css,*.js,*.ts,*.php :set tabstop=2 shiftwidth=2
55 autocmd BufNewFile,BufRead *.go :set noexpandtab tabstop=8 shiftwidth=8
56augroup END
57
58" ----------------------------------------
59" Open .vimrc and 'source' it.
60" ----------------------------------------
61nnoremap <Leader>. :vs ~/.config/nvim/init.vim<CR>
62nnoremap <Leader>s :source ~/.config/nvim/init.vim<CR>
63
64" ----------------------------------------
65" Clear highlighted characters.
66" ----------------------------------------
67nnoremap <C-[><C-[> :nohlsearch<CR>
68
69" ----------------------------------------
70" vimshell setting.
71" ----------------------------------------
72if has('nvim')
73 nnoremap <Leader>- :split term://bash<CR>
74 nnoremap <Leader>l :vsplit term://bash<CR>
75elseif !has('nvim')
76 nnoremap <Leader>- :below terminal ++close ++rows=13 bash<CR>
77 nnoremap <Leader>l :vertical terminal ++close bash<CR>
78endif
79
80" ----------------------------------------
81" Load plugins and automatically run `:PackerCompile` whenever plugins.lua is updated.
82" ----------------------------------------
83lua require('plugins')
84augroup packer_user_config
85 autocmd!
86 autocmd BufWritePost plugins.lua source <afile> | PackerCompile
87augroup end
88
89" ----------------------------------------
90" indent_guides setting.
91" ----------------------------------------
92let g:indent_guides_enable_on_vim_startup = 1
93let g:indent_guides_start_level = 2
94let g:indent_guides_guide_size = 1
95
96" ----------------------------------------
97" fern.vim setting.
98" ----------------------------------------
99nnoremap <Leader>o :Fern . -drawer -reveal=% -width=30 -toggle<CR>
100let g:fern#default_hidden = 1
書き換え後のinit.lua
1-- ----------------------------------------
2-- Key bind and other setting.
3-- ----------------------------------------
4vim.opt.encoding = "utf-8" -- Prevent garbled characters
5vim.opt.fileencoding = "utf-8" -- Setting for handling multi byte characters
6vim.scriptencoding = "utf-8" -- Setting for handling multi byte characters
7vim.opt.number = true -- Add row number
8vim.opt.title = true -- Add a filename to each tabs
9vim.opt.cursorline = true -- Add cursor line
10vim.opt.tabstop = 4 -- Insert spaces when the tab key is pressed
11vim.opt.shiftwidth = 4 -- Change the number of spaces inserted for indentation
12-- vim.opt.softtabstop = 4 -- Make spaces feel like real tabs
13vim.opt.expandtab = true -- Convert tabs to spaces
14vim.opt.smartindent = true -- Add a new line with autoindent
15vim.opt.colorcolumn = "120" -- Add a color on 80'th column
16vim.opt.hlsearch = true -- Highlight searched characters
17vim.opt.incsearch = true -- Highlight when inputting chars
18vim.opt.wildmenu = true -- Show completion suggestions at command line mode
19vim.opt.conceallevel = 0 -- Show double quotations in json file and so on.
20vim.g.mapleader = " " -- Set a space key to a leader.
21vim.opt.mouse = "" -- Don't use a mouse.
22vim.opt.signcolumn = "yes" -- Always show signcolumn to prevent rattling.
23
24-- ----------------------------------------
25-- Remove unnecessary spaces at the end of line.
26-- ----------------------------------------
27vim.api.nvim_create_augroup("auto_remove_unnecessary_spaces_at_the_end_of_line", { clear = true })
28vim.api.nvim_create_autocmd("BufWritePre", {
29 group = "auto_remove_unnecessary_spaces_at_the_end_of_line",
30 pattern = "*",
31 command = [[%s/\s\+$//e]]
32})
33
34-- ----------------------------------------
35-- Copy to the system clipboard.
36-- ----------------------------------------
37if vim.fn.has('clipboard') == 1 then
38 vim.opt.clipboard = "unnamed"
39end
40
41-- ----------------------------------------
42-- Remember a history of undo/redo.
43-- ----------------------------------------
44if vim.fn.has('persistent_undo') == 1 then
45 local undo_path = vim.fn.expand('~/.vim/undo/')
46 vim.cmd('set undodir=' .. undo_path)
47 vim.opt.undofile = true
48end
49
50-- ----------------------------------------
51-- Settings for indent each files.
52-- ----------------------------------------
53vim.api.nvim_create_augroup("html_css_js_and_others_indent", { clear = true })
54vim.api.nvim_create_autocmd({"BufNewFile", "BufRead"}, {
55 group = "html_css_js_and_others_indent",
56 pattern = { "*.yml", "*.yaml", "*.tmpl", "*json" },
57 command = "set tabstop=2 shiftwidth=2"
58})
59vim.api.nvim_create_autocmd({"BufNewFile", "BufRead"}, {
60 group = "html_css_js_and_others_indent",
61 pattern = { "*.html", "*.css", "*.js", "*.ts", "*.php" },
62 command = "set tabstop=2 shiftwidth=2"
63})
64vim.api.nvim_create_autocmd({"BufNewFile", "BufRead"}, {
65 group = "html_css_js_and_others_indent",
66 pattern = "*.go",
67 command = "set noexpandtab tabstop=8 shiftwidth=8"
68})
69
70-- ----------------------------------------
71-- Open init.vim and 'source' it.
72-- ----------------------------------------
73vim.api.nvim_set_keymap('n', '<Leader>.', ':vs ~/.config/nvim/init.lua<CR>', { noremap = true, silent = true })
74vim.api.nvim_set_keymap('n', '<Leader>s', ':source ~/.config/nvim/init.lua<CR>', { noremap = true, silent = true })
75
76-- ----------------------------------------
77-- Clear highlighted characters.
78-- ----------------------------------------
79vim.api.nvim_set_keymap('n', '<C-[><C-[>', ':nohlsearch<CR>', { noremap = true, silent = true })
80
81-- ----------------------------------------
82-- vimshell setting.
83-- ----------------------------------------
84if vim.fn.has('nvim') == 1 then
85 vim.api.nvim_set_keymap('n', '<Leader>-', ':split term://bash<CR>', { noremap = true, silent = true })
86 vim.api.nvim_set_keymap('n', '<Leader>l', ':vsplit term://bash<CR>', { noremap = true, silent = true })
87else
88 vim.api.nvim_set_keymap('n', '<Leader>-', ':below terminal ++close ++rows=13 bash<CR>', { noremap = true, silent = true })
89 vim.api.nvim_set_keymap('n', '<Leader>l', ':vertical terminal ++close bash<CR>', { noremap = true, silent = true })
90end
91
92-- ----------------------------------------
93-- Load plugins and automatically run `:PackerCompile` whenever plugins.lua is updated.
94-- ----------------------------------------
95-- require('plugins')
96-- vim.api.nvim_create_augroup("packer_user_config", { clear = true })
97-- vim.api.nvim_create_autocmd("BufWritePost", {
98-- group = "packer_user_config",
99-- pattern = "plugins.lua",
100-- command = "source <afile> | PackerCompile"
101-- })
102
103-- ----------------------------------------
104-- indent_guides setting.
105-- ----------------------------------------
106vim.g.indent_guides_enable_on_vim_startup = 1
107vim.g.indent_guides_start_level = 2
108vim.g.indent_guides_guide_size = 1
109
110-- ----------------------------------------
111-- fern.vim setting.
112-- ----------------------------------------
113vim.api.nvim_set_keymap('n', '<Leader>o', ':Fern . -drawer -reveal=% -width=30 -toggle<CR>', { noremap = true, silent = true })
114vim.api.nvim_set_var('fern#default_hidden', 1)
115
116-- ----------------------------------------
117-- lazy.nvim setting.
118-- ----------------------------------------
119require('lazy_nvim')
まとめ
書き換え後も恙無く、快適にNeovimを使えています。