Home

Awesome

nvim.lua

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.

Why use this module?

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

Installation

Plug 'norcalli/nvim.lua'

Usage

local nvim = require 'nvim'

API

API Function and Command Shortcuts

All of these methods cache the inital lookup in the metatable, but there is a small overhead regardless.

nvim.$method

nvim.$method(...) redirects to vim.api.nvim_$method(...)

e.g. nvim.command(...) == vim.api.nvim_command(...).

This is mostly for laziness.

nvim.fn.$method

nvim.fn.$method(...) redirects to vim.api.nvim_call_function($method, {...})

e.g. nvim.fn.expand("%:h") or nvim.fn.has("terminal")

nvim.ex.$method

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>")

Variable shortcuts

nvim.g

nvim.v

nvim.b

nvim.w

nvim.env

nvim.o

nvim.bo

nvim.wo