Skip to content

Commit

Permalink
feat(lua): vim.cmd() with kwargs acts like nvim_cmd() neovim#18523
Browse files Browse the repository at this point in the history
  • Loading branch information
famiu authored and kraftwerk28 committed Jun 1, 2022
1 parent 4a44d38 commit fdad865
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 18 deletions.
47 changes: 32 additions & 15 deletions runtime/doc/lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1017,21 +1017,10 @@ vim.call({func}, {...}) *vim.call()*
See also |vim.fn|.
Equivalent to: >
vim.fn[func]({...})
<
vim.cmd({cmd}) *vim.cmd()*
Executes multiple lines of Vimscript at once. It is an alias to
|nvim_exec()|, where `output` is set to false. Thus it works identical
to |:source|.
See also |ex-cmd-index|.
Example: >
vim.cmd('echo 42')
vim.cmd([[
augroup My_group
autocmd!
autocmd FileType c setlocal cindent
augroup END
]])
<
vim.cmd({command})
See |vim.cmd()|.

vim.fn.{func}({...}) *vim.fn*
Invokes |vim-function| or |user-function| {func} with arguments {...}.
To call autoload functions, use the syntax: >
Expand Down Expand Up @@ -1296,6 +1285,34 @@ vim.wo *vim.wo*
==============================================================================
Lua module: vim *lua-vim*

cmd({command}) *vim.cmd()*
Execute Vim script commands.

Example: >
vim.cmd('echo 42')
vim.cmd([[
augroup My_group
autocmd!
autocmd FileType c setlocal cindent
augroup END
]])
vim.cmd({ cmd = 'echo', args = { '"foo"' } })
<

Parameters: ~
{command} string|table Command(s) to execute. If a
string, executes multiple lines of Vim script
at once. In this case, it is an alias to
|nvim_exec()|, where `output` is set to false.
Thus it works identical to |:source|. If a
table, executes a single command. In this case,
it is an alias to |nvim_cmd()| where `opts` is
empty.

See also: ~
|ex-cmd-index|

*vim.connection_failure_errmsg()*
connection_failure_errmsg({consequence})
TODO: Documentation
Expand Down
30 changes: 27 additions & 3 deletions runtime/lua/vim/_editor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,33 @@ vim.funcref = function(viml_func_name)
return vim.fn[viml_func_name]
end

-- An easier alias for commands.
vim.cmd = function(command)
return vim.api.nvim_exec(command, false)
--- Execute Vim script commands.
---
--- Example:
--- <pre>
--- vim.cmd('echo 42')
--- vim.cmd([[
--- augroup My_group
--- autocmd!
--- autocmd FileType c setlocal cindent
--- augroup END
--- ]])
--- vim.cmd({ cmd = 'echo', args = { '"foo"' } })
--- </pre>
---
---@param command string|table Command(s) to execute.
--- If a string, executes multiple lines of Vim script at once. In this
--- case, it is an alias to |nvim_exec()|, where `output` is set to
--- false. Thus it works identical to |:source|.
--- If a table, executes a single command. In this case, it is an alias
--- to |nvim_cmd()| where `opts` is empty.
---@see |ex-cmd-index|
function vim.cmd(command)
if type(command) == 'table' then
return vim.api.nvim_cmd(command, {})
else
return vim.api.nvim_exec(command, false)
end
end

-- These are the vim.env/v/g/o/bo/wo variable magic accessors.
Expand Down

0 comments on commit fdad865

Please sign in to comment.