banner
orion

orion

中国科学技术大学研究生;数据库内核开发工程师;生产力爱好者;

Fluently Using ChatGPT in Lunarvim/Neovim

Demo#

Chatting with ChatGPT#

image

  • Enter NeoAI in command line mode to bring up the dialog box on the right side of the terminal, which has the same experience and speed as using a browser.
  • Press Esc key to hide the dialog box. Please note that each time you re-enter NeoAI, it is equivalent to opening a new session.
  • So how do you bring up the dialog box without opening a new session after hiding it?

Explaining the code#

NeoAIContext

  • By selecting the code to be passed in the view mode and then entering NeoAIContext in the command line mode, it is equivalent to copying and sending the selected code to ChatGPT, so that ChatGPT can provide more accurate answers based on the code information you provided.
  • If you directly enter NeoAIContext in the command line mode, it is equivalent to copying and sending the entire buffer.

Writing documents#

NeoAIInject

  • Enter NeoAIInject followed by the topic of the document you want to write in the command line mode, and ChatGPT will generate the corresponding document and insert it into the currently open file.

Configuration and Usage#

Adding plugins#

For Lunarvim users, just add the following code to lvim.plugins:

 {
        "Bryley/neoai.nvim",
        dependencies = {
            "MunifTanjim/nui.nvim",
        },
        cmd = {
            "NeoAI",
            "NeoAIOpen",
            "NeoAIClose",
            "NeoAIToggle",
            "NeoAIContext",
            "NeoAIContextOpen",
            "NeoAIContextClose",
            "NeoAIInject",
            "NeoAIInjectCode",
            "NeoAIInjectContext",
            "NeoAIInjectContextCode",
        },
        keys = {
            { "<leader>as", desc = "summarize text" },
            { "<leader>ag", desc = "generate git message" },
        },
        config = function()
            require('neoai').setup {
                -- Below are the default options, feel free to override what you would like changed
                ui = {
                    output_popup_text = "NeoAI",
                    input_popup_text = "Prompt",
                    width = 30,               -- As percentage eg. 30%
                    output_popup_height = 80, -- As percentage eg. 80%
                    submit = "<Enter>",       -- Key binding to submit the prompt
                },
                models = {
                    {
                        name = "openai",
                        model = "gpt-3.5-turbo",
                        params = nil,
                    },
                },
                register_output = {
                    ["g"] = function(output)
                        return output
                    end,
                    ["c"] = require("neoai.utils").extract_code_snippets,
                },
                inject = {
                    cutoff_width = 75,
                },
                prompts = {
                    context_prompt = function(context)
                        return "Hey, I'd like to provide some context for future "
                            .. "messages. Here is the code/text that I want to refer "
                            .. "to in our upcoming conversations:\n\n"
                            .. context
                    end,
                },
                mappings = {
                    ["select_up"] = "<C-k>",
                    ["select_down"] = "<C-j>",
                },
                open_api_key_env = "OPENAI_API_KEY",
                shortcuts = {
                    {
                        name = "textify",
                        key = "<leader>as",
                        desc = "fix text with AI",
                        use_context = true,
                        prompt = [[
                Please rewrite the text to make it more readable, clear,
                concise, and fix any grammatical, punctuation, or spelling
                errors
            ]],
                        modes = { "v" },
                        strip_function = nil,
                    },
                    {
                        name = "gitcommit",
                        key = "<leader>ag",
                        desc = "generate git commit message",
                        use_context = false,
                        prompt = function()
                            return [[
                    Using the following git diff generate a consise and
                    clear git commit message, with a short title summary
                    that is 75 characters or less:
                ]] .. vim.fn.system("git diff --cached")
                        end,
                        modes = { "n" },
                        strip_function = nil,
                    },
                },
            }
        end
    },

neoai

Adding OPENAI_API_KEY environment variable#

  • Open ~/.zshrc (If you are using bash, open ~/.bashrc)
    sudo vim ~/.zshrc
    
  • Add the following content at the end (replace it with your own OPENAI token)
    export OPENAI_API_KEY=sk-kSynjjGgqIdsgVIK7iyz$$$$$$$$$$$$KFYkQwJxM8
    
  • Save and exit (enter wq in command line mode)
  • Make the changes take effect
    source ~/.zshrc
    
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.