Skip to content

Instantly share code, notes, and snippets.

@partrita
Last active July 18, 2024 00:53
Show Gist options
  • Save partrita/566825fb2507b51426a83c70e9102af5 to your computer and use it in GitHub Desktop.
Save partrita/566825fb2507b51426a83c70e9102af5 to your computer and use it in GitHub Desktop.
Getting Started with Neovim Using NvChad

source: https://blog.spoonconsulting.com/getting-started-with-neovim-using-nvchad-a-developers-guide-f97d81e85d60

Setting up Neovim and NvChad

Install Nerd Font

Configure Terminal's fonts

Install Neovim

  • use brew command for macOS and linux.
  • use scoop command for windows

Install NvChard

Play around with NvChard

– Press <Spacebar> + th to open the theme selector. Select a theme and press <ENTER> or <ESC> twice to close it.

  • Press <Ctrl> + n to toggle the file navigator. (If there are no files or folders listed, it means the directory in which you launched Neovim is empty)
  • Press <ENTER> to expand a folder or open a file.
  • Press g? to show a list of mappings for the file navigator. Press q to close the list.
  • Press <Spacebar> + ff to open the Find Files window and press <ESC> twice to close it.
  • Press <Spacebar> + ch to show all the keyboard mappings. Use it to explore more features.
  • You will notice that some mappings start with <leader> which is referring to the Spacebar key.
  • C is for the Ctrl/Control key, A is for the Alt/Option key and S is for the Shift key.

Exploring NvChad’s configuration

On Linux and macOS, you can find the configuration files in ~/.config/nvim/ and on Windows, you can find them in ~\AppData\Local\nvim\.

There are two critical observations:

  1. The files have the extension .lua

What is Lua? Lua is a lightweight, high-level, and straightforward programming language, resembling pseudo-code and making it relatively easy to learn for developers. For these reasons, Neovim allows users to write their configuration files in Lua, and although it is optional to complete this guide, learning basic Lua is highly recommended if you plan to make more changes or tweaks on your own.

  1. There is primarily three key folders: core, plugins and custom All the default NvChad configurations and plugins are stored in the file init.lua (main init.lua), and the files in the core and plugins folders. These files should not be modified directly (Read this section of NvChad's documentation to understand why). Instead, all our modifications will be made inside the custom folder. This is where the magic happens.

Custom folder

There are only 4 important files we need to consider: overrides.lua, lspconfig.lua, mappings.lua and plugins.lua. These 4 files are enough for us to update the language servers, edit the syntax highlighting support, modify keyboard mappings and manage plugins.

Customize Neovim

Language server and syntax highlighting

To customize the language servers and syntax highlighting, we need to update the file custom/overrides.lua. This file consists of two main tables: M.treesitter and M.mason. Treesitter is a plugin that provides syntax highlighting and Mason is another plugin which makes it easy to install language servers.

Install syntax highlighting and language servers

Let’s add syntax highlighting for Python by simply adding python to M.treesitter. Similarly let's add pyre, which is a language server for Python, to M.mason. Check out the full list of supported languages for Treesitter and the list of available language servers for Mason. Your code should now look like this:

local M = {}

M.treesitter = {
  ensure_installed = {
    "vim",
    "lua",
    "html",
    "css",
    "javascript",
    "typescript",
    "tsx",
    "c",
    "markdown",
    "markdown_inline",
    "python", -- Added python syntax highlighting here
  },
  indent = {
    enable = true,
    -- disable = {
    --   "python"
    -- },
  },
}

M.mason = {
  ensure_installed = {
    -- lua stuff
    "lua-language-server",
    "stylua",

    -- web dev stuff
    "css-lsp",
    "html-lsp",
    "typescript-language-server",
    "deno",
    "prettier",

    -- c/cpp stuff
    "clangd",
    "clang-format",

    -- python
    "pyre", -- Added python language server here
  },
}

-- git support in nvimtree
M.nvimtree = {
  git = {
    enable = true,
  },

  renderer = {
    highlight_git = true,
    icons = {
      show = {
        git = true,
      },
    },
  },
}

return M

We now add pyre to the file custom/configs/lspconfig.lua to use the default language server capabilities.

local M = {}

local on_attach = require("plugins.configs.lspconfig").on_attach
local capabilities = require("plugins.configs.lspconfig").capabilities

local lspconfig = require "lspconfig"

-- if you just want default config for the servers then put them in a table
local servers = { "html", "cssls", "pyre" } -- Add pyre language server

for _, lsp in ipairs(servers) do
  lspconfig[lsp].setup {
    on_attach = on_attach,
    capabilities = capabilities,
  }
end
--

Plugins

Plugins essentially expand Neovim’s capabilities by introducing additional features and functionalities. To manage plugins, we need to update the file custom/plugins.lua.

Following the installation instructions from the plugins’ documentation, we need to make the following changes to our plugins.lua file:

local overrides = require("custom.configs.overrides")

---@type NvPluginSpec[]
local plugins = {

  -- Override plugin definition options

  {
    "neovim/nvim-lspconfig",
    dependencies = {
      -- format & linting
      {
        "jose-elias-alvarez/null-ls.nvim",
        config = function()
          require "custom.configs.null-ls"
        end,
      },
    },
    config = function()
      require "plugins.configs.lspconfig"
      require "custom.configs.lspconfig"
    end, -- Override to setup mason-lspconfig
  },

  -- override plugin configs
  {
    "williamboman/mason.nvim",
    opts = overrides.mason
  },

  {
    "nvim-treesitter/nvim-treesitter",
    opts = overrides.treesitter,
  },

  {
    "nvim-tree/nvim-tree.lua",
    opts = overrides.nvimtree,
  },

  -- Install a plugin
  {
    "max397574/better-escape.nvim",
    event = "InsertEnter",
    config = function()
      require("better_escape").setup()
    end,
  },

  -- lightspeed
  {
    "ggandor/lightspeed.nvim",
    event = 'VeryLazy',
  },

  -- nvim-surround
  {
    "kylechui/nvim-surround",
    version = "*", -- Use for stability; omit to use `main` branch for the latest features
    event = "VeryLazy",
    config = function()
        require("nvim-surround").setup({})
    end
  },  
  
  -- To make a plugin not be loaded
  -- {
  --   "NvChad/nvim-colorizer.lua",
  --   enabled = false
  -- },

  -- All NvChad plugins are lazy-loaded by default
  -- For a plugin to be loaded, you will need to set either `ft`, `cmd`, `keys`, `event`, or set `lazy = false`
  -- If you want a plugin to load on startup, add `lazy = false` to a plugin spec, for example
  -- {
  --   "mg979/vim-visual-multi",
  --   lazy = false,
  -- }
}

return plugins
call plug#begin()
Plug 'preservim/NERDTree'
Plug 'junegunn/fzf'
Plug 'catppuccin/nvim', { 'as': 'catppuccin' }
Plug 'itchyny/lightline.vim'
call plug#end()
" Shortcut configs
nmap <C-n> :NERDTreeToggle<CR>
" Theme config
colorscheme catppuccin-latte " catppuccin-latte, catppuccin-frappe, catppuccin-macchiato, catppuccin-mocha
let g:lightline = {'colorscheme': 'catppuccin'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment