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")
endHere'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")
endOr 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()
endPlug '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.gcan be used to get/setg:global variables.- e.g.
nvim.g.variable == g:variable nvim.g.variable = 123ornvim.g.variable = nilto delete the variable:h nvim_get_var:h nvim_set_var:h nvim_del_varfor more
- e.g.
nvim.vcan be used to get/setv:variables.- e.g.
nvim.v.count1 == v:count1 - Useful
v:variables,v:register,v:count1, etc.. nvim.v.variable = 123to set the value (when not read-only).:h nvim_get_vvar:h nvim_set_vvarfor more
- e.g.
nvim.bcan 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 = 123ornvim.b.variable = nilto delete the variable:h nvim_buf_get_var:h nvim_buf_set_var:h nvim_buf_del_varfor more
- Can use
nvim.wcan 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 = 123ornvim.w.variable = nilto delete the variable:h nvim_win_get_var:h nvim_win_set_var:h nvim_win_del_varfor more
- Can use
- If you're interested in variables set by
set/setlocal, then you wantnvim.wo.
nvim.envcan be used to get/set environment variables.- e.g.
nvim.env.PWD == $PWD nvim.env.TEST = 123to set the value. Equivalent tolet $TEST = 123.:h setreg:h setregfor more. These aren't API functions.
- e.g.
nvim.ocan be used to get/set global options, as in:h optionswhich are set throughset.- e.g.
nvim.o.shiftwidth == &shiftwidth nvim.o.shiftwidth = 8is equivalent toset shiftwidth=8orlet &shiftwidth = 8:h nvim_get_option:h nvim_set_optionfor more.
- e.g.
nvim.bocan be used to get/set buffer options, as in:h optionswhich 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 = 8is equivalent tosetlocal shiftwidth=8:h nvim_buf_get_option:h nvim_buf_set_optionfor more.
- Can use
nvim.wocan be used to get/set window options, as in:h optionswhich 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 = 4is equivalent tosetlocal foldlevel=4nvim.wo.variable = nilto delete the variable. (only works if there's a global fallback):h nvim_win_get_option:h nvim_win_set_optionfor more.
- Can use