Part 1: Setting Up NVChad

Note: I am using Arch Linux. So, use terminal commands as per your Distro.

Step 1: Removing any previous configuration

  1. Linux/MacOS Users
rm -rf ~/.config/nvim
rm -rf ~/.local/state/nvim
rm -rf ~/.local/share/nvim
  1. Flatpak(Linux) Users
rm -rf ~/.var/app/io.neovim.nvim/config/nvim
rm -rf ~/.var/app/io.neovim.nvim/data/nvim
rm -rf ~/.var/app/io.neovim.nvim/.local/state/nvim
  1. Window Users
  • CMD
     rd -r ~\AppData\Local\nvim
     rd -r ~\AppData\Local\nvim-data
    
  • Powershell
    rm -Force ~\AppData\Local\nvim
    rm -Force ~\AppData\Local\nvim-data
    

Step 2: Installing NVChad

  1. Clone the NVChad repository
  • Linux/MacOS:
  git clone https://github.com/NvChad/starter ~/.config/nvim && nvim
  • Flatpak:
 git clone https://github.com/NvChad/starter ~/.var/app/io.neovim.nvim/config/nvim && flatpak run io.neovim.nvim
  • Windows (CMD):
  git clone https://github.com/NvChad/starter %USERPROFILE%\AppData\Local\nvim && nvim
  • Windows (Powershell):
  git clone https://github.com/NvChad/starter $ENV:USERPROFILE\AppData\Local\nvim && nvim

Step 3: Basic Neovim commands to attempt further processes

Type nvim or neovim on the terminal before any file name(optional) to enter into neovim environment.

  1. Enter editing mode:
  i
  1. Escape the editing mode to save or exit:

Press the escape button

  1. Save file:
:w
  1. Exit with saving:
  :wq
  1. Exit without saving:
  :q!

Step 4: Essential processes after installing

  1. Run following command into nvim after lazy.nvim finishes downloading plugins:
 :MasonInstallAll
  1. Navigate to nvim directory in .config folder to the remove .git folder.
  rm -rf /home/username/.config/nvim/.git
  1. Learn customization of UI and base46 by using following command inside nvim:
:h nvui
  1. Update the packages:
 :Lazy sync

Step 5: Setting up LSP Configurations

You have to add required language server protocols to improve your coding experience.

  1. Ensure that lazy_config is set up correctly after vim.otp.rtp in the file home/username/.config/nvim/init.lua, if not, then add the following content to init.lua file after vim.otp.rtp:
local lazy_config = require "configs.lazy"
-- load plugins
require("lazy").setup({
 {
   "NvChad/NvChad",
   lazy = false,
   branch = "v2.5",
   import = "nvchad.plugins",
 },
 { import = "plugins"},
}, lazy_config)
  1. Before moving further make sure that you have installed npm server on your system. If not, then download it using:
 sudo pacman -S nodejs npm
sudo npm install -g pyright
  1. For C/C++ and Python development, ensure clangd and pyright is installed and configured: Add the following piece of code to init.lua after the ending of above mentioned code:
--clangd server setup
local lspconfig = require('lspconfig')
lspconfig.clangd.setup({           --For C/C++
  cmd = { "clangd" },  -- Ensure this points to the system-installed clangd
  -- Additional configuration options can be added here
})
lspconfig.pyright.setup({      --For Python
  autoSearchPaths = true,
  diagnosticMode = "workplace",   --you can also set it to "openFilesOnly" to check errors in only oepned files
})
  1. Edit lazy.lua file to enable plugins and comment the lines after disabled_plugins lines like mentioned below:

Address: /home/username/.config/nvim/lua/configs/lazy.lua

  performance = {
    rtp = {
      disabled_plugins = {
        --"2html_plugin",
        --"tohtml",
        --"getscript",
        --"getscriptPlugin",
        --"gzip",
        --"logipat",
        --"netrw",
        --"netrwPlugin",
        --"netrwSettings",
        --"netrwFileHandlers",
        --"matchit",
        --"tar",
        --"tarPlugin",
        --"rrhelper",
        --"spellfile_plugin",
        --"vimball",
        --"vimballPlugin",
        --"zip",
        --"zipPlugin",
        --"tutor",
        --"rplugin",
        --"syntax",
        --"synmenu",
        --"optwin",
        --"compiler",
        --"bugreport",
        --"ftplugin",
     },
    },
  },
  1. Open nvim and install lsp for any language, I am doing for C/C++ and Python:
 :MasonInstall clangd
 :MasonIntall python-lsp-server
 :MasonInstall pyright

