Awesome plugin: ChatGPT with NeoVim
Tags: neovim, chatgpt, webdev, tutorial
Published: 2022-12-18 05:14:17

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

[https://chat.openai.com](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`.

- https://github.com/jackMort/ChatGPT.nvim
- [(2) ChatGPT - preview of my first neovim plugin [WIP] : neovim](https://www.reddit.com/r/neovim/comments/zkv5pq/chatgpt_preview_of_my_first_neovim_plugin_wip/)

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.

```sh
$ touch $HOME/.openai_key.zsh
$ vi $HOME/.openai_key.zsh
```
 
Modify `$HOME/.openai_key.zsh` to set OPEN_API_KEY that is obtainable on [this page](https://beta.openai.com/account/api-keys).
 
```bash
export OPENAI_API_KEY=<value>
```

Add the following code into `~/.zshrc` file.

```bash
# ChatGPT
source "$HOME/.openai_key.zsh"
```

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.
        
```bash
$ source ~/.zshrc
```

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.

- P**lugin/package manager**: https://github.com/wbthomason/packer.nvim
- **Where writes code**: init.lua

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

```lua
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"
        }
    })
    ...
)
```
        
Then, install it.
        
```vi
:PackerInstall
```
        
Currently, three commands are available in `ChatGPT.nvim`. So I registered those in `init.lua` as my key bindings.

```lua
-- 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>')
```
        
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.

{% embed https://www.youtube.com/watch?v=EkAhVKk7W10&ab_channel=%E6%9C%9D%E9%AE%AE%E4%B8%AD%E5%A4%AE%E6%94%BE%E9%80%81 %}

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.