summaryrefslogtreecommitdiffstats
path: root/dotfiles
diff options
context:
space:
mode:
authorMinijackson <minijackson@riseup.net>2022-05-03 09:10:59 +0200
committerMinijackson <minijackson@riseup.net>2022-05-03 09:10:59 +0200
commite8e1563aad6d3f22f8c87f0d3c19b001eb14b714 (patch)
tree42a034df8283d41ec603201b524394163af0b32e /dotfiles
parentec3c701d84eb6cb48ceeb2365c1733f998cc87aa (diff)
downloadnixos-config-reborn-e8e1563aad6d3f22f8c87f0d3c19b001eb14b714.tar.gz
nixos-config-reborn-e8e1563aad6d3f22f8c87f0d3c19b001eb14b714.zip
vim: convert autocmd to Lua, better restore position on open
Diffstat (limited to 'dotfiles')
-rw-r--r--dotfiles/vim.lua71
1 files changed, 60 insertions, 11 deletions
diff --git a/dotfiles/vim.lua b/dotfiles/vim.lua
index 619cb9b..95ba8a1 100644
--- a/dotfiles/vim.lua
+++ b/dotfiles/vim.lua
@@ -3,9 +3,9 @@
3 3
4vim.o.undofile = true 4vim.o.undofile = true
5vim.o.backup = true 5vim.o.backup = true
6vim.opt.backupdir:remove(".") 6vim.opt.backupdir:remove "."
7 7
8vim.opt.shortmess:append("c") 8vim.opt.shortmess:append "c"
9 9
10vim.o.mouse = "a" 10vim.o.mouse = "a"
11 11
@@ -59,15 +59,56 @@ vim.g.mapleader = ";"
59 59
60-- From neovim#14420 60-- From neovim#14420
61-- Restores the position of previously opened files 61-- Restores the position of previously opened files
62vim.cmd [[
63 augroup LastCursorPos
64 autocmd!
65 autocmd BufReadPost * if @% !~# "\.git[\/\\]COMMIT_EDITMSG$" && line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g`\"" | endif
66 augroup end
67]]
68 62
69-- Highlight yanked text 63-- Inspired by: https://github.com/ethanholz/nvim-lastplace
70vim.cmd "autocmd TextYankPost * silent! lua vim.highlight.on_yank()" 64
65local last_cursor_pos_augroup = vim.api.nvim_create_augroup("LastCursorPos", {})
66vim.api.nvim_create_autocmd("BufReadPost", {
67 group = last_cursor_pos_augroup,
68 desc = "Restore the position of previously opened files",
69 callback = function(opts)
70 if vim.tbl_contains({ "quickfix", "nofile", "help" }, vim.bo.buftype) then
71 return
72 end
73
74 if vim.tbl_contains({ "gitcommit", "gitrebase", "svn", "hgcommit" }, vim.bo.filetype) then
75 return
76 end
77
78 -- If a line has already been specified on the command line, we are done
79 -- nvim file +num
80 if vim.fn.line "." > 1 then
81 return
82 end
83
84 -- If the last line is set and the less than the last line in the buffer
85 if vim.fn.line [['"]] > 0 and vim.fn.line [['"]] <= vim.fn.line "$" then
86 if vim.fn.line "w$" == vim.fn.line "$" then
87 -- if the last line in the current buffer is also the last line visible
88 -- in this window
89 vim.cmd [[normal! g`"]]
90 elseif vim.fn.line "$" - vim.fn.line [['"]] > ((vim.fn.line "w$" - vim.fn.line "w0") / 2) - 1 then
91 -- if we're not at the bottom of the file, center the cursor on the
92 -- screen after we make the jump
93 vim.cmd [[normal! g`"zz]]
94 else
95 -- otherwise, show as much context as we can by jumping to the end of
96 -- the file and then to the mark. If we pressed zz here, there would be
97 -- blank lines at the bottom of the screen. We intentionally leave the
98 -- last line blank by pressing <c-e> so the user has a clue that they
99 -- are near the end of the file.
100 vim.cmd [[normal! G'"<c-e>]]
101 end
102 end
103 end,
104})
105
106vim.api.nvim_create_autocmd("TextYankPost", {
107 desc = "Highlight yanked text",
108 callback = function(opts)
109 vim.highlight.on_yank()
110 end,
111})
71 112
72vim.g.tex_flavor = "latex" 113vim.g.tex_flavor = "latex"
73 114
@@ -211,6 +252,7 @@ require("nvim-treesitter.configs").setup {
211 252
212vim.o.foldmethod = "expr" 253vim.o.foldmethod = "expr"
213vim.o.foldexpr = "nvim_treesitter#foldexpr()" 254vim.o.foldexpr = "nvim_treesitter#foldexpr()"
255vim.opt.foldopen = { "all" }
214 256
215vim.api.nvim_set_hl(0, "TSCurrentScope", { 257vim.api.nvim_set_hl(0, "TSCurrentScope", {
216 bg = vim.g.current_gruvbox_colors.dark0_soft[1], 258 bg = vim.g.current_gruvbox_colors.dark0_soft[1],
@@ -337,7 +379,14 @@ vim.keymap.set(
337 379
338-- Text yanked into the "t register gets copied using OSC52 escape sequences 380-- Text yanked into the "t register gets copied using OSC52 escape sequences
339-- (e.g. goes through SSH) 381-- (e.g. goes through SSH)
340vim.cmd "autocmd TextYankPost * if v:event.regname is 't' | OSCYankReg t | endif" 382vim.api.nvim_create_autocmd("TextYankPost", {
383 desc = "Setup for OSCYank",
384 callback = function(opts)
385 if vim.v.event.regname == "t" then
386 vim.cmd [[OSCYankReg t]]
387 end
388 end,
389})
341 390
342-- Diffview 391-- Diffview
343 392