index : neolij.nvim
Seamless Neovim/Zellij integration with built-in navigation, pane/tab management, and Lua scripting.
about summary refs log tree commit diff
neolij.nvim / lua / zellij / init.lua
-rw-r--r-- blob 984cb9dbd46a4c3d5b42aaadaac09c518fe88bfb 3308 bytes (plain)
1
local M = {}
2
3
-- Modes
4
local MODE = {
5
  WINDOW = "window",
6
  TAB = "tab",
7
}
8
9
  local direction_translation = {
10
    up = "k",
11
    down = "j",
12
    left = "h",
13
    right = "l",
14
  }
15
-- Run zellij action safely
16
function M.zellij_action(args)
17
  vim.fn.system("zellij action " .. args)
18
  if vim.v.shell_error ~= 0 then
19
    error("zellij executable not found in PATH")
20
  end
21
end
22
23
-- Try vim window move, fallback to zellij
24
local function navigate(direction, vim_key, fallback)
25
  local before = vim.fn.winnr()
26
  vim.cmd("wincmd " .. vim_key)
27
  local after = vim.fn.winnr()
28
29
  -- If vim didn't move, fallback to zellij
30
  if before == after then
31
    M.zellij_action(fallback .. " " .. direction)
32
  end
33
end
34
35
-- Window navigation
36
function M.move(direction)
37
  navigate(direction, direction_translation[direction], "move-focus")
38
end
39
40
-- Window navigation or tab switch
41
function M.move_or_tab(direction)
42
  navigate(direction, direction_translation[direction], "move-focus-or-tab")
43
end
44
45
-- Tab actions
46
function M.new_tab()
47
  M.zellij_action("new-tab")
48
end
49
50
function M.rename_tab(opts)
51
  local name = opts.args
52
53
  if name == "" then
54
    name = vim.fn.input("Tab name: ")
55
  end
56
57
  if name == nil or name == "" then
58
    vim.notify("No tab name provided", vim.log.levels.ERROR)
59
    return
60
  end
61
62
  M.zellij_action("rename-tab " .. name)
63
end
64
65
function M.move_tab(direction)
66
  M.zellij_action("move-tab " .. direction)
67
end
68
69
-- Pane actions
70
function M.new_pane()
71
  M.zellij_action("new-pane")
72
end
73
74
function M.rename_pane(opts)
75
  local name = opts.args
76
77
  if name == "" then
78
    name = vim.fn.input("Pane name: ")
79
  end
80
81
  if name == nil or name == "" then
82
    vim.notify("No pane name provided", vim.log.levels.ERROR)
83
    return
84
  end
85
86
  M.zellij_action("rename-pane " .. name)
87
end
88
89
function M.move_pane(direction)
90
  M.zellij_action("move-pane " .. direction)
91
end
92
93
-- Setup user commands
94
function M.setup()
95
  vim.api.nvim_create_user_command("ZellijUp", function() M.move("up") end, {})
96
  vim.api.nvim_create_user_command("ZellijDown", function() M.move("down") end, {})
97
  vim.api.nvim_create_user_command("ZellijLeft", function() M.move("left") end, {})
98
  vim.api.nvim_create_user_command("ZellijRight", function() M.move("right") end, {})
99
100
  vim.api.nvim_create_user_command("ZellijLeftTab", function() M.move_or_tab("left") end, {})
101
  vim.api.nvim_create_user_command("ZellijRightTab", function() M.move_or_tab("right") end, {})
102
103
  vim.api.nvim_create_user_command("ZellijNewTab", M.new_tab, {})
104
  vim.api.nvim_create_user_command("ZellijRenameTab", M.rename_tab, { nargs = "?" })
105
  vim.api.nvim_create_user_command("ZellijMoveTabRight", function() M.move_tab("right") end, {})
106
  vim.api.nvim_create_user_command("ZellijMoveTabLeft", function() M.move_tab("left") end, {})
107
108
  vim.api.nvim_create_user_command("ZellijNewPane", M.new_tab, {})
109
  vim.api.nvim_create_user_command("ZellijRenamePane", M.rename_pane, { nargs = "?" })
110
  vim.api.nvim_create_user_command("ZellijMovePaneUp", function() M.move_pane("up") end, {})
111
  vim.api.nvim_create_user_command("ZellijMovePaneDown", function() M.move_pane("down") end, {})
112
  vim.api.nvim_create_user_command("ZellijMovePaneRight", function() M.move_pane("right") end, {})
113
  vim.api.nvim_create_user_command("ZellijMovePaneLeft", function() M.move_pane("left") end, {})
114
115
end
116
117
return M