Better Windows CMD

Mon, Dec 2, 2024heziah

添加到%AppData%/../local/clink/clink_start.cmd

plain
@echo off
chcp 65001 >nul 2>&1
doskey cp=copy
doskey mv=move
doskey clear=cls
doskey cat=type $1
doskey ls=lsd
doskey l=lsd -l
doskey ll=lsd -l
doskey la=lsd -a
doskey lla=lsd -la
doskey lt=lsd --tree
doskey open = explorer $*

添加到%AppData%/../local/clink/zoxide.lua ,实现 z 快速跳转

javascript
-- =============================================================================
--
-- Settings copied from 'zoxide init'. Run `clink set` to modify these options, e.g. `clink set zoxide.cmd f`
--

settings.add('zoxide.cmd', 'z', 'Changes the prefix of the aliases')
settings.add('zoxide.hook', { 'pwd', 'prompt', 'none' }, 'Changes when directory scores are incremented')
settings.add('zoxide.no_aliases', false, "Don't define aliases")
settings.add('zoxide.usepromptfilter', false, "Use clink promptfilter to hook even if onbeginedit is supported")
settings.add('zoxide.promptfilter_prio', -50, "Changes the priority of the promptfilter hook (only if usepromptfilter is true)")

-- =============================================================================
--
-- Utility functions for zoxide.
--

-- Generate `cd` command
local function __zoxide_cd(dir)
  if os.getenv '_ZO_ECHO' == '1' then
    print(dir)
  end

  -- 'cd /d -' doesn't work for clink versions before v1.2.41 (https://github.com/chrisant996/clink/issues/191)
  -- lastest cmder release (v1.3.18) uses clink v1.1.45
  if dir == '-' and (clink.version_encoded or 0) < 10020042 then
    return 'cd -'
  end

  return 'cd /d ' .. dir
end

-- Run `zoxide query` and generate `cd` command from result
local function __zoxide_query(options, keywords)
  options = table.concat(options, ' ')
  keywords = table.concat(keywords, ' ')

  local file = io.popen('zoxide query ' .. options .. ' -- ' .. keywords)
  local result = file:read '*line'
  local ok = file:close()

  if ok then
    return __zoxide_cd(result)
  else
    return 'call' -- no-op that just sets %ERRORLEVEL% to 1
  end
end

-- Add directory to the database.
local function __zoxide_add(dir)
  os.execute('zoxide add -- "' .. dir:gsub('^(.-)\\*$', '%1') .. '"')
end

-- =============================================================================
--
-- Hook configuration for zoxide.
--
local __usepromptfilter = settings.get 'zoxide.usepromptfilter'

if not clink.onbeginedit then
  __usepromptfilter = true
end

local __zoxide_oldpwd
function __zoxide_hook()
  local zoxide_hook = settings.get 'zoxide.hook'

  if zoxide_hook == 'none' then
    -- do nothing
    return
  elseif zoxide_hook == 'prompt' then
    -- run `zoxide add` on every prompt
    __zoxide_add(os.getcwd())
  elseif zoxide_hook == 'pwd' then
    -- run `zoxide add` when the working directory changes
    local cwd = os.getcwd()
    if __zoxide_oldpwd and __zoxide_oldpwd ~= cwd then
      __zoxide_add(cwd)
    end
    __zoxide_oldpwd = cwd
  end
end

if __usepromptfilter then
  local __promptfilter_prio = settings.get 'zoxide.promptfilter_prio'
  local __zoxide_prompt = clink.promptfilter(__promptfilter_prio)

  function __zoxide_prompt:filter()
    __zoxide_hook()
  end
else
  clink.onbeginedit(__zoxide_hook)
end

-- =============================================================================
--
-- Define aliases.
--

-- 'z' alias
local function __zoxide_z(keywords)
  if #keywords == 0 then
    -- NOTE: `os.getenv("HOME")` returns HOME or HOMEDRIVE+HOMEPATH
    --       or USERPROFILE.
    return __zoxide_cd(os.getenv('HOME'))
  elseif #keywords == 1 then
    local keyword = keywords[1]
    if keyword == '-' then
      return __zoxide_cd '-'
    elseif os.isdir(keyword) then
      return __zoxide_cd(keyword)
    end
  end

  local cwd = '"' .. os.getcwd() .. '"'
  return __zoxide_query({ '--exclude', cwd }, keywords)
end

-- 'zi' alias
local function __zoxide_zi(keywords)
  return __zoxide_query({ '--interactive' }, keywords)
end

-- =============================================================================
--
-- Clink input text filter.
--

