Skip to content

Commit 8d9e993

Browse files
committed
feat: make vim.cmd use nvim_cmd for table argument
1 parent cf68f0a commit 8d9e993

File tree

2 files changed

+59
-18
lines changed

2 files changed

+59
-18
lines changed

runtime/doc/lua.txt

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,21 +1017,10 @@ vim.call({func}, {...}) *vim.call()*
10171017
See also |vim.fn|.
10181018
Equivalent to: >
10191019
vim.fn[func]({...})
1020-
<
1021-
vim.cmd({cmd}) *vim.cmd()*
1022-
Executes multiple lines of Vimscript at once. It is an alias to
1023-
|nvim_exec()|, where `output` is set to false. Thus it works identical
1024-
to |:source|.
1025-
See also |ex-cmd-index|.
1026-
Example: >
1027-
vim.cmd('echo 42')
1028-
vim.cmd([[
1029-
augroup My_group
1030-
autocmd!
1031-
autocmd FileType c setlocal cindent
1032-
augroup END
1033-
]])
1034-
<
1020+
1021+
vim.cmd({command})
1022+
See |vim.cmd()|.
1023+
10351024
vim.fn.{func}({...}) *vim.fn*
10361025
Invokes |vim-function| or |user-function| {func} with arguments {...}.
10371026
To call autoload functions, use the syntax: >
@@ -1296,6 +1285,34 @@ vim.wo *vim.wo*
12961285
==============================================================================
12971286
Lua module: vim *lua-vim*
12981287

1288+
cmd({command}) *vim.cmd()*
1289+
Execute Vim script commands.
1290+
1291+
Example: >
1292+
1293+
vim.cmd('echo 42')
1294+
vim.cmd([[
1295+
augroup My_group
1296+
autocmd!
1297+
autocmd FileType c setlocal cindent
1298+
augroup END
1299+
]])
1300+
vim.cmd({ cmd = 'echo', args = { '"foo"' } })
1301+
<
1302+
1303+
Parameters: ~
1304+
{command} string|table Command(s) to execute. If a
1305+
string, executes multiple lines of Vim script
1306+
at once. In this case, it is an alias to
1307+
|nvim_exec()|, where `output` is set to false.
1308+
Thus it works identical to |:source|. If a
1309+
table, executes a single command. In this case,
1310+
it is an alias to |nvim_cmd()| where `opts` is
1311+
empty.
1312+
1313+
See also: ~
1314+
|ex-cmd-index|
1315+
12991316
*vim.connection_failure_errmsg()*
13001317
connection_failure_errmsg({consequence})
13011318
TODO: Documentation

runtime/lua/vim/_editor.lua

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,33 @@ vim.funcref = function(viml_func_name)
284284
return vim.fn[viml_func_name]
285285
end
286286

287-
-- An easier alias for commands.
288-
vim.cmd = function(command)
289-
return vim.api.nvim_exec(command, false)
287+
--- Execute Vim script commands.
288+
---
289+
--- Example:
290+
--- <pre>
291+
--- vim.cmd('echo 42')
292+
--- vim.cmd([[
293+
--- augroup My_group
294+
--- autocmd!
295+
--- autocmd FileType c setlocal cindent
296+
--- augroup END
297+
--- ]])
298+
--- vim.cmd({ cmd = 'echo', args = { '"foo"' } })
299+
--- </pre>
300+
---
301+
---@param command string|table Command(s) to execute.
302+
--- If a string, executes multiple lines of Vim script at once. In this
303+
--- case, it is an alias to |nvim_exec()|, where `output` is set to
304+
--- false. Thus it works identical to |:source|.
305+
--- If a table, executes a single command. In this case, it is an alias
306+
--- to |nvim_cmd()| where `opts` is empty.
307+
---@see |ex-cmd-index|
308+
function vim.cmd(command)
309+
if type(command) == 'table' then
310+
return vim.api.nvim_cmd(command, {})
311+
else
312+
return vim.api.nvim_exec(command, false)
313+
end
290314
end
291315

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

0 commit comments

Comments
 (0)