Skip to content

Commit b85329b

Browse files
committed
Add floating terminal
1 parent a546ebe commit b85329b

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- [Aesthetics](#aesthetics)
1919
- [Remaps](#remaps)
2020
- [General](#general)
21+
- [Floating terminal](#floating-terminal)
2122
- [Split navigation](#split-navigation)
2223
- [Set - Unset](#set-unset)
2324
- [LSP](#lsp)
@@ -124,6 +125,14 @@ nvim/
124125
| `<leader>s` | Source file in current buffer |
125126

126127

128+
### Floating terminal <a name="floating-terminal"></a> ###
129+
130+
***
131+
| Keybind | Action |
132+
|---------------|----------------------------|
133+
| `<leader>t` | Toggle floating terminal |
134+
135+
127136
### Split navigation <a name="split-navigation"></a> ###
128137
***
129138
| Keybind | Action |

after/plugin/floating_terminal.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vim.keymap.set("n", "<leader>t", "<cmd>FloatingTerminal<cr>")
2+
vim.keymap.set("t", "<leader>t", "<cmd>FloatingTerminal<cr>")
3+

plugin/floating_terminal.lua

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
local state = {
2+
floating = {
3+
buf = -1,
4+
win = -1,
5+
}
6+
}
7+
8+
local function create_floating_window(opts)
9+
opts = opts or {}
10+
11+
local width = opts.width or math.floor(vim.o.columns * 0.8)
12+
local height = opts.height or math.floor(vim.o.lines * 0.8)
13+
14+
local col = math.floor((vim.o.columns - width) / 2)
15+
local row = math.floor((vim.o.lines - height) / 2)
16+
17+
local buf = nil
18+
if vim.api.nvim_buf_is_valid(opts.buf) then
19+
buf = opts.buf
20+
else
21+
buf = vim.api.nvim_create_buf(false, true)
22+
end
23+
24+
local win_config = {
25+
relative = "editor",
26+
width = width,
27+
height = height,
28+
col = col,
29+
row = row,
30+
style = "minimal",
31+
border = "rounded",
32+
}
33+
34+
local win = vim.api.nvim_open_win(buf, true, win_config)
35+
36+
return { buf = buf, win = win }
37+
end
38+
39+
local toggle_terminal = function()
40+
if not vim.api.nvim_win_is_valid(state.floating.win) then
41+
state.floating = create_floating_window { buf = state.floating.buf }
42+
if vim.bo[state.floating.buf].buftype ~= "terminal" then
43+
vim.cmd.terminal()
44+
end
45+
else
46+
vim.api.nvim_win_hide(state.floating.win)
47+
end
48+
end
49+
50+
vim.api.nvim_create_user_command("FloatingTerminal", toggle_terminal, {})

0 commit comments

Comments
 (0)