xonecas.io
56101
Df

dotfiles / files

dotfiles / .config / nvim / init.lua

7.1 KB
1-- Bootstrap lazy.nvim
2local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
3if not (vim.uv or vim.loop).fs_stat(lazypath) then
4  local lazyrepo = "https://github.com/folke/lazy.nvim.git"
5  local out = vim.fn.system({ "git", "clone", "--filter=blob:none", "--branch=stable", lazyrepo, lazypath })
6  if vim.v.shell_error ~= 0 then
7    vim.api.nvim_echo({
8      { "Failed to clone lazy.nvim:\n", "ErrorMsg" },
9      { out, "WarningMsg" },
10      { "\nPress any key to exit..." },
11    }, true, {})
12    vim.fn.getchar()
13    os.exit(1)
14  end
15end
16vim.opt.rtp:prepend(lazypath)
17
18-- Setup lazy.nvim
19require("lazy").setup({
20  spec = {
21    -- Theme
22    {
23      "kungfusheep/mfd.nvim",
24      lazy = false,
25      priority = 1000,
26      config = function()
27        vim.cmd.colorscheme('mfd-flir-rh')
28      end
29    },
30
31    -- Git signs
32    {
33      "lewis6991/gitsigns.nvim",
34      opts = {},
35    },
36
37    -- File explorer
38    {
39      'nvim-tree/nvim-tree.lua',
40      dependencies = { 'nvim-tree/nvim-web-devicons' },
41      cmd = 'NvimTreeToggle',
42      opts = {
43        git = { enable = true, ignore = true }, -- respect .gitignore
44        filters = { dotfiles = false }, -- show dot files
45      },
46    },
47
48    -- Fuzzy Finder
49    {
50      'nvim-telescope/telescope.nvim',
51      dependencies = {
52        'nvim-lua/plenary.nvim',
53        { 'nvim-telescope/telescope-fzf-native.nvim', build = 'make' },
54      },
55      cmd = 'Telescope',
56      opts = {
57        defaults = {
58          file_ignore_patterns = {
59            '^%.git/',
60            '^node_modules/',
61          },
62        },
63        pickers = {
64          find_files = {
65            hidden = true,
66            no_ignore = false,
67          },
68        },
69      },
70    },
71
72    -- Tree-sitter syntax highlighting
73    {
74      'nvim-treesitter/nvim-treesitter',
75      lazy = false,
76      build = ':TSUpdate',
77      config = function()
78        local parsers = {
79          'bash',
80          'css',
81          'go',
82          'html',
83          'javascript',
84          'json',
85          'lua',
86          'markdown',
87          'markdown_inline',
88          'python',
89          'query',
90          'tsx',
91          'typescript',
92          'vim',
93          'vimdoc',
94          'zig',
95        }
96
97        require('nvim-treesitter').install(parsers)
98
99        vim.api.nvim_create_autocmd('FileType', {
100          callback = function(args)
101            pcall(vim.treesitter.start, args.buf)
102          end,
103        })
104      end,
105    },
106  },
107
108  -- Configure any other settings here. See the documentation for more details.
109  -- disable automatic plugin update notifications on startup
110  checker = { enabled = false },
111})
112
113-- Basic settings
114vim.opt.relativenumber = true
115vim.opt.number = true
116vim.opt.ignorecase = true
117vim.opt.smartcase = true
118vim.opt.smartindent = true
119vim.opt.cinkeys:remove('0#') -- Prevent auto-unindenting lines that start with '#' (for Python comments, preprocessor directives)
120vim.opt.linebreak = true
121vim.opt.swapfile = false
122vim.opt.backup = false
123vim.opt.writebackup = false
124vim.opt.termguicolors = true
125
126-- Hide mode display and partial keybind. Reclaim the space.
127vim.opt.showmode = false
128vim.opt.showcmd = false
129vim.opt.cmdheight = 0
130
131-- Indentation settings
132vim.opt.expandtab = true
133vim.opt.tabstop = 2
134vim.opt.shiftwidth = 2
135vim.opt.softtabstop = 2
136
137-- Set leader key to space (must be set before lazy.nvim)
138vim.g.mapleader = ' '
139vim.g.maplocalleader = ' '
140
141-- Keymaps
142local keymap = vim.keymap.set
143local opts = { noremap = true, silent = true }
144
145-- My keybindings
146keymap('n', '<leader>h', ':nohlsearch<CR>', opts)
147keymap('n', '<leader>q', ':qall<CR>', { noremap = true, silent = true, desc = 'Quit all' })
148keymap('n', '<leader>Q', function()
149  if vim.fn.winnr('$') > 1 then
150    vim.cmd.close()
151  else
152    vim.cmd.bdelete()
153  end
154end, { noremap = true, silent = true, desc = 'Close focused buffer/split' })
155keymap('n', '<leader>s', ':write<CR>', { noremap = true, silent = true, desc = 'Save buffer' })
156keymap('n', '<leader>S', ':wall<CR>', { noremap = true, silent = true, desc = 'Save all buffers' })
157keymap('n', '<leader>d', function()
158  vim.diagnostic.setqflist({ open = true })
159end, { desc = 'Open diagnostics quickfix list' })
160
161-- Plugin keybinds
162keymap('n', '<leader>f', ':Telescope grep_string<CR>', opts)
163keymap('n', '<leader>g', ':Telescope live_grep<CR>', opts)
164keymap('n', '<leader>o', ':Telescope find_files<CR>', opts)
165keymap('n', '<leader>b', ':Telescope buffers<CR>', opts)
166keymap('n', '<leader>e', ':NvimTreeToggle<CR>', { noremap = true, silent = true, desc = 'Toggle file explorer' })
167
168-- Movement remaps
169keymap('n', 'j', 'gj', { noremap = true })
170keymap('n', 'k', 'gk', { noremap = true })
171
172-- Command aliases
173vim.api.nvim_create_user_command('Q', 'q', {})
174vim.api.nvim_create_user_command('W', 'w', {})
175
176-- Statusline
177local mode_groups = {
178  { 'NORMAL', 'String' },
179  { 'INSERT', 'Constant' },
180  { 'VISUAL', 'Special' },
181  { 'REPLACE', 'ErrorMsg' },
182  { 'COMMAND', 'WarningMsg' },
183  { 'TERMINAL', 'Title' },
184}
185local mode_keys = { n = 1, i = 2, v = 3, R = 4, c = 5, t = 6 }
186local git_groups = {
187  { 'StatusGitAdd', '+', 'DiffAdd DiagnosticOk String' },
188  { 'StatusGitChange', '~', 'DiffChange DiagnosticWarn Constant' },
189  { 'StatusGitDelete', '-', 'DiffDelete DiagnosticError ErrorMsg' },
190}
191
192local function apply_theme()
193  local function fg(names)
194    for name in names:gmatch('%S+') do
195      local color = vim.api.nvim_get_hl(0, { name = name, link = false }).fg
196      if color then return color end
197    end
198  end
199  for _, m in ipairs(mode_groups) do
200    local color = fg(m[2])
201    if color then vim.api.nvim_set_hl(0, 'StatusMode' .. m[1], { fg = color, bold = true }) end
202  end
203  for _, g in ipairs(git_groups) do
204    local color = fg(g[3])
205    if color then vim.api.nvim_set_hl(0, g[1], { fg = color, bold = true }) end
206  end
207  local dir = fg('Directory')
208  if dir then vim.api.nvim_set_hl(0, 'StatusFile', { fg = dir }) end
209  -- match the statusline background to the Normal background
210  local bg = vim.api.nvim_get_hl(0, { name = 'Normal', link = false }).bg
211  for _, g in ipairs({ 'StatusLine', 'StatusLineNC' }) do
212    local hl = vim.api.nvim_get_hl(0, { name = g, link = false })
213    hl.bg, hl.cterm, hl.ctermbg = bg, nil, nil
214    vim.api.nvim_set_hl(0, g, hl)
215  end
216end
217
218apply_theme()
219vim.api.nvim_create_autocmd('ColorScheme', { callback = apply_theme })
220
221local function git_info()
222  local d = vim.b.gitsigns_status_dict
223  if not (d and d.head) then return '' end
224  local parts, counts = { ' ' .. d.head }, { d.added, d.changed, d.removed }
225  for i, g in ipairs(git_groups) do
226    if (counts[i] or 0) > 0 then
227      parts[#parts + 1] = ('%%#%s#%s%d%%#StatusLine#'):format(g[1], g[2], counts[i])
228    end
229  end
230  return table.concat(parts, ' ')
231end
232
233local function statusline()
234  local m = vim.api.nvim_get_mode().mode:sub(1, 1)
235  if m == '\22' or m == 'V' or m == 's' or m == 'S' then m = 'v' end
236  local mode = mode_groups[mode_keys[m] or 1][1]
237  return table.concat({
238    '%#StatusMode' .. mode .. '# ' .. mode .. ' ',
239    '%#StatusLine# %f%m%=',
240    git_info(),
241    ' %#StatusFile#' .. (vim.bo.filetype ~= '' and vim.bo.filetype or 'text'),
242    '%#StatusLine# %p%% ',
243  })
244end
245
246_G.Statusline = statusline
247vim.o.statusline = '%!v:lua.Statusline()'