如何像Jupyter一样在Neovim上运行Python
#python #jupyter #neovim #vim

我是代码编辑的人,我是一个码头人。我尝试了从sublimetext vscode到pycharm的所有编辑器,然后我一直回到vim/neovim。

因为我在AI/ML上工作,因此无法在Python中开发,也不会与Jupyter Notebooks遇到Deople Demo'ing代码。我承认这真的很酷。

所以,我去了旅程,以解决Neovim上的这一缺失的功能。

因此,首先,我将展示如何使用Lazyvim进行操作,因为它的软件包经理不太容易就愿意尝试Neovim进行旋转的人开始工作。

tl; dr :请参阅视频https://www.youtube.com/watch?v=8xP1eZ7E0h0

Neovim安装

brew install neovim@0.9.0

其他需求

  • git
  • python

如果您还没有安装它们(或仅在此处停止):

brew install git
brew install python@3.11

安装Lazyim

备份旧配置

如果您已经有另一个安装,请进行一些备份:

# required
mv ~/.config/nvim ~/.config/nvim.bak

# optional but recommended
mv ~/.local/share/nvim ~/.local/share/nvim.bak
mv ~/.local/state/nvim ~/.local/state/nvim.bak
mv ~/.cache/nvim ~/.cache/nvim.bak

下载 lazyvim 启动器,不要惊讶 *快速与激情”的安装是:

git clone https://github.com/LazyVim/starter ~/.config/nvim

现在,将Neovim旋转,握住您的座位并跑步:

nvim

安装插件以运行代码

成功安装了leazyvim后,退出并添加一个插件:

cd ~/.config/nvim
nvim .

在文件夹lua/plugins/code-runner.lua
上创建文件(文件浏览器上的'a'命令)

return {
  "hkupty/iron.nvim",
  config = function(plugins, opts)
    local iron = require("iron.core")

    iron.setup({
      config = {
        -- Whether a repl should be discarded or not
        scratch_repl = true,
        -- Your repl definitions come here
        repl_definition = {
          python = {
            -- Can be a table or a function that
            -- returns a table (see below)
            command = { "python" },
          },
        },
        -- How the repl window will be displayed
        -- See below for more information
        repl_open_cmd = require("iron.view").right(60),
      },
      -- Iron doesn't set keymaps by default anymore.
      -- You can set them here or manually add keymaps to the functions in iron.core
      keymaps = {
        send_motion = "<space>rc",
        visual_send = "<space>rc",
        send_file = "<space>rf",
        send_line = "<space>rl",
        send_mark = "<space>rm",
        mark_motion = "<space>rmc",
        mark_visual = "<space>rmc",
        remove_mark = "<space>rmd",
        cr = "<space>r<cr>",
        interrupt = "<space>r<space>",
        exit = "<space>rq",
        clear = "<space>rx",
      },
      -- If the highlight is on, you can change how it looks
      -- For the available options, check nvim_set_hl
      highlight = {
        italic = true,
      },
      ignore_blank_lines = true, -- ignore blank lines when sending visual select lines
    })

    -- iron also has a list of commands, see :h iron-commands for all available commands
    vim.keymap.set("n", "<space>rs", "<cmd>IronRepl<cr>")
    vim.keymap.set("n", "<space>rr", "<cmd>IronRestart<cr>")
    vim.keymap.set("n", "<space>rF", "<cmd>IronFocus<cr>")
    vim.keymap.set("n", "<space>rh", "<cmd>IronHide<cr>")
  end,
}

保存退出文件<space>qq

测试代码跑步者

打开一个python文件test.py

name = "AIOPS Again"
print(f"Hello, {name}")

name = "Another AIOPS"
print(f"Hello, {name}")

键入<space>r并输入,您应该在右侧具有代码执行窗口:

Neovim Menu

如果您执行代码,则有几个选项:

  1. 按行执行 <space>rl
  2. 执行选定的行 <space>rc
  3. 执行全文件 <space>rf

尝试通过选择一些行来执行一条行选择:

Lines of Code Selected on Neovim

他们将代码发送给跑步者:

Code Execution Result

实际上,此插件可以运行您想要的任何内容:

Runtime selection