nvim
is an object which contains shortcut/magic methods that are very useful for mappings.
Fun fact: nvim.fn/env/v/g/o/wo/bo were merged into master as of November 2019.
Here's an excerpt of functionality from my init.lua
. Without nvim.lua
, it looks like this:
-- Save and close the current buffer instead of all of VIM.
-- But if it's the last buffer, then save and close vim.
if #vim.api.nvim_list_bufs() > 1 then
if not vim.api.nvim_buf_get_option(vim.api.nvim_get_current_buf(), "modifiable") then
vim.api.nvim_command("bd")
else
vim.api.nvim_command("w | bd")
end
else
vim.api.nvim_command("xit")
end
Here's what it looks like with nvim.lua
:
if #nvim.list_bufs() > 1 then
if not nvim.bo.modifiable then
nvim.command("bd")
else
nvim.command("w | bd")
end
else
nvim.command("xit")
end
Or if you're extreme:
if #nvim.list_bufs() > 1 then
if not nvim.bo.modifiable then
nvim.ex.bd()
else
nvim.command("w | bd")
end
else
nvim.ex.xit()
end
Plug 'norcalli/nvim.lua'
local nvim = require 'nvim'
All of these methods cache the inital lookup in the metatable, but there is a small overhead regardless.
nvim.$method(...)
redirects to vim.api.nvim_$method(...)
e.g. nvim.command(...) == vim.api.nvim_command(...)
.
This is mostly for laziness.
nvim.fn.$method(...)
redirects to vim.api.nvim_call_function($method, {...})
e.g. nvim.fn.expand("%:h")
or nvim.fn.has("terminal")
nvim.ex.$command(...)
is approximately :$command flatten({...}).join(" ")
e.g. nvim.ex.edit("term://$SHELL")
or nvim.ex.startinsert()
NOTE: Since !
isn't a valid identifier character, you can use _
at the end to indicate a !
e.g. nvim.ex.nnoremap_("x", "<Cmd>echo hi<CR>")
nvim.g
can be used to get/setg:
global variables.- e.g.
nvim.g.variable == g:variable
nvim.g.variable = 123
ornvim.g.variable = nil
to delete the variable:h nvim_get_var
:h nvim_set_var
:h nvim_del_var
for more
- e.g.
nvim.v
can be used to get/setv:
variables.- e.g.
nvim.v.count1 == v:count1
- Useful
v:
variables,v:register
,v:count1
, etc.. nvim.v.variable = 123
to set the value (when not read-only).:h nvim_get_vvar
:h nvim_set_vvar
for more
- e.g.
nvim.b
can be used to get/setb:
buffer variables for the current buffer.- Can use
nvim.b[bufnr]
to target a different buffer than the current one.- e.g.
nvim.b[bufnr].x = '123'
- e.g.
- e.g.
nvim.b.variable == b:variable
nvim.b.variable = 123
ornvim.b.variable = nil
to delete the variable:h nvim_buf_get_var
:h nvim_buf_set_var
:h nvim_buf_del_var
for more
- Can use
nvim.w
can be used to get/setw:
buffer variables for the current buffer.- Can use
nvim.w[winnr]
to target a different window than the current one.- e.g.
nvim.w[winnr].x = '123'
- e.g.
- e.g.
nvim.w.variable == w:variable
nvim.w.variable = 123
ornvim.w.variable = nil
to delete the variable:h nvim_win_get_var
:h nvim_win_set_var
:h nvim_win_del_var
for more
- Can use
- If you're interested in variables set by
set/setlocal
, then you wantnvim.wo
.
nvim.env
can be used to get/set environment variables.- e.g.
nvim.env.PWD == $PWD
nvim.env.TEST = 123
to set the value. Equivalent tolet $TEST = 123
.:h setreg
:h setreg
for more. These aren't API functions.
- e.g.
nvim.o
can be used to get/set global options, as in:h options
which are set throughset
.- e.g.
nvim.o.shiftwidth == &shiftwidth
nvim.o.shiftwidth = 8
is equivalent toset shiftwidth=8
orlet &shiftwidth = 8
:h nvim_get_option
:h nvim_set_option
for more.
- e.g.
nvim.bo
can be used to get/set buffer options, as in:h options
which are set throughsetlocal
.- Can use
nvim.bo[bufnr]
to target a different buffer than the current one.- e.g.
nvim.bo[bufnr].ft = 'markdown'
- e.g.
- Only for the current buffer.
- e.g.
nvim.bo.shiftwidth == &shiftwidth
nvim.bo.shiftwidth = 8
is equivalent tosetlocal shiftwidth=8
:h nvim_buf_get_option
:h nvim_buf_set_option
for more.
- Can use
nvim.wo
can be used to get/set window options, as in:h options
which are set throughset/setlocal
.- Can use
nvim.wo[winnr]
to target a different window than the current one.- e.g.
nvim.wo[winnr].wrap = false
- e.g.
- These are the valid options as of writing:
{ "arabic", "arab", "breakindent", "bri", "breakindentopt", "briopt", "colorcolumn", "cc", "concealcursor", "cocu", "conceallevel", "cole", "cursorbind", "crb", "cursorcolumn", "cuc", "cursorline", "cul", "diff", "fillchars", "fcs", "foldcolumn", "fdc", "foldenable", "fen", "foldexpr", "fde", "foldignore", "fdi", "foldlevel", "fdl", "foldmarker", "fmr", "foldmethod", "fdm", "foldminlines", "fml", "foldnestmax", "fdn", "foldtext", "fdt", "linebreak", "lbr", "list", "listchars", "lcs", "number", "nu", "numberwidth", "nuw", "previewwindow", "pvw", "relativenumber", "rnu", "rightleft", "rl", "rightleftcmd", "rlc", "scroll", "scr", "scrollbind", "scb", "signcolumn", "scl", "spell", "statusline", "stl", "winblend", "winbl", "winhighlight", "winhl", "winfixheight", "wfh", "winfixwidth", "wfw", "wrap" }
- e.g.
nvim.wo.foldlevel == &foldlevel
nvim.wo.foldlevel = 4
is equivalent tosetlocal foldlevel=4
nvim.wo.variable = nil
to delete the variable. (only works if there's a global fallback):h nvim_win_get_option
:h nvim_win_set_option
for more.
- Can use