nvim

https://git.tonybtw.com/nvim.git git://git.tonybtw.com/nvim.git
1,536 bytes raw
1
-- Remap leaving 'terminal mode' to double tap esc
2
vim.keymap.set("t", "<esc><esc>", "<c-\\><c-n>")
3
4
local state = {
5
    floating = {
6
        buf = -1,
7
        win = -1,
8
    }
9
}
10
11
local function open_floating_terminal(opts)
12
    opts = opts or {}
13
    local width = opts.width or math.floor(vim.o.columns * 0.8)
14
    local height = opts.height or math.floor(vim.o.lines * 0.8)
15
16
    local row = math.floor((vim.o.lines - height) / 2)
17
    local col = math.floor((vim.o.columns - width) / 2)
18
19
    local buf = nil
20
    if vim.api.nvim_buf_is_valid(opts.buf) then
21
        buf = opts.buf
22
    else
23
        buf = vim.api.nvim_create_buf(false, true)
24
    end
25
    if not buf then
26
        error("Failed to create buffer")
27
    end
28
29
    local win = vim.api.nvim_open_win(buf, true, {
30
        relative = 'editor',
31
        width = width,
32
        height = height,
33
        row = row,
34
        col = col,
35
        style = 'minimal',
36
        border = 'rounded',
37
    })
38
39
    return { buf = buf, win = win }
40
end
41
42
local toggle_terminal = function()
43
    if not vim.api.nvim_win_is_valid(state.floating.win) then
44
        state.floating = open_floating_terminal({ buf = state.floating.buf });
45
        if vim.bo[state.floating.buf].buftype ~= "terminal" then
46
            vim.cmd.terminal()
47
            vim.cmd("startinsert!")
48
        end
49
    else
50
        vim.api.nvim_win_hide(state.floating.win)
51
    end
52
end
53
54
vim.api.nvim_create_user_command("Flterm", toggle_terminal, {})
55
vim.api.nvim_set_keymap('n', '<leader>ft', [[:Flterm<CR>]], { noremap = true, silent = true })