DEV Community

Cover image for Awesome plugin: ChatGPT with NeoVim
uuta
uuta

Posted on • Updated on

Awesome plugin: ChatGPT with NeoVim

Today, OpenAI GPT-3 chatbot has enormous potential. Lately, there’s been a new AI bot in town: ChatGPT.

https://chat.openai.com

Releasing ChatGPT, it seems that many ambitious software engineers work on advancing its API via Slack, Line bot, and so on. I was finding luckily a NeoVim plugin that interacts with OpenAI’s GPT-3 chatbot: ChatGPT.nvim.

I’m a heavy user who’s enthusiastic to try a new feature of NeoVim, so this time I also began introducing this fascinating plugin. I will share with you how we use it and ChatGPT.nvim works.

Input the touch command to make .openai_key.zsh file.

$ touch $HOME/.openai_key.zsh
$ vi $HOME/.openai_key.zsh
Enter fullscreen mode Exit fullscreen mode

Modify $HOME/.openai_key.zsh to set OPEN_API_KEY that is obtainable on this page.

export OPENAI_API_KEY=<value>
Enter fullscreen mode Exit fullscreen mode

Add the following code into ~/.zshrc file.

# ChatGPT
source "$HOME/.openai_key.zsh"
Enter fullscreen mode Exit fullscreen mode

The reason why sets up an environment variable via .zsh is that the ChatGPT NeoVim plugin reads it with os.getenv() which is the method in python to extract an environment variable.

https://github.com/jackMort/ChatGPT.nvim/blob/6f602143d64b506fa8fcfa349df54ddb289360f2/lua/chatgpt/api.lua#L9-L13

Execute the following command to load configurations.

$ source ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

Let’s move on to the next subject which configures NeoVim. It depends on developers about how the NeoVim configuration is, so the next description might not be suitable for everybody. I would be glad to consider what your configuration is. My NeoVim environment is there.

If you’ve already handled packer.nvim, installing ChatGPT.nvim is really effortless. Copy and paste the next code in the require(’packer’).

require('packer').startup(function(use)
    ...
    -- ChatGPT
    -- https://github.com/jackMort/ChatGPT.nvim
    use({
        "jackMort/ChatGPT.nvim",
        config = function() require("chatgpt").setup({}) end,
        requires = {
            "MunifTanjim/nui.nvim", "nvim-lua/plenary.nvim",
            "nvim-telescope/telescope.nvim"
        }
    })
    ...
)
Enter fullscreen mode Exit fullscreen mode

Then, install it.

:PackerInstall
Enter fullscreen mode Exit fullscreen mode

Currently, three commands are available in ChatGPT.nvim. So I registered those in init.lua as my key bindings.

-- ChatGPT
-- https://github.com/jackMort/ChatGPT.nvim
vim.keymap.set('n', '<Leader>tk', '<cmd>:ChatGPT<cr>')
vim.keymap.set('n', '<Leader>tj', '<cmd>:ChatGPTActAs<cr>')
vim.keymap.set('n', '<Leader>tt', '<cmd>:ChatGPTEditWithInstructions<cr>')
Enter fullscreen mode Exit fullscreen mode

Let’s take a look at the screen after pushing the command <Leader>tt or :ChatGPTEditWithInstructions: opens an interactive window to edit selected text or the whole window.

Super cool! OpenAI GPT-3 chatbot returned a response that fixed a bug. Of course, the response from OpenAI GPT-3 chatbot might be not always correct and proper, but it will give you great insight and improve your velocity of development.

Top comments (0)