NeoVim markdown-preview.nvim 插件 v0.0.10 配置:macOS 下 3 步实现 iTerm2 与 Chrome 自动分屏
NeoVim markdown-preview.nvim 插件 v0.0.10 配置macOS 下 3 步实现 iTerm2 与 Chrome 自动分屏在 macOS 环境下使用 NeoVim 编写 Markdown 文档时实时预览功能能极大提升写作效率。本文将介绍如何通过 markdown-preview.nvim 插件配合 AppleScript 脚本实现 iTerm2 终端与 Chrome 浏览器的自动分屏布局打造无缝的键盘流写作体验。1. 环境准备与插件安装首先确保系统满足以下基础条件macOS 10.15 或更高版本NeoVim 0.7.0iTerm2 3.4.0Google Chrome 或 Edge 浏览器推荐使用 lazy.nvim 作为插件管理器在~/.config/nvim/lua/plugins/markdown.lua中添加以下配置return { iamcco/markdown-preview.nvim, cmd { MarkdownPreviewToggle, MarkdownPreview, MarkdownPreviewStop }, ft { markdown }, build function() vim.fn[mkdp#util#install]() end, config function() vim.g.mkdp_auto_close 0 -- 保持预览窗口开启 vim.g.mkdp_refresh_slow 1 -- 保存时刷新而非实时刷新 end }关键配置参数说明参数类型默认值作用mkdp_auto_close布尔1切换缓冲区时自动关闭预览mkdp_refresh_slow布尔0延迟刷新模式(保存时刷新)mkdp_browser字符串指定浏览器路径安装完成后执行:Lazy install等待构建完成可通过:checkhealth markdown-preview验证安装状态。2. AppleScript 窗口管理脚本在~/.config/nvim/scripts/md_split.applescript创建自动化脚本# 获取主显示器分辨率 tell application Finder to set {screenWidth, screenHeight} to bounds of window of desktop # 计算半屏尺寸考虑菜单栏24px高度 set halfWidth to screenWidth / 2 set usableHeight to screenHeight - 24 # 退出iTerm全屏模式如果处于全屏 tell application iTerm if exists window 1 then set isFullscreen to (get is fullscreen of current session of current window) if isFullscreen then tell application System Events to tell process iTerm click menu item Toggle Full Screen of menu View of menu bar 1 end tell delay 0.5 end if end if end tell # 配置Chrome窗口右侧 tell application Google Chrome make new window set bounds of front window to {halfWidth, 24, screenWidth, screenHeight} activate end tell # 配置iTerm窗口左侧 tell application iTerm set bounds of front window to {0, 24, halfWidth, screenHeight} activate end tell脚本调优建议不同显示器需调整screenWidth和screenHeight参数若使用带刘海的 MacBook需增加顶部边距建议设为32延迟参数delay可根据机器性能调整0.3-1秒3. NeoVim 快捷键集成在~/.config/nvim/lua/config/keymaps.lua中添加快捷键映射local function get_script_path() return vim.fn.expand(~/.config/nvim/scripts/md_split.applescript) end vim.keymap.set(n, leadermp, function() local script_path get_script_path() vim.fn.system(osascript .. vim.fn.shellescape(script_path)) vim.cmd(MarkdownPreview) end, { desc Markdown分屏预览 })进阶配置技巧使用silent !避免命令输出干扰添加错误处理确保脚本执行成功支持多显示器配置需修改AppleScript4. 工作流优化与问题排查分辨率适配方案创建不同显示器的预设配置# 4K显示器预设 if screenWidth 3000 then set halfWidth to 1920 set screenWidth to 3840 # 笔记本预设 else set halfWidth to screenWidth / 2 end if常见问题解决窗口位置异常检查系统「显示器」设置中的排列方式确保没有开启「缩放分辨率」快捷键冲突-- 检查现有映射 :verbose map leadermp浏览器未启动-- 添加备用浏览器检测 if application Google Chrome is not running then activate application Google Chrome delay 2 end if性能优化建议禁用不需要的 markdown-preview 功能vim.g.mkdp_markdown_css vim.g.mkdp_highlight_css 对于大型文档设置刷新延迟vim.g.mkdp_refresh_slow 1这套方案将传统 Markdown 写作中的多步操作简化为单次快捷键触发实现了真正的键盘中心化工作流。实际使用中可根据个人偏好调整窗口比例和快捷键映射建议搭配 Vim 的 Markdown 语法高亮和折叠功能使用效果更佳。