Skip to content

Commit 16615d8

Browse files
author
ruben.galvan
committed
adding floating terminal
1 parent 3e6536b commit 16615d8

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

init.lua

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,23 @@ vim.api.nvim_create_autocmd('TextYankPost', {
220220
end,
221221
})
222222

223+
vim.api.nvim_create_autocmd('TermOpen', {
224+
group = vim.api.nvim_create_augroup('custom-term-open', { clear = true }),
225+
callback = function()
226+
vim.opt.number = false
227+
vim.opt.relativenumber = false
228+
vim.cmd.setlocal 'nospell'
229+
end,
230+
})
231+
232+
vim.keymap.set('n', '<leader>st', function()
233+
vim.cmd.vnew()
234+
vim.cmd.term()
235+
vim.cmd.wincmd 'J'
236+
vim.cmd.setlocal 'nospell'
237+
vim.api.nvim_win_set_height(0, 5)
238+
end, { desc = 'Move focus to the upper window' })
239+
223240
-- [[ Install `lazy.nvim` plugin manager ]]
224241
-- See `:help lazy.nvim.txt` or https://github.com/folke/lazy.nvim for more info
225242
local lazypath = vim.fn.stdpath 'data' .. '/lazy/lazy.nvim'
@@ -403,7 +420,9 @@ require('lazy').setup({
403420
require('telescope').setup {
404421
-- You can put your default mappings / updates / etc. in here
405422
-- All the info you're looking for is in `:help telescope.setup()`
406-
--
423+
defaults = {
424+
file_ignore_patterns = { '.git', 'node_modules/.*', 'out/' },
425+
},
407426
-- defaults = {
408427
-- mappings = {
409428
-- i = { ['<c-enter>'] = 'to_fuzzy_refine' },

plugin/floaterminal.lua

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
vim.keymap.set('t', '<esc><esc>', '<c-\\><c-n>')
2+
3+
local state = {
4+
floating = {
5+
buf = -1,
6+
win = -1,
7+
},
8+
}
9+
10+
local function create_floating_window(opts)
11+
opts = opts or {}
12+
local width = opts.width or math.floor(vim.o.columns * 0.8)
13+
local height = opts.height or math.floor(vim.o.lines * 0.8)
14+
15+
-- Calculate the position to center the window
16+
local col = math.floor((vim.o.columns - width) / 2)
17+
local row = math.floor((vim.o.lines - height) / 2)
18+
19+
-- Create a buffer
20+
local buf = nil
21+
if vim.api.nvim_buf_is_valid(opts.buf) then
22+
buf = opts.buf
23+
else
24+
buf = vim.api.nvim_create_buf(false, true) -- No file, scratch buffer
25+
end
26+
27+
-- Define window configuration
28+
local win_config = {
29+
relative = 'editor',
30+
width = width,
31+
height = height,
32+
col = col,
33+
row = row,
34+
style = 'minimal', -- No borders or extra UI elements
35+
border = 'rounded',
36+
}
37+
38+
-- Create the floating window
39+
local win = vim.api.nvim_open_win(buf, true, win_config)
40+
41+
return { buf = buf, win = win }
42+
end
43+
44+
local toggle_terminal = function()
45+
if not vim.api.nvim_win_is_valid(state.floating.win) then
46+
state.floating = create_floating_window { buf = state.floating.buf }
47+
if vim.bo[state.floating.buf].buftype ~= 'terminal' then
48+
vim.cmd.terminal()
49+
end
50+
else
51+
vim.api.nvim_win_hide(state.floating.win)
52+
end
53+
end
54+
55+
-- Example usage:
56+
-- Create a floating window with default dimensions
57+
vim.api.nvim_create_user_command('Floaterminal', toggle_terminal, {})
58+
vim.keymap.set({ 'n', 't' }, '<leader>tt', toggle_terminal, { desc = 'Open Floating Terminal' })

0 commit comments

Comments
 (0)