In MasonInstall menu, Press U to update and arrow key to scroll and find your required lsp

  1. Update packages using:
 :Lazy update    
 :Lazy sync

Part 2: Integrating ChatGPT into Neovim

Step 1: Get the API Key

  1. Click here and Sign up or Login in for OPENAI Platform.

  2. Click on the setting ⚙️ option and Navigate to API keys.

  3. Click on + Create new secret key and fill some sections to generate your key.

  4. After generating, copy your key.

Step 2: Save into your zsh or bash file

  1. Verify your terminal type by typing following command into your terminal:
echo $0

if it indicates /bin/zsh or zsh then you’re a zsh terminal user, if it indicates /bin/bash or bash then you’re bash terminal user.

  1. If you’re a zsh terminal user, open .zshrc file or if you’re a bash terminal user, open .bashrc file into your home directory and add the following lines:
 export OPENAI_API_KEY="paste_your_api_key_here"
  1. Source the file after making changes and pasting your OPENAI_API_KEY key, by running following command into terminal:
 source .zshrc

or

source .bashrc
  1. Verify your API key:
echo $OPENAI_API_KEY

Step 3: Configuring the init.lua file

Address: /home/username/.config/nvim/init.lua

Locate the following piece of code into init.lua file:

   {
   "NvChad/NvChad",
   lazy = false,
   branch = "v2.5",
   import = "nvchad.plugins",
   },

Then, add the following beautiful text just after it:

     {
    "jackMort/ChatGPT.nvim",
    event = "VeryLazy",
    dependencies = {
      "MunifTanjim/nui.nvim",
      "nvim-lua/plenary.nvim",
      "nvim-telescope/telescope.nvim",
      "folke/trouble.nvim",
    },
      config = function()
      require("chatgpt").setup({
        -- Your ChatGPT.nvim configuration
        api_key_cmd = "echo $OPENAI_API_KEY",

        vim.api.nvim_set_keymap("n", "<C-s>", "<cmd>lua require('chatgpt').submit()<CR>", { noremap = true, silent = true }),-- Submit text in the ChatGPT prompt
        vim.api.nvim_set_keymap("n", "<C-c>", "<cmd>lua require('chatgpt').close()<CR>", { noremap = true, silent = true }), -- Close ChatGPT window
        vim.api.nvim_set_keymap("n", "<C-y>", "<cmd>lua require('chatgpt').yank_last()<CR>", { noremap = true, silent = true }), -- Yank last ChatGPT response
        vim.api.nvim_set_keymap("n", "<C-u>", "<cmd>lua require('chatgpt').scroll_up()<CR>", { noremap = true, silent = true }), -- Scroll up
        vim.api.nvim_set_keymap("n", "<C-d>", "<cmd>lua require('chatgpt').scroll_down()<CR>", { noremap = true, silent = true }), -- Scroll down
        vim.api.nvim_set_keymap("n", "<C-e>", "<cmd>lua vim.api.nvim_screenshot('/home/ramzan/screenshot')<CR>", { noremap = true, silent = true}),
        vim.api.nvim_set_keymap("n", "<C-2>", "<cmd>lua require('chatgpt').goto_next_end()<CR>", { noremap = true, silent = true}),
        vim.api.nvim_set_keymap("n", "<C-w>", "<cmd>lua require('chatgpt').close()<CR>", { noremap = true, silent = true})
      })
    end,
  },

This will automatically set the keybindings which are essential to run ChatGPT into it. Save this file and exit.

Step 4: Final setup

  1. Open Neovim and update the lazy package:
 :Lazy update
 :Lazy sync
  1. Shortcuts to run ChatGPT:
  • Open Neovim
  • Open ChatGPT menu:
:ChatGPT
  • It will open a menu inside our terminal, then give any command to it.
  • Press Esc and then press enter to submit the command.
  • Press Esc and then press ctrl+C or enter :q to exit ChatGPT.
  1. You can further learn neovim Shortcuts by:
 :h nvui

That’s it. Finally the setup is over and chatgpt is working inside our terminal. If any issue persists like Incorrect OPENAI_API_KEY then you just have to source the bash or zsh file source .zshrc or source.bashrc. Happy coding 💟