Skip to main content
  1. Blog/

Neovim Setup

·900 words·5 mins
Neovim Setup Lua
Table of Contents

You can find my whole source code for my config here

I have been switch to Neovim from Vscode as my main editor for daily work for a while. I found it quite interesting and powerful of customizing my needed.(still on the way of learning and simplifying my config). I start with Neovim kickstart to configure my personal setting. Also I watch many video from theprimeagen and TJ for learning how to set up and use it. I tried several different distributions, such as Lunarvim, Lazynvim, Nvchad,…etc. In the end I land on customizing my own configuration since it is much more intuitive to me.

Here is how I setup my Neovim on both my mac and windows. With the package manger Lazy.nvim to manage my plugins.

This is my current config structure:

─ nvim   └── lua
      ├── config
      │   ├── auto-session.lua
      │   ├── copilot.lua
      │   ├── indentscope.lua
      │   ├── lualine.lua
      │   ├── nord.lua
      │   ├── nvim-tree.lua
      │   ├── obsidian.lua
      │   ├── oil.lua
      │   ├── rose-pine.lua
      │   ├── telescope.lua
      │   ├── transparent.lua
      │   ├── treesitter.lua
      │   └── trouble.lua
      ├── core
      │   ├── autocmds.lua
      │   ├── globals.lua
      │   ├── init.lua
      │   ├── keymaps.lua
      │   ├── lazy.lua
      │   └── options.lua
      ├── lsp
      │   ├── cmp.lua
      │   ├── dap-init.lua
      │   ├── dap-server.lua
      │   ├── lsp-formater.lua
      │   ├── lsp-init.lua
      │   ├── lsp-linter.lua
      │   ├── lsp-server.lua
      │   └── none-ls.lua
      └── plugins
          ├── lsp.lua
          └── plugins.lua

Core setting
#

For the main setting folder core contains the basic setting for the editor, such as autocmds.lua, globals.lua, init.lua, keymaps.lua, lazy.lua, options.lua.

  • autocmds.lua contains the setting for the autocmds for specific file type.
  • globals.lua contains the global setting for the editor.
  • init.lua main entry point for the editor.
  • keymaps.lua contains the keymaps.
  • lazy.lua contains the setting for the Lazy.nvim package manager.

Plugins configuration
#

The plugins folder contains the setting for the plugins and LSP I use. It is always in working progress to fit by my needs, because I tried to keep it simple and light.

As the plugins that load the config from config folder. Each file in the config folder is the setting for the specific plugin. You can find one example below:

-- plugins.lua
  {
    "nvim-telescope/telescope.nvim",
    event = "VimEnter",
    branch = "0.1.x",
    dependencies = {
      "nvim-lua/plenary.nvim",
      {
        "nvim-telescope/telescope-fzf-native.nvim",
        build = "make",
        cond = function()
          return vim.fn.executable "make" == 1
        end,
      },
      { "nvim-tree/nvim-web-devicons" },
      { "nvim-telescope/telescope-ui-select.nvim" },
      { "nvim-telescope/telescope-frecency.nvim" },
    },
    config = function()
      require "config.telescope"
    end,
  },

and it load the setting from config/telescope.lua:

-- config/telescope.lua

local builtin = require "telescope.builtin"
local utils = require "telescope.utils"
local actions = require "telescope.actions"

-- setup
require("telescope").setup {
  defaults = {
    vimgrep_arguments = {
      "rg",
      "-L",
      "--hidden",
      "--glob",
      "!**/.git/*",
      "--smart-case",
      "--with-filename",
      "--column",

      "--line-number",
    },
    -- path_display = "smart",
    initial_mode = "insert",
    layout_config = {
      horizontal = {
        prompt_position = "bottom",
        preview_width = 0.65,
      },
      vertical = {
        mirror = false,
      },
      width = 0.85,
      height = 0.90,
      preview_cutoff = 80,
    },
    mappings = {
      n = {
        ["<C-p>"] = actions.move_selection_previous, -- move to prev result
        ["<C-n>"] = actions.move_selection_next, -- move to next result
        ["<C-d>"] = actions.delete_buffer,
        ["<C-q>"] = actions.send_selected_to_qflist + actions.open_qflist,
        ["<C-c>"] = actions.close,
      },
    },
  },
  pickers = {
    find_files = {
      hidden = true,
      find_command = {
        "rg",
        "-L",
        "--files",
        "--hidden",
        "--glob",
        "!**/.git/*",
      },
    },
    grep_string = {
      additional_args = { "--hidden" },
    },
    live_grep = {
      additional_args = { "--hidden" },
    },
  },
  extensions = {
    fzf = {
      fuzzy = true, -- false will only do exact matching
      override_generic_sorter = true, -- override the generic sorter
      override_file_sorter = true, -- override the file sorter
      case_mode = "smart_case", -- or "ignore_case" or "respect_case"
    },
    ["ui-select"] = {
      require("telescope.themes").get_dropdown {},
    },
  },
}

pcall(require("telescope").load_extension, "fzf")
pcall(require("telescope").load_extension, "frecency")
pcall(require("telescope").load_extension "ui-select")

-- keymaps
vim.keymap.set("n", "<leader>f", builtin.find_files, { desc = "Find Files" })
vim.keymap.set("n", "<leader>sk", builtin.keymaps, { desc = "Search Keymaps" })
vim.keymap.set("n", "<leader><leader>", builtin.buffers, { desc = "Find file in buffer" })
vim.keymap.set("n", "<space>ff", function() builtin.find_files { cwd = utils.buffer_dir() } end, { desc = "file browser in buffer" })
vim.keymap.set("n", "<leader>fl", function() builtin.live_grep { grep_open_files = true } end, { desc = "Find live grep" })

vim.keymap.set("n", "<leader>fs", function() builtin.grep_string { search = vim.fn.input "Grep > " } end, { desc = "Grep search" })

-- word search
vim.keymap.set("n", "<leader>fw", function() local word = vim.fn.expand "<cword>" builtin.grep_string { search = word }
end, { desc = "word search" })
vim.keymap.set("n", "<leader>fW", function() local word = vim.fn.expand "<cWORD>" builtin.grep_string { search = word } end, { desc = "cWord search" }),

Here you can find the list of plugins in my config, because I will keep updating by daily use that why put it in github readme.

Language Server Protocol(LSP)
#

In my daily work, I mainly programming in c++/c, python, and some web development, DevOps work with go. Here is the structure of it: it starts from lsp-init.lua and it load the setting from lsp-server.lua, lsp-linter.lua, lsp-formater for auto-install. Since null-ls is no longer update, I switch to none-ls as a server for linting and formatting. I have also set up the debugger but somehow i do not use too much.

Here is the plugin that used for LSP:

LSP and Autocompletion

Extra LSP - clangd_extension

Debuger - nvim-dap

Usage
#

if you want to use my config, you can find it here on my repo. Have fun with it and feel free to ask me any question.