local function onfilterinput(text)
  args = string.explode(text, ' ', '"')
  if #args == 0 then
    return
  end

  -- settings
  zoxide_cmd = settings.get 'zoxide.cmd'
  zoxide_no_aliases = settings.get 'zoxide.no_aliases'

  -- edge case:
  -- * zoxide command prefix is 'cd'
  -- * clink converted 'cd -' -> 'cd /d "some_directory"'
  local cd_regex = '^%s*cd%s+/d%s+"(.-)"%s*$'
  if zoxide_cmd == 'cd' and text:match(cd_regex) then
    if zoxide_no_aliases then
      -- clink handles it
      return
    else
      -- zoxide handles it
      return __zoxide_cd(text:gsub(cd_regex, '%1')), false
    end
  end

  local cmd = table.remove(args, 1)
  if cmd == '__zoxide_z' or (cmd == zoxide_cmd and not zoxide_no_aliases) then
    return __zoxide_z(args), false
  elseif cmd == '__zoxide_zi' or (cmd == zoxide_cmd .. 'i' and not zoxide_no_aliases) then
    return __zoxide_zi(args), false
  else
    return
  end
end

if clink.onfilterinput then
  clink.onfilterinput(onfilterinput)
else
  clink.onendedit(onfilterinput)
end

clink.argmatcher("zi")
clink.argmatcher("z")
-- =============================================================================
--
-- To initalize zoxide, add this script to one of clink's lua script locations (e.g. zoxide.lua)
-- (see https://chrisant996.github.io/clink/clink.html#location-of-lua-scripts)
javascript
:: 设置每个会话默认开启“建议列表” (即 F2 开启的那个框)
clink set suggestionlist.default True
:: 将弹出列表的搜索模式改为 filter (过滤),而不是 find (查找)
clink set clink.popup_search_mode filter
:: 4. 历史去重:如果新输入的命令与旧的重复,删除旧的,只保留最后一次(保持历史简洁)
clink set history.dupe_mode erase_prev
:: 7. 开启子字符串匹配:输入 "web" 就能匹配到 "admin-web"
clink set match.substring True
:: 8. 忽略大小写且对横线/下划线不敏感(relaxed 模式最适合程序员)
clink set match.ignore_case relaxed

:: 9. 智能翻译斜杠:无论你输入 / 还是 \,Clink 都会自动根据 Windows 环境转换(解决跨平台拷贝路径的烦恼)
clink set match.translate_slashes auto

:: 11. 粘贴多行时,将换行符转为空格(防止粘贴后立即执行,给你检查的机会)
:: 可选值:space (转为空格), delete (删除换行), crlf (保持原样)
clink set clink.paste_crlf space

1. 快速导航(光标移动)

这些按键能让你在长命令中瞬间移动,而不是一直按着左/右键。

  • Ctrl + A 或 Home:跳到行首。

  • Ctrl + E 或 End:跳到行尾。

  • Ctrl + ← 或 Alt + B:向左跳过一个单词。

  • Ctrl + → 或 Alt + F:向右跳过一个单词。

  • Ctrl + X 再按 Ctrl + E:在记事本(或你的编辑器)中编辑当前命令(适合写复杂的 Go 启动脚本)。

2. 高效删除(剪切与撤销)

  • Ctrl + W:向左删除一个单词(非常常用,写错参数时直接删掉)。

  • Alt + D:向右删除一个单词。

  • Ctrl + K:删除光标到行尾的所有内容。

  • Ctrl + U:删除光标到行首的所有内容。

  • Ctrl + _ (Ctrl + 减号/下划线):撤销上一次操作(Undo)。

  • Ctrl + Y:粘贴(Yank)刚才用 Ctrl+K 或 Ctrl+W 删掉的内容。

3. 神级历史操作(除了 Ctrl+R 之外)

  • Alt + . (Alt + 点):插入上一条命令的最后一个参数

    • 场景: 你刚输入 mkdir my_new_project,接着输入 cd 然后按 Alt + .,它会自动补全 my_new_project。连续按会循环找更早的参数。
  • **Alt + *** (Alt + 星号):展开通配符

    • 场景: 输入 ls *.go 后按这个键,它会把所有匹配的文件名直接列在命令行里。
  • F7:弹出 Windows 风格的历史记录窗口(Clink 通常会美化它)。

  • PageUp / PageDown:基于当前已输入内容的前缀搜索历史。

4. 补全增强

  • Tab:标准补全。

  • Shift + Tab:反向循环补全项。

  • Ctrl + Space (Ctrl + 空格):列出所有可能的补全,但不自动填入(适合文件太多时查看列表)。

  • F2:在“循环补全”和“弹出列表”模式之间切换。