Skip to content

Instantly share code, notes, and snippets.

@eduardoarandah
Created November 12, 2025 18:29
Show Gist options
  • Save eduardoarandah/4925bcbfec40081666af633e82b01ef6 to your computer and use it in GitHub Desktop.
Save eduardoarandah/4925bcbfec40081666af633e82b01ef6 to your computer and use it in GitHub Desktop.
Command to dictate in Neovim with hear
-- Command to dictate in Neovim with hear
-- Usage: :Hear
-- https://github.com/sveinbjornt/hear
vim.api.nvim_create_user_command("Hear", function()
-- Save current buffer and cursor position
local original_buf = vim.api.nvim_get_current_buf()
local original_win = vim.api.nvim_get_current_win()
local cursor_pos = vim.api.nvim_win_get_cursor(original_win)
-- Create temporary buffer for terminal
local buf = vim.api.nvim_create_buf(false, true)
-- Calculate floating window dimensions (30% width, 20% height)
local width = math.floor(vim.o.columns * 0.8)
local height = math.floor(vim.o.lines * 0.2)
local row = math.floor((vim.o.lines - height) / 2)
local col = math.floor((vim.o.columns - width) / 2)
-- Floating window configuration
local opts = {
relative = "editor",
width = width,
height = height,
row = row,
col = col,
style = "minimal",
border = "rounded",
title = " Dictation (hear) ",
title_pos = "center",
}
-- Open floating window
local win = vim.api.nvim_open_win(buf, true, opts)
-- Run hear in terminal buffer using vim.fn.jobstart
vim.fn.jobstart("hear --locale=en-US -m -p -t 2", {
term = true,
on_exit = function()
-- Get all content from terminal buffer
local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false)
-- Filter empty lines
lines = vim.tbl_filter(function(line)
return line ~= ""
end, lines)
-- Close floating window
vim.api.nvim_win_close(win, true)
-- Return to original window
vim.api.nvim_set_current_win(original_win)
vim.api.nvim_set_current_buf(original_buf)
-- Insert dictated text at cursor position
if lines then
vim.api.nvim_win_set_cursor(original_win, cursor_pos)
local row = cursor_pos[1]
local col = cursor_pos[2]
-- Insert text
vim.api.nvim_buf_set_text(original_buf, row - 1, col, row - 1, col, lines)
end
end,
})
-- Enter terminal mode automatically
vim.cmd("startinsert")
end, {})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment