summaryrefslogtreecommitdiff
path: root/config/nvim/init.lua
diff options
context:
space:
mode:
authorSuleyman Farajli <suleyman@farajli.net>2025-06-19 01:07:18 +0400
committerSuleyman Farajli <suleyman@farajli.net>2025-06-19 01:07:18 +0400
commit09d70e1bc0bf1e5c54b3faf2918a3e14c4fc4cfc (patch)
tree2d8990fde64d5a81e65605447657c427ac4f19d4 /config/nvim/init.lua
parent226b3c808393b5e91e3eb6c6c591f5bc5e5d9775 (diff)
refactor: improve Neovim config for better readability and functionality
- Switch to Lua API options for setting vim options instead of vim.cmd - Add autocmd to trim trailing whitespace on save (except markdown) - Disable auto-comments via autocmd for all filetypes - Enhance and standardize key mappings with noremap and silent flags - Clean up plugin config indentation and comments
Diffstat (limited to 'config/nvim/init.lua')
-rw-r--r--config/nvim/init.lua83
1 files changed, 60 insertions, 23 deletions
diff --git a/config/nvim/init.lua b/config/nvim/init.lua
index df90607..ea8a3d2 100644
--- a/config/nvim/init.lua
+++ b/config/nvim/init.lua
@@ -1,15 +1,14 @@
require("plugins")
-vim.cmd("let mapleader = ' '")
-vim.cmd("set relativenumber")
-vim.cmd("set number")
-vim.cmd("colorscheme duskfox")
-vim.cmd("set nowrap")
-vim.cmd("set clipboard=unnamedplus") -- Use system clipboard
-vim.cmd("set background=dark")
-vim.cmd("set shm+=I") -- Disable intro message
-vim.cmd("autocmd FileType * setlocal formatoptions-=c formatoptions-=r formatoptions-=o") -- Disable auto-comment
--- vim.cmd("set list") -- Show spaces tabs etc.
+vim.g.mapleader = ' '
+vim.opt.background = "dark"
+vim.opt.clipboard = "unnamedplus" -- Use system clipboard
+vim.opt.list = true -- Show spaces, tabs, etc.
+vim.opt.number = true
+vim.opt.relativenumber = true
+vim.opt.shortmess:append("I") -- Disable intro message
+vim.opt.wrap = false
+vim.cmd.colorscheme("duskfox")
vim.opt.fillchars = {
vert = "|",
@@ -22,16 +21,54 @@ vim.opt.fillchars = {
foldclose = ">",
}
-vim.keymap.set('n', '<leader><leader>', ':w!<CR>')
-vim.keymap.set('n', '<leader>q', ':wq!<CR>')
-vim.keymap.set('n', '<C-t>', ':tabnew<CR>')
-vim.keymap.set('n', 'J', ':tabn<CR>')
-vim.keymap.set('n', 'K', ':tabp<CR>')
-vim.keymap.set('n', '<C-n>', ':vsplit<CR>')
-vim.keymap.set('n', 'H', ':wincmd h<CR>')
-vim.keymap.set('n', 'L', ':wincmd l<CR>')
-vim.keymap.set('n', '<leader>e', ':Neotree toggle right<CR>')
-vim.keymap.set('n', '<leader>o', '<C-W>w')
-vim.keymap.set('i', '<C-h>', '<Left>')
-vim.keymap.set('i', '<C-l>', '<Right>')
-vim.keymap.set('i', '<C-space>', '<ESC>')
+-- Remove trailing whitespace on all lines before saving, excluding markdown files
+vim.api.nvim_create_autocmd("BufWritePre", {
+ pattern = "*",
+ callback = function()
+ if vim.bo.filetype == "markdown" then
+ return
+ end
+ vim.cmd([[%s/\s\+$//e]])
+ end,
+})
+
+-- Disable auto-comment
+vim.api.nvim_create_autocmd("FileType", {
+ pattern = "*",
+ callback = function()
+ vim.opt_local.formatoptions:remove({ "c", "r", "o" })
+ end,
+})
+
+
+-- NOTE: On some terminal emulators, the keybinds
+-- <C-j> and <C-k> don't work either in normal or insert mode.
+
+-- Tabs
+vim.keymap.set('n', '<C-t>', '<cmd>tabnew<CR>' , { noremap = true, silent = true })
+vim.keymap.set('n', 'H' , '<cmd>wincmd h<CR>', { noremap = true, silent = true })
+vim.keymap.set('n', 'J' , '<cmd>tabn<CR>' , { noremap = true, silent = true })
+vim.keymap.set('n', 'K' , '<cmd>tabp<CR>' , { noremap = true, silent = true })
+vim.keymap.set('n', 'L' , '<cmd>wincmd l<CR>', { noremap = true, silent = true })
+
+-- Windows
+vim.keymap.set('n', '<C-Down>' , '<cmd>split<CR>' , { noremap = true, silent = true })
+vim.keymap.set('n', '<C-Right>', '<cmd>vsplit<CR>', { noremap = true, silent = true })
+vim.keymap.set('n', '<C-h>' , '<C-w>h' , { noremap = true })
+vim.keymap.set('n', '<C-j>' , '<C-w>j' , { noremap = true })
+vim.keymap.set('n', '<C-k>' , '<C-w>k' , { noremap = true })
+vim.keymap.set('n', '<C-l>' , '<C-w>l' , { noremap = true })
+
+-- Imitate normal mode in insert mode
+vim.keymap.set('i', '<C-h>' , '<Left>' , { noremap = true })
+vim.keymap.set('i', '<C-l>' , '<Right>', { noremap = true })
+vim.keymap.set('i', '<C-j>' , '<Down>' , { noremap = true })
+vim.keymap.set('i', '<C-k>' , '<Up>' , { noremap = true })
+vim.keymap.set('i', '<C-space>', '<ESC>' , { noremap = true })
+
+-- Plugins
+vim.keymap.set('n', '<leader>e', '<cmd>Neotree toggle right<CR>', { noremap = true, silent = true })
+
+-- Other
+vim.keymap.set('n', '<leader><leader>', '<cmd>w!<CR>' , { noremap = true, silent = true })
+vim.keymap.set('n', '<leader>q' , '<cmd>wq!<CR>', { noremap = true, silent = true })