Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prepend an item to Lua array?

Tags:

lua

As title. I'm working with a neovim config, and the content of this array will be concatenated into a command to execute:

cmd = { 'mono', omnisharp_bin, '--languageserver', '--hostPID', tostring(vim.fn.getpid()) }

like this one but imagine that the 'mono' has not been prepended.

like image 674
Rainning Avatar asked Oct 24 '25 06:10

Rainning


1 Answers

You can use table.insert:

cmd = { omnisharp_bin, '--languageserver', '--hostPID', tostring(vim.fn.getpid()) }
table.insert(cmd, 1, 'mono')
-- cmd is now what you want

You can use either table.insert(mytable, position, value) to insert value at position position and shift all the values after that over, or use table.insert(table, value) to insert a value at the end of the array part.

like image 169
Jasmijn Avatar answered Oct 27 '25 03:10

Jasmijn