0
0

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 1 year has passed since last update.

nvimが(引数で指定しなかった場合に)デフォルトで開くファイル・ディレクトリを設定する

Posted at

日記や作業中のメモを取るのにneovimを使っているのだが、

  • いちいちメモ帳のフォルダのパスを指定するのが面倒に感じてきた
  • nvim-qtなどのGUIアプリの場合そもそも引数の指定をしないで開くのが自然
    開いてから改めてパスを指定する必要があって二度手間1
  • 引数にパスを指定しない場合のデフォルトの空バッファは自分は使っていない

と言うことで、空バッファの代わりにメモ用のリポジトリを開くようにしたい。

やること

  • neovimでパスを指定しない際に空バッファの代わりにメモ用のディレクトリ(もしくはファイル)を開くようにする

結論

引数を指定しない場合、(:ls で取得できる)バッファは無名の1つだけなので、これを判定する。
具体的には
len(getbufinfo({'buflisted':1}))でバッファの数を
getbufinfo({'buflisted':1})[0].nameでアクティブなバッファの名前を取得できる。(vim scriptの場合)

自分はinit.luaで設定しているので、luaに直して条件分岐など追加したのが以下。

~/.config/nvim/init.lua
local default_dir = "path/to/default"
local bufinfoargs = {}
bufinfoargs['buflisted'] = 1
local bufinfo = vim.fn.getbufinfo(bufinfoargs)

if (#bufinfo == 1 and bufinfo[1]["name"] == '') then
  vim.cmd("cd " .. default_dir)
  vim.cmd("e " .. default_dir)
end

今日の日記など、日別のファイルを開くのであればこのような感じ

~/.config/nvim/init.lua
if (#bufinfo == 1 and bufinfo[1]["name"] == '') then
  vim.cmd("cd " .. default_dir)
  vim.cmd("e " .. default_dir .. '/'.. os.date('%Y/%m/%d') .. '.md')
end

参考

  1. 言うてGUIアプリはWindowsでしか使わないが。LinuxやMacだとGuakeやiTermのショートカットキーで開けるターミナルの方がメモ用には都合がいいので。

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?