owenrusk.dev

dotfiles

shell, editor, ssh.

git clone https://owenrusk.dev/dotfiles.git

dotfiles / nvim/lua/owen/autocmds.lua -rw-r--r-- · 1322 bytes

 1 local group = vim.api.nvim_create_augroup("owen", { clear = true })
 2 
 3 local function au(event, opts)
 4   opts.group = group
 5   vim.api.nvim_create_autocmd(event, opts)
 6 end
 7 
 8 -- back to where i was when i left the file
 9 au("BufReadPost", {
10   callback = function(args)
11     local mark = vim.api.nvim_buf_get_mark(args.buf, '"')
12     if mark[1] > 0 and mark[1] <= vim.api.nvim_buf_line_count(args.buf) then
13       pcall(vim.api.nvim_win_set_cursor, 0, mark)
14     end
15   end,
16 })
17 
18 -- two spaces where everyone else uses two spaces
19 au("FileType", {
20   pattern = { "lua", "yaml", "json", "javascript", "typescript", "css", "markdown" },
21   callback = function()
22     vim.opt_local.shiftwidth = 2
23     vim.opt_local.tabstop = 2
24     vim.opt_local.softtabstop = 2
25   end,
26 })
27 
28 -- trailing whitespace goes on save. not in markdown, where two spaces mean a line break. sadly.
29 au("BufWritePre", {
30   callback = function(args)
31     if vim.bo[args.buf].filetype == "markdown" then
32       return
33     end
34     local view = vim.fn.winsaveview()
35     vim.cmd([[keeppatterns %s/\s\+$//e]])
36     vim.fn.winrestview(view)
37   end,
38 })
39 
40 au("FileType", {
41   pattern = "gitcommit",
42   callback = function()
43     vim.opt_local.spell = true
44     vim.opt_local.textwidth = 72
45   end,
46 })
47 
48 au("TextYankPost", {
49   callback = function()
50     vim.hl.on_yank({ timeout = 150 })
51   end,
52 })