Simplify command aliases

Tue, 28 Mar 2023 23:57:49 -0500

author
Meredith Howard <mhoward@roomag.org>
date
Tue, 28 Mar 2023 23:57:49 -0500
changeset 1095
226221dc1d6b
parent 1094
694fb6cd9cc2
child 1096
8bb51fcd6f2a

Simplify command aliases

.config/nvim/lua/config/commands.lua file | annotate | diff | comparison | revisions
.config/nvim/lua/config/util.lua file | annotate | diff | comparison | revisions
--- a/.config/nvim/lua/config/commands.lua
+++ b/.config/nvim/lua/config/commands.lua
@@ -1,7 +1,5 @@
 local command = vim.api.nvim_create_user_command
-local calias = require("config.util").calias
-local cmd = vim.cmd
-local fn = vim.fn
+local cmd, fn = vim.cmd, vim.fn
 
 command("Hgcd", function()
   local root = fn.systemlist("hg root 2>/dev/null")[1]
@@ -31,16 +29,20 @@ command("Grep", function(ctx)
   cmd.cfirst()
 end, { nargs = "*", complete = "file" })
 
-calias("Q", "q")
-calias("Qa", "qa")
-calias("W", "w")
-calias("grep", "Grep")
+require("config.util").calias({
+  -- replace default:
+  grep = "Grep",
 
-calias("gcd", "Gcd")
-calias("hgcd", "Hgcd")
+  -- typos:
+  Q = "q",
+  Qa = "qa",
+  W = "w",
+  gcd = "Gcd",
+  hgcd = "Hgcd",
 
--- Switch these to default to stay in one window + buffer
-calias("doc", "ViewDoc!")
-calias("help", "ViewDocHelp!")
-calias("man", "ViewDocMan!")
-calias("perldoc", "ViewDocPerl!")
+  -- Make the ! versions default to stay in one window + buffer:
+  doc = "ViewDoc!",
+  help = "ViewDocHelp!",
+  man = "ViewDocMan!",
+  perldoc = "ViewDocPerl!",
+})
--- a/.config/nvim/lua/config/util.lua
+++ b/.config/nvim/lua/config/util.lua
@@ -1,18 +1,19 @@
 local M = {}
 
-local api = vim.api
-local fn = vim.fn
+local api, fn = vim.api, vim.fn
 
-function M.calias(abbrev, expand)
-  vim.cmd.cnoreabbrev(
-    string.format(
-      [[<expr> %s (getcmdtype() == ":" && getcmdline() == "%s") ? "%s" : "%s"]],
-      abbrev,
-      abbrev,
-      expand,
-      abbrev
+function M.calias(aliases)
+  for abbrev, expand in pairs(aliases) do
+    vim.cmd.cnoreabbrev(
+      string.format(
+        [[<expr> %s (getcmdtype() == ":" && getcmdline() == "%s") ? "%s" : "%s"]],
+        abbrev,
+        abbrev,
+        expand,
+        abbrev
+      )
     )
-  )
+  end
 end
 
 function M.autocmd(group, event, pattern, opts)

mercurial