Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to autosave when leave insert #1673

Open
wideweide opened this issue Feb 21, 2025 · 0 comments
Open

how to autosave when leave insert #1673

wideweide opened this issue Feb 21, 2025 · 0 comments

Comments

@wideweide
Copy link

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant