Using vim.bo.filetype in an if statement to set filetype specific keybinds in neovim with init.lua

huangapple go评论51阅读模式
英文:

Using vim.bo.filetype in an if statement to set filetype specific keybinds in neovim with init.lua

问题

我想要一个简单的按键映射,只在使用neovim编辑C文件时才生效
这是我在init.lua文件中的代码
```lua
if vim.bo.filetype == 'c' then
    vim.api.nvim_set_keymap('i', '(', '(  )<left><left>', {noremap = true})
end

我试图使用简单的if语句来判断我所在的文件是否具有'c'文件扩展名,如果是,就'启用'按键映射。
然而,这似乎不起作用。

我正在使用Arch Linux,使用gcc作为C编译器(如果这很重要的话),并且使用neovim v0.9.1。
[我也在if语句之外单独测试了按键映射,它可以工作]

我知道这个解决方案;

local function setup_c_mappings()
  vim.api.nvim_set_keymap('i', '(', '(  )<left><left>', {noremap = true})
end

-- 在编辑C文件时触发setup_c_mappings
vim.cmd([[
  augroup CFileMappings
    autocmd!
    autocmd FileType c,cpp lua setup_c_mappings()
  augroup END
]])

然而,我希望有一种更'简约'/'简单'的方法来实现相同的结果,因此才有了非常基本的if语句。

我所有的研究都让人们说要使用上面的解决方案,但我仍然希望可能还有一种使用if vim.bo.filetype == 'c'的方法。


<details>
<summary>英文:</summary>

I want to have a simple keymap that is only active when editing a C file using neovim.
This is the code in my init.lua file:
```lua
if vim.bo.filetype == &#39;c&#39; then
	vim.api.nvim_set_keymap(&#39;i&#39;, &#39;(&#39;, &#39;(  )&lt;left&gt;&lt;left&gt;&#39;, {noremap = true})
end

I was aiming for a simple if= statement to see if the file im in has the 'c' file extension, and if it does, 'enable' the keymap.
However, this does not seem to work

I am on Arch linux, using gcc as the c compiler (if that matters), and using neovim v0.9.1
[i have also tested the keymap by itself outside of the if statement and it works]

I am aware of this solution;

local function setup_c_mappings()
  vim.api.nvim_set_keymap(&#39;i&#39;, &#39;(&#39;, &#39;(  )&lt;left&gt;&lt;left&gt;&#39;, {noremap = true})
end

-- Trigger setup_c_mappings when editing a C file
vim.cmd([[
  augroup CFileMappings
    autocmd!
    autocmd FileType c,cpp lua setup_c_mappings()
  augroup END
]])

However i was hoping for a more 'minimalistic'/simpler way of achieving the same result, hence the very basic 'if' statement

All of my research has people saying to use the above solution, but i am still hoping there might still be a way to use if vim.bo.filetype == &#39;c&#39; solution.

答案1

得分: 1

通常,你希望将你的特定文件类型设置放在 ftplugin/c.lua 中,然后只需添加一行代码
vim.api.nvim_set_keymap('i', '(', '( )<left><left>', {noremap = true})。请查看 :h load-plugins

你可能还想查看 :h vim.keymap.set

init.lua 只在你启动 Neovim 时运行,但不会在你打开一个缓冲区时运行。你希望每次打开一个 C 文件的缓冲区时都设置键映射,因此需要使用自动命令/ftplugin。

如果坚持使用 if vim.bo.filetype == 'c' 解决方案,你可以创建一个在每次文件类型为 c 时运行函数的自动命令,然后在函数中执行你的逻辑。

请注意,你可以使用 Lua API 创建自动命令(参见 :h api-autocmd)。

英文:

Generally, you want to put your filetype specific settings in ftplugin/c.lua and just put the single line
vim.api.nvim_set_keymap(&#39;i&#39;, &#39;(&#39;, &#39;( )&lt;left&gt;&lt;left&gt;&#39;, {noremap = true}). See :h load-plugins.

You may also want to look into :h vim.keymap.set

init.lua only runs when you start Neovim, but not when you open a buffer. You want to set the keymap every time you open a buffer with c file, hence the need for autocommands/ftplugin.

If you insist on using the if vim.bo.filetype == &#39;c&#39; solution. You can create a autocommand that runs a function on every filetype with BufEnter, then do your logic in the function.

Note that you can create autocommand with lua api (see :h api-autocmd)

huangapple
  • 本文由 发表于 2023年7月3日 16:45:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76603175.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定