firenvim icon indicating copy to clipboard operation
firenvim copied to clipboard

how to autosave when leave insert

Open wideweide opened this issue 10 months ago • 0 comments

I use lua like down, when press esc ,show *** written, but not sync to textarea when use hotkey to 'SubmitForm' ,the vim.cmd('w') is work well ,vim.cmd('write') not

I'm not sure it is a bug,or just my config has some error...

Operating System: Pop!_OS 22.04 LTS
Kernel: Linux 6.9.3-76060903-generic Architecture: x86-64 firefox 135.0.1 nvm --version 0.40.1

-- Firenvim 特定配置
if vim.g.started_by_firenvim then
  -- 在 Firenvim 中使用的字体
  vim.o.guifont = "JetBrainsMono Nerd Font:h18"
  
  -- 自动保存设置
  vim.opt.autowrite = true
  
  -- 退出插入模式时自动保存
  vim.api.nvim_create_autocmd("InsertLeave", {
    callback = function()
      vim.cmd('write')
      vim.cmd('w')
    end,
  })

 vim.api.nvim_create_autocmd("UIEnter", {
  callback = function()
    vim.fn.timer_start(1000, function()
      if vim.api.nvim_get_option('lines') < 10 then
        vim.opt.lines = 10
      end
      vim.cmd('startinsert')
    end)
  end,
}) 


  -- 在 init.lua 中定义一个新的命令
  vim.api.nvim_create_user_command('SubmitForm', function()
    -- 稍作延迟确保内容已经更新到 textarea
    vim.defer_fn(function()
      -- 保存当前缓冲区内容
      vim.cmd('w')  -- 保存内容
      vim.fn['firenvim#press_keys']("<CR>")
      -- 清空当前缓冲区的所有内容
      vim.cmd('1,$d')
    end, 500)  -- 延迟500毫秒,确保内容已更新
  end, {})

  -- 设置快捷键 Ctrl+Enter 触发命令
  vim.api.nvim_set_keymap('n', '<C-CR>', ':SubmitForm<CR>', { noremap = true, silent = true }) -- 普通模式
  vim.api.nvim_set_keymap('i', '<C-CR>', '<Esc>:SubmitForm<CR>', { noremap = true, silent = true }) -- 插入模式

end

wideweide avatar Feb 21 '25 01:02 wideweide