How to get current cmdline content in neovim?

Viewed 43

I have an autocmd:

vim.api.nvim_create_autocmd(
        { 'CmdlineChanged' }, {
        callback = function()
            Log("inside")
        end
    })

This "event" is fiered every time a key is pressed inside cmdline. How to get the content of the cmdline?

2 Answers

Here a working example to spam a nvim_cmd.log file...

-- Example: ~/.config/nvim/lua/init.lua
-- Than ~/.config/nvim/init.vim need: lua require('init')
vim.api.nvim_create_autocmd({'CmdlineChanged', 'CmdlineEnter'},
  {callback = function()
  local str,chk = vim.fn.getcmdline():gsub('','')
  if chk > 1 then
    local fh = io.open('nvim_cmd.log', 'a+')
    fh:write(('%s%s\n'):format(vim.fn.getcmdtype(), str))
    fh:flush() 
  end         
  end          
  }
)

...in a very short time the nvim_cmd.log look like this...

:b                                                                                                                                                        
:bn                 
:e                             
:e                         
:e n
:e nv                                                                                                                                                              
:e nvi                                                                                    
:e nvim                                                
:e nvim_                
:e nvim_c                                        
:e nvim_cm       
:e nvim_cmd                                 
:e nvim_cmd.                                                                   
:e nvim_cmd.l
:e nvim_cmd.lo
:e nvim_cmd.log
:e nvim_cmd.log
:e nvim_cmd.lo
:e nvim_cmd.l
:e nvim_cmd.
:e nvim_cmd                                                                       
:e nvim_cm
:e nvim_c
:e nvim_
:e nvim
:e nvi
:e nv
:e n
:e 
:e i
:e in
:e ini
:e init
:e init.
:e init.l
:e init.lu
:e init.lua
:l
:ls
:b
:bp
Related