Skip to content

Commit 0fbc4bf

Browse files
committed
chore: initial commit
0 parents  commit 0fbc4bf

12 files changed

+653
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
init.lua

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Sirisak Lueangsaksri
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
testrun:
2+
nvim -u tests/init.lua

README.md

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# tmux.nvim
2+
3+
_This plugin still under development_
4+
5+
This plugin provides a framework to turns
6+
[Neovim](https://github.com/neovim/neovim) into terminal multiplexer
7+
(heavily based on [tmux](https://github.com/tmux/tmux))
8+
9+
The intention of this plugin is to eliminate the need of tmux for a basic
10+
terminal multiplexing feature by leveraging Neovim split and tab features
11+
12+
## Table Of Contents
13+
14+
* [Installation](#installation)
15+
* [Integration](#integration)
16+
* [Configuration](#configuration)
17+
* [Contributes](#contributes)
18+
* [License](#license)
19+
20+
## Installation
21+
22+
It is highly recommended to clone the repository (or download a zip file) and
23+
place it somewhere (`~/.tmux.nvim` would work too)
24+
25+
```sh
26+
git clone https://github.com/spywhere/tmux.nvim ~/.tmux.nvim
27+
```
28+
29+
or install using a plugin manager of your choice, for example:
30+
31+
```viml
32+
" neovim 0.5 or later which support lsp and lua
33+
Plug 'itchyny/lightline.vim' " Dependency: status line
34+
Plug 'spywhere/lightline-lsp'
35+
```
36+
37+
## Integration
38+
39+
Create a file named `init.lua` and place it somewhere
40+
(in this example, `~/.tmux.nvim/init.lua` will be used)
41+
42+
```lua
43+
local tmux = require('tmux')
44+
45+
-- some configurations go here
46+
47+
tmux.start() -- this will start a terminal session
48+
49+
```
50+
51+
then, when you're ready to use Neovim as terminal multiplexer, just run
52+
53+
```sh
54+
nvim -u ~/.tmux.nvim/init.lua
55+
```
56+
57+
## Configuration
58+
59+
As the plugin still under development, please find the default configurations
60+
from following locations...
61+
62+
- Neovim configurations: `lua/tmux/config.lua`
63+
- Key bindings: `lua/tmux/bindings.lua`
64+
- Available commands: `lua/tmux/commands.lua`
65+
66+
Please note that some options are opinionated and will be updated to match
67+
tmux's defaults later on
68+
69+
## Contributes
70+
71+
During the development, you can use the following command to automatically setup
72+
a working configurations to test the plugin...
73+
74+
```
75+
make testrun
76+
```
77+
78+
or
79+
80+
```
81+
nvim -u tests/init.lua
82+
```
83+
84+
## License
85+
86+
Released under the [MIT License](LICENSE)

lua/tmux/bindings.lua

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
local fn = require('tmux.lib.fn')
2+
local cmds = require('tmux.commands')
3+
4+
return fn.nested(2, function (P, M)
5+
M.bind('c', cmds.new_window())
6+
M.bind('-', cmds.split_window({ 'h' }))
7+
M.bind('<bar>', cmds.split_window({ 'v' }))
8+
M.bind(':', cmds.command_prompt())
9+
10+
M.bind('<C-h>', cmds.select_pane({ 'L' }), { 'n' })
11+
M.bind('<C-j>', cmds.select_pane({ 'D' }), { 'n' })
12+
M.bind('<C-k>', cmds.select_pane({ 'U' }), { 'n' })
13+
M.bind('<C-l>', cmds.select_pane({ 'R' }), { 'n' })
14+
end)

lua/tmux/commands.lua

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
local fn = require('tmux.lib.fn')
2+
3+
local M = {}
4+
5+
M.new_window = function ()
6+
return fn.nested(2, function ()
7+
vim.cmd('terminal')
8+
end)
9+
end
10+
M.neww = M.new_window
11+
12+
M.command_prompt = function ()
13+
return fn.nested(2, function ()
14+
vim.cmd('stopinsert')
15+
vim.api.nvim_feedkeys(':', 'n', true)
16+
end)
17+
end
18+
19+
M.send_prefix = function ()
20+
return fn.nested(2, function (P)
21+
vim.api.nvim_feedkeys(P.prefix, 't', true)
22+
end)
23+
end
24+
25+
M.kill_window = function ()
26+
return fn.nested(2, function ()
27+
end)
28+
end
29+
M.killw = M.kill_window
30+
31+
M.previous_window = function ()
32+
return fn.nested(2, function ()
33+
end)
34+
end
35+
M.prev = M.pervious_window
36+
37+
M.next_window = function ()
38+
return fn.nested(2, function ()
39+
end)
40+
end
41+
M.next = M.next_window
42+
43+
M.select_window = function ()
44+
return fn.nested(2, function ()
45+
end)
46+
end
47+
M.selectw = M.select_window
48+
49+
M.split_window = function (opts)
50+
for key, value in pairs(opts) do
51+
if type(key) == 'number' then
52+
if value == 'h' then
53+
return fn.nested(2, function ()
54+
vim.cmd('split | terminal')
55+
end)
56+
elseif value == 'v' then
57+
return fn.nested(2, function ()
58+
vim.cmd('vsplit | terminal')
59+
end)
60+
end
61+
end
62+
end
63+
64+
return fn.nested(2)
65+
end
66+
M.splitw = M.split_window
67+
68+
M.select_pane = function (opts)
69+
for key, value in pairs(opts) do
70+
if type(key) == 'number' then
71+
if value == 'L' then
72+
return fn.nested(2, function ()
73+
vim.cmd('wincmd h')
74+
end)
75+
elseif value == 'D' then
76+
return fn.nested(2, function ()
77+
vim.cmd('wincmd j')
78+
end)
79+
elseif value == 'U' then
80+
return fn.nested(2, function ()
81+
vim.cmd('wincmd k')
82+
end)
83+
elseif value == 'R' then
84+
return fn.nested(2, function ()
85+
vim.cmd('wincmd l')
86+
end)
87+
end
88+
end
89+
end
90+
91+
return fn.nested(2)
92+
end
93+
M.selectp = M.select_pane
94+
95+
return M

lua/tmux/config.lua

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
local fn = require('tmux.lib.fn')
2+
3+
return fn.nested(2, function ()
4+
-- no status line
5+
vim.o.laststatus = 0
6+
vim.o.statusline = ' '
7+
-- no command line
8+
-- currently not possible
9+
-- ref: https://github.com/neovim/neovim/issues/1004
10+
-- vim.o.cmdheight = 0
11+
-- workaround for hiding command line
12+
vim.opt.lines:append(1)
13+
14+
-- remove command line clutter
15+
vim.o.showmode = false
16+
vim.o.showcmd = false
17+
vim.o.ruler = false
18+
19+
vim.o.splitright = true
20+
vim.o.splitbelow = true
21+
end)

lua/tmux/event.lua

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
local fn = require('tmux.lib.fn')
2+
local registry = require('tmux.lib.registry')
3+
4+
return fn.nested(2, function (P, M)
5+
function start_insert()
6+
vim.defer_fn(function ()
7+
if vim.bo.buftype == 'terminal' then
8+
vim.cmd('startinsert!')
9+
end
10+
end, 10)
11+
end
12+
13+
registry.auto({ 'BufEnter', 'CmdlineLeave' }, start_insert)
14+
registry.auto('TermOpen', 'startinsert!')
15+
16+
function on_window_close()
17+
vim.defer_fn(function ()
18+
local wins = vim.api.nvim_list_wins()
19+
20+
local count = 0
21+
for _, win in ipairs(wins) do
22+
local buffer = vim.api.nvim_win_get_buf(win)
23+
local buffer_type = vim.api.nvim_buf_get_option(buffer, 'buftype')
24+
if buffer_type == 'terminal' then
25+
count = count + 1
26+
end
27+
end
28+
29+
if count == 0 then
30+
vim.cmd('cquit! ' .. P.last_status)
31+
end
32+
end, 10)
33+
end
34+
35+
function on_terminal_close(event)
36+
P.last_status = event.status
37+
38+
on_window_close()
39+
end
40+
41+
registry.auto('TermClose', 'call ' .. registry.call_for_fn(on_terminal_close, 'v:event'))
42+
registry.auto('BufLeave', on_window_close)
43+
end)

0 commit comments

Comments
 (0)