nix-configs

Personal NixOS and home-manager configuration files
Log | Files | Refs

neovim.nix (2531B)


      1 {...}: {
      2   programs.neovim = {
      3     enable = true;
      4     defaultEditor = true;
      5     extraConfig = ''
      6       set smartcase
      7       set nocompatible
      8       let mapleader =","
      9       set backspace=indent,eol,start
     10       set clipboard+=unnamedplus
     11       set ignorecase
     12       set linebreak
     13       set mouse=a
     14       set nohlsearch
     15       set number
     16       set title
     17       set titlestring=%f\ %m
     18       set whichwrap+=<,>,h,l,[,]
     19       set list listchars=nbsp:¬,tab:»·,trail:·,extends:>
     20       set shiftwidth=2
     21       set softtabstop=2
     22       set tabstop=2
     23       syntax on
     24       map <C-H> <C-W>
     25       map <C-Del> X<Esc>ce<del>
     26       map <F8> :setlocal spell! spelllang=en_gb<CR>
     27       xnoremap <A-z> :s/^.<CR>gv " alt-z removes first character of line
     28       nnoremap <A-z> 0x " alt-z in normal mode removes first char
     29       :iab <expr> _date strftime("%F")
     30       :iab <expr> _time strftime("%H:%M")
     31       :iab <expr> _stamp strftime("%F\T%H:%M:00")
     32       autocmd BufWritePre * %s/\s\+$//e
     33       autocmd BufWritepre * %s/\n\+\%$//e
     34       autocmd FileType nix setlocal tabstop=2 shiftwidth=2 expandtab
     35       autocmd BufWritePre *.nix %s/\s\+$//e | retab
     36       autocmd BufWritePost *.nix silent! execute '!alejandra -qq %' | edit
     37     '';
     38     extraLuaConfig = ''
     39       -- Define and register :NixSortIncludes to sort selected Nix includes
     40       vim.api.nvim_create_user_command('NixSortIncludes', function()
     41         -- Get the start and end lines of the visual selection
     42         local start_line = vim.fn.line("'<") - 1
     43         local end_line = vim.fn.line("'>")
     44 
     45         -- Get the lines from the current buffer
     46         local lines = vim.api.nvim_buf_get_lines(0, start_line, end_line, false)
     47 
     48         -- Function to extract path for sorting
     49         local function extract_path(line)
     50           local cleaned = line
     51           cleaned = cleaned:gsub("^%s*%(", "")           -- remove leading '('
     52           cleaned = cleaned:gsub("^import%s+", "")       -- remove 'import'
     53           cleaned = cleaned:gsub("%s+%b{}", "")          -- remove '{...}'
     54           cleaned = cleaned:gsub("^%s+", "")             -- trim leading spaces
     55           cleaned = cleaned:gsub("%)+%s*$", "")          -- remove trailing ')'
     56           return cleaned
     57         end
     58 
     59         -- Sort lines using the extracted path
     60         table.sort(lines, function(a, b)
     61           return extract_path(a) < extract_path(b)
     62         end)
     63 
     64         -- Replace lines in the buffer
     65         vim.api.nvim_buf_set_lines(0, start_line, end_line, false, lines)
     66       end, { range = true })
     67     '';
     68   };
     69 }