Skip to main content
20 votes
Accepted

How to remove Neovim trailing white space?

This should do the trick: vim.api.nvim_create_autocmd({ "BufWritePre" }, { pattern = { "*" }, command = [[%s/\s\+$//e]], }) Be sure to read the h:nvim_create_autocmd() for ...
InsertSON's user avatar
  • 396
15 votes
Accepted

How can I change the root of my nvim-tree?

You can use the command Ctrl+], it will do it.
samuelnihoul's user avatar
10 votes
Accepted

Are vim.cmd() and vim.api.nvim_command() the same, if not, what are the differences?

vim.cmd(command) calls either of these two lua APIs: vim.api.nvim_cmd: when command is a table vim.api.nvim_exec: when command is a string The lua code for vim.cmd: vim.cmd = setmetatable({}, { ...
3N4N's user avatar
  • 5,774
9 votes
Accepted

How can I get the current cursor position in Lua?

You get a hashed value because nvim_win_get_cursor() returns a tuple (row, col). You can show the value with: print(unpack(lineNum)) You can also get the row and column in two variables with this: ...
statox's user avatar
  • 50.1k
8 votes
Accepted

How to change editor mode from lua (or viml)

After some conversation with the OP, we settled on :startinsert/:stopinsert/vim.cmd('startinsert') is probably as close as it gets
8 votes
Accepted

How do I port a filetype command to neovim's lua format?

How can I port filetype plugin indent on? You don't. This is the default (in Neovim). From :he nvim-defaults, Syntax highlighting is enabled by default :filetype plugin indent on is enabled by ...
Matt's user avatar
  • 20.9k
8 votes

How to remove Neovim trailing white space?

I agree with the accepted answer. Something that begins to annoy quickly is that the cursor position is reset to the beginning of the line. To keep it, one can use getpos/setpos like this: vim.api....
BillGatesPriv's user avatar
7 votes
Accepted

How to check if an external command exists in Lua

The answer below assumes you are running neovim and not vim. :h vim.cmd() lets you execute a vimscript command from lua. It takes the command as a string. So you can wrap any vimscript in a string and ...
3N4N's user avatar
  • 5,774
6 votes

How to add keymapping for LSP code formatting in visual mode?

I found this question while solving the same problem, but now the function range_formatting() is deprecated. Now should use vim.lsp.formatexpr() or vim.lsp.buf.format() instead. You can define a ...
Fanteria's user avatar
  • 106
6 votes
Accepted

How can I port nmap, map, and imap commands to Lua configuration?

You use vim.api.nvim_set_keymap vim.api.nvim_set_keymap(mode, keys, mapping, options) So to port nmap <silent> <F2> <Plug>(lcn-rename) It would look like vim.api.nvim_set_keymap('n'...
Evan Carroll's user avatar
  • 1,404
6 votes
Accepted

How to inspect a neovim variable?

The = syntax :lua =var In version 0.9, you don't even need to type in lua : :=var will do. The vim.print() syntax Before version 0.9, there was :lua vim.pretty_print(var) Now with 0.9, vim....
cassepipe's user avatar
  • 415
5 votes
Accepted

How to get the Lua version of Neovim?

According to nanotee/nvim-lua-guide, the version of Lua that Neovim embeds is LuaJIT 2.1.0, which maintains compatibility with Lua 5.1. NeoVim is also likely to stick with Lua 5.1. See this entry from ...
filbranden's user avatar
  • 29k
5 votes
Accepted

Why does this mapping of g to gj give "'Undefined variable gj"?

I think you're using expr_opts which has expr = true. That means to create an <expr> mapping, like (ignoring visual and select modes) :nnoremap <expr> j gj which doesn't work either. Use ...
D. Ben Knoble's user avatar
  • 26.6k
4 votes

Some migration problems from vimrc to init.lua

Neovim API is neither complete nor stable, and so, IMO and contrary to what other people may suggest, there's no good point (at least for now) in accepting Lua as primary configuration language. ...
Matt's user avatar
  • 20.9k
4 votes

How to add `~/.config/nvim' into runtime path for Lua?

Removing the following lines from my init.lua vim.cmd [[set runtimepath=$VIMRUNTIME]] vim.cmd [[set packpath=~/.local/share/nvim/site]] and placing all my .lua modules (except init.lua) into ~/....
Miki Vanoušek's user avatar
4 votes

Is the goal of Lua in neovim to replace Ex, and if not what is it?

The point of Lua support in NeoVim is not really to replace Ex commands altogether. That wouldn't really be possible (consider commands like :e or :w or :s etc.) or productive. Instead, the point of ...
filbranden's user avatar
  • 29k
4 votes
Accepted

Get byte length of current line in Lua

You can use the vim function col: vim.fn.col('$')-1
Mass's user avatar
  • 14.2k
4 votes
Accepted

How can I setup a nerd font in init.lua?

I would do: vim.opt.guifont='CascadiaCode' But If you want the Nerd version of it you have to install it and do: vim.opt.guifont='CaskaydiaCove NFM' Remark: Because 'Cascadia` is a Microsoft ...
Vivian De Smedt's user avatar
4 votes

How do you pass options to a lua plugin?

require'nvim-gpt'.setup { no_default_keymaps = true, } is the correct way. It is not dependent on the plugin manager. (my mistake was elsewhere, calling this setup function accidentally from two ...
TamaMcGlinn's user avatar
3 votes

How to add keymapping for LSP code formatting in visual mode?

It's sufficient just to do vim.keymap.set("v", "<leader>qf", vim.lsp.buf.format, { remap = false }) From :h vim.lsp.buf.format(), range (table|nil) Range to format. Table ...
wardw's user avatar
  • 250
3 votes
Accepted

See live word count in Lualine

The answer is https://github.com/nvim-lualine/lualine.nvim/issues/328. Found with help from @statox. add local function getWords() return tostring(vim.fn.wordcount().words) end to whichever lua ...
muppi090909's user avatar
3 votes

How can I switch to the `[No Name]` buffer

an API to return all (loaded) buffers :h getbufinfo() loop through each of them :h filter() if the buffer is a No Name-buffer :echo empty(bufname(N)) :echo empty(getbufinfo(N)[0].name) an API to ...
Matt's user avatar
  • 20.9k
3 votes
Accepted

Neovim Lua: Error while calling lua chunk [...] '' expected near 'local'

In my case this was because I had this in my file, return require('packer').startup(function() -- stuff end) And then after that I had let on_attach = function(client, bufnr) ... end And that ...
Evan Carroll's user avatar
  • 1,404
3 votes
Accepted

Using Lua, how can I run a function whenever the cursor moves?

To run anything automatically in Vim/Neovim after an event such as a file save(BufWrite) or your cursor moving(CursorMoved) we can use an autocommand. Here is the Lua implementation for your specific ...
Mimic Mannan's user avatar
3 votes
Accepted

How to move cursor one line down with API in the way like the default 'j'?

As reminded by @D.Ben's comment, the following workaround works for me: vim.cmd('norm! j')
vim.ggyG's user avatar
  • 201
3 votes
Accepted

Is it possible to set a vim.api.nvim_create_autocmd for a FileType (not just a pattern)?

It is possible write is like this in lua, just use the FileType event. (replace your file type, I don´t what is the correct one for terraform) vim.api.nvim_create_autocmd( "FileType", { ...
sinarf's user avatar
  • 46
3 votes
Accepted

Minimap config in lua

The error you are receiving is caused by you attempting to call the setup function of a Vimscript plugin. Remember that Vimscript plugins do not need a setup() function and not all Lua plugins need ...
unrealapex's user avatar
3 votes
Accepted

How can I change the default terminal in Neovim (lua)?

In Lua the syntax is slightly different. I would do: vim.g.terminal_emulator='powershell' Or: vim.api.nvim_set_var('terminal_emulator', 'powershell')
Vivian De Smedt's user avatar
3 votes
Accepted

Neovim lua module not found even though it exist

The solution was to change core.options to [username].core.options, since every require call in Lua looks for the file from the lua/ directory. The reason is not completely clear to me yet. But a ...
me.nkr's user avatar
  • 61
3 votes
Accepted

How can I create command abbreviations (`ca`) with the lua api?

!!Note: this solution only works on Neovim >= v0.10; see @ruuter's answer if you are on an older version of neovim. Command abbreviation is treated as just another keymap mode by nvim_set_keymap. ...
David Goldstein's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible