Show HN:Hardtime.nvim——克服不良习惯并掌握Vim操作
Show HN: Hardtime.nvim – break bad habits and master Vim motions

原始链接: https://github.com/m4xshen/hardtime.nvim

Hardtime.nvim 是一个 Vim 插件,旨在改善你的导航习惯并改掉不良倾向。它通过阻止或提示较差的按键操作(hjkl、方向键、鼠标)以及短时间内重复按键来鼓励高效的移动。相反,它提倡使用相对跳转、单词移动、f/t 命令、操作符+移动以及括号导航。 该插件会提供你最常见不良习惯的报告。它可配置,选项包括禁用特定按键/文件类型、设置重复按键的时间限制以及自定义提示信息。你可以使用命令启用/禁用它,并自定义提示的显示方式,可以选择内联显示或通过通知显示。Hardtime 还可以强制在指定空闲时间后退出插入模式。你可以通过向 `setup()` 传递一个配置表来设置 Hardtime。

Hacker News 上的讨论总结如下: 一个新的 Neovim 插件 Hardtime.nvim 旨在帮助用户改掉坏习惯并掌握 Vim 的移动命令。讨论重点在于高效的 Vim 移动命令(例如 `f`、`t`、`;`、`/`)与重复按键(例如 `jjjj` 或 `llll`)之间的争论。一些用户发现后者更直观,而另一些用户则提倡学习更高级的技术。 几位评论者分享了他们首选的导航方法,包括使用相对行号、搜索命令以及 easymotion/flash.nvim/vim-sneak 等插件。讨论还涉及到为了获得其现代工具和社区支持而转向 Neovim 的权衡取舍,一些用户发现过渡具有挑战性。也有人提到了一个类似的服务 vimgolf.ai。总的来说,这个插件引发了关于提高 Vim 效率以及为个人用户找到最有效工作流程的讨论。

原文
  • Block repeated keys within a short period of time
  • Provide hints for faster Vim motion
  • Get report of your most common bad habits

👍🏻 Recommended workflow

Instead of only relying on hjkl, arrow keys and mouse, you should:

  1. Use relative jump (eg: 5j 12-) for vertical movement within the screen.
  2. Use CTRL-U CTRL-D CTRL-B CTRL-F gg G for vertical movement outside the screen.
  3. Use word-motion (w W b B e E ge gE) for short-distance horizontal movement.
  4. Use f F t T , ; 0 ^ $ for medium to long-distance horizontal movement.
  5. Use operator + motion/text-object (eg: ci{ y5j dap) whenever possible.
  6. Use % and square bracket commands (see :h [) to jump between brackets.

Learn more in this blog post

  1. Install via your favorite package manager.
{
   "m4xshen/hardtime.nvim",
   lazy = false,
   dependencies = { "MunifTanjim/nui.nvim" },
   opts = {},
},
  1. Setup the plugin in your init.lua. This step is not needed with lazy.nvim if opts is set as above.
require("hardtime").setup()

If you want to see the hint messages in insert and visual mode, set the 'showmode' to false.

But if you want to see both the hint message and current mode you can setup with one of the following methods:

  • Display the mode on status line and set 'showmode' to false. You can do this with some statusline plugin such as lualine.nvim.
  • Set the 'cmdheight' to 2 so that the hint message won't be replaced by mode message.
  • Use nvim-notify to display hint messages on the right top corner instead of commandline.

Hardtime is enabled by default. You can change its state with the following commands:

  • :Hardtime enable enable Hardtime
  • :Hardtime disable disable Hardtime
  • :Hardtime toggle toggle Hardtime

You can view the most frequently seen hints with :Hardtime report.

Your log file is at ~/.local/state/nvim/hardtime.nvim.log.

You can pass your config table into the setup() function or opts if you use lazy.nvim.

If the option is a table (key = value pair), you can set value to false to disable the default value.

Examples:

disabled_keys = {
   ["<Up>"] = false, -- Allow <Up> key
   ["<Space>"] = { "n", "x" }, -- Disable <Space> key in normal and visual mode
},
disabled_filetypes = { 
   lazy = false, -- Enable Hardtime in lazy filetype
   ["dapui*"] = false, -- Enable Hardtime in filetype starting with dapui
},
Option Name Type Default Value Meaning
max_time number 1000 Maximum time (in milliseconds) to consider key presses as repeated.
max_count number 3 Maximum count of repeated key presses allowed within the max_time period.
disable_mouse boolean true Disable mouse support.
hint boolean true Enable hint messages for better commands.
notification boolean true Enable notification messages for restricted and disabled keys.
timeout number or boolean 3000 Time to show notification in milliseconds, set to false to disable timeout.
allow_different_key boolean true Allow different keys to reset the count.
enabled boolean true Whether the plugin is enabled by default or not.
resetting_keys table See Config Keys in what modes that reset the count.
restricted_keys table See Config Keys in what modes triggering the count mechanism.
restriction_mode string ("block" or "hint") "block" The behavior when restricted_keys trigger count mechanism.
disabled_keys table See Config Keys in what modes are disabled.
disabled_filetypes table See Config Hardtime is disabled under these filetypes.
hints table See Config key is a string pattern you want to match, value is a table of hint message and pattern length. Learn more about Lua string pattern.
callback function(text) vim.notify callback function can be used to override the default notification behavior.
force_exit_insert_mode boolean false Enable forcing exit Insert mode if user is inactive in Insert mode.
max_insert_idle_ms number 5000 Maximum amount of idle time, in milliseconds, allowed in Insert mode.
ui table of strings/table pair See Config An option to customize the popup for the Hardtime report.

These are two default hints:

hints = {
   ["k%^"] = {
      message = function()
         return "Use - instead of k^" -- return the hint message you want to display
      end,
      length = 2, -- the length of actual key strokes that matches this pattern
   },
   ["d[tTfF].i"] = { -- this matches d + {t/T/f/F} + {any character} + i
      message = function(keys) -- keys is a string of key strokes that matches the pattern
         return "Use " .. "c" .. keys:sub(2, 3) .. " instead of " .. keys
         -- example: Use ct( instead of dt(i
      end,
      length = 4,
   },
}

Check out some examples of custom hint in discussion!

Please read CONTRIBUTING.md.

联系我们 contact @ memedata.com