Hot answers tagged

18 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
  • 376
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
  • 49.4k
9 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
7 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.4k
7 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

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,544
6 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,544
5 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
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
  • 25.4k
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.4k
4 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,236
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
  • 14k
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
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
  • 200
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 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
3 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
  • 28.4k
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,236
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 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
3 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

Avoiding name conflicts in ~/.config/nvim/lua/

Most configs I've seen handle this by putting a my directory inside the lua directory. This would allow you to do: require("my.telescope")
heygarrett's user avatar
2 votes
Accepted

How to find line and column for a n-th byte in a buffer?

You can use the charidx() function to convert the byte index to the character index. For example: chidx = vim.fn.charidx(vim.fn.getline(line), col)
Yegappan Lakshmanan's user avatar
2 votes

How to find line and column for a n-th byte in a buffer?

Use :goto to go to a specific byte. Then can use line() & col() :goto 444 :echo [line('.'), col('.')] For more help see: :h :goto :h line() :h col()
Peter Rincker's user avatar
2 votes

How do I open an existing file in a floating window?

I finally found a solution: In the view() function, in place of the vim.cmd(":edit catalog.txt") line: local file_path = vim.api.nvim_get_runtime_file("myPlugin/lua/catalog.txt", ...
Ludo's user avatar
  • 31
2 votes
Accepted

How to check if a buffer hidden in all tabs?

Buffer is called "hidden" if it's Loaded Not shown in any window :echo bufloaded(N) && win_findbuf(N)->empty() or :echo bufexists(N) && getbufinfo(N)[0].hidden
Matt's user avatar
  • 20.4k
2 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.4k
2 votes

How do I change my language in my init.lua? - neovim

Recent versions of Neovim require an additional boolean parameter to set the language, so, this would be the proper command in init.lua: vim.api.nvim_exec ('language en_US', true)
pcesarperez's user avatar
2 votes
Accepted

How to set keywordprg to call a lua function in neovim?

The 'keywordprg' setting can take an Ex command (starting with :), but when it does, the Ex command needs to be able to take an argument, and you can't pass one to a Lua function using :lua that way. ...
filbranden's user avatar
  • 28.4k
2 votes

Unable to use lua inside a `.vim` file

In order to load the plugins, you have to wrap your Plug part with call plug#begin(<path>) and call plug#end(). So it should look like this for example: call plug#begin('~/.vim/plugged') Plug '...
TornaxO7's user avatar
  • 561

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