summaryrefslogtreecommitdiffstats
path: root/lua/core/auto.lua
blob: 8206ad612c26068ceecd23e4a6c22e8ca6e68502 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
local function auto(event, opts)
  a.nvim_create_autocmd(event, opts)
end

a.nvim_create_augroup('bufcheck', {clear = true})

auto('TextYankPost', { -- highlight yanks
  group = 'bufcheck',
  pattern = '*',
  callback = function()
    vim.highlight.on_yank{ timeout = 250 }
  end
})

auto('FileType', { -- start git messages in insert mode
  group = 'bufcheck',
  pattern = { 'gitcommit', 'gitrebase', },
  command = 'startinsert | 1'
})

auto('BufRead', { -- return to last place
  pattern = '*',
  command = [[call setpos(".", getpos("'\""))]]
})

auto('TermOpen', { -- start terminal in insert mode
  group    = 'bufcheck',
  pattern  = '*',
  callback = function()
    vim.cmd('startinsert | set winfixheight')
    o.winfixheight = true
    o.cmdheight = 0
  end
})

auto('TermClose', { -- close terminal buffers after shell dies
  group    = 'bufcheck',
  pattern  = 'term://*',
  callback = function()
    vim.cmd('call nvim_input("<CR>")')
    o.cmdheight = 1
  end
})

auto('InsertEnter', { -- toggle things when entering insert mode
  group = 'bufcheck',
  callback = function()
    o.colorcolumn = { 80 }
  end
})

auto('InsertLeave', { -- toggle things when exiting insert mode
  group = 'bufcheck',
  callback = function()
    o.colorcolumn = { 0 }
  end
})

auto('BufWritePre', { -- make dirs when they don't exist
  pattern = '*',
  group = vim.api.nvim_create_augroup('auto_create_dir', { clear = true }),
  callback = function(ctx)
    local dir = vim.fn.fnamemodify(ctx.file, ':p:h')
    vim.fn.mkdir(dir, 'p')
  end
})