From 1cf300e2c67c84b1ae3acebc3567c2cc86d63e54 Mon Sep 17 00:00:00 2001 From: Yusaku Mandai Date: Sun, 28 Dec 2025 22:29:54 +0900 Subject: [PATCH] nvim: add vim-slime --- nvim/lua/helper-ipython.lua | 35 +++++++++++++++++++++++++++++++++++ nvim/lua/plugins.lua | 19 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 nvim/lua/helper-ipython.lua diff --git a/nvim/lua/helper-ipython.lua b/nvim/lua/helper-ipython.lua new file mode 100644 index 0000000..a58feea --- /dev/null +++ b/nvim/lua/helper-ipython.lua @@ -0,0 +1,35 @@ +local M = {} + +-- Detects the start and end lines of the current IPython cell. +function M.detect_cell_range() + local cur = vim.fn.line(".") + local last = vim.fn.line("$") + + -- Find the start of the cell by searching upwards for # %% + local start = cur + for l = cur, 1, -1 do + local line = vim.fn.getline(l) + if line:match("^%s*# %%%%") and l ~= cur then + start = l + 1 + break + elseif l == 1 then + start = 1 + end + end + + -- Find the end of the cell by searching downwards for # %% + local finish = cur + for l = cur + 1, last do + local line = vim.fn.getline(l) + if line:match("^%s*# %%%%") then + finish = l - 1 + break + elseif l == last then + finish = last + end + end + + return start, finish +end + +return M diff --git a/nvim/lua/plugins.lua b/nvim/lua/plugins.lua index 11c5539..6f06b7c 100644 --- a/nvim/lua/plugins.lua +++ b/nvim/lua/plugins.lua @@ -20,6 +20,25 @@ return { end, }, + -- Sends buffer content to console REPL. + { + "jpalardy/vim-slime", + config = function() + vim.g.slime_target = "tmux" + vim.g.slime_python_ipython = 1 + vim.g.slime_default_config = { + socket_name = "default", + target_pane = "{last}", + } + vim.g.slime_dont_ask_default = 1 + + vim.keymap.set("n", "ip", function() + local s, e = require("helper-ipython").detect_cell_range() + vim.cmd(string.format("%d,%dSlimeSend", s, e)) + end) + end, + }, + -- Lua library for nvim. { "nvim-lua/plenary.nvim" },