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
- Linux/MacOS Users
rm -rf ~/.config/nvim
rm -rf ~/.local/state/nvim
rm -rf ~/.local/share/nvim
- 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
- 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
- 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.
- Enter editing mode:
i
- Escape the editing mode to save or exit:
Press the escape button
- Save file:
:w
- Exit with saving:
:wq
- Exit without saving:
:q!
Step 4: Essential processes after installing
- Run following command into nvim after lazy.nvim finishes downloading plugins:
:MasonInstallAll
- Navigate to nvim directory in
.config
folder to the remove.git
folder.
rm -rf /home/username/.config/nvim/.git
- Learn customization of UI and base46 by using following command inside nvim:
:h nvui
- Update the packages:
:Lazy sync
Step 5: Setting up LSP Configurations
You have to add required language server protocols to improve your coding experience.
- Ensure that
lazy_config
is set up correctly aftervim.otp.rtp
in the filehome/username/.config/nvim/init.lua
, if not, then add the following content toinit.lua
file aftervim.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)
- 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
- 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
})
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",
},
},
},
- 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
- Update packages using:
:Lazy update
:Lazy sync
Part 2: Integrating ChatGPT into Neovim
Step 1: Get the API Key
Click here and Sign up or Login in for OPENAI Platform.
Click on the setting ⚙️ option and Navigate to API keys.
Click on
+ Create new secret key
and fill some sections to generate your key.After generating, copy your key.
Step 2: Save into your zsh or bash file
- Verify your terminal type by typing following command into your terminal:
echo $0
if it indicates
/bin/zsh
orzsh
then you’re a zsh terminal user, if it indicates/bin/bash
orbash
then you’re bash terminal user.
- 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"
- Source the file after making changes and pasting your OPENAI_API_KEY key, by running following command into terminal:
source .zshrc
or
source .bashrc
- 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
- Open Neovim and update the lazy package:
:Lazy update
:Lazy sync
- 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 pressenter
to submit the command. - Press
Esc
and then pressctrl+C
or enter:q
to exit ChatGPT.
- 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 filesource .zshrc
orsource.bashrc
. Happy coding 💟