aboutsummaryrefslogtreecommitdiff
path: root/Common/nvim/lua/basic.lua
blob: e731da89f7886a17582fb0b899183c3f8a16ef7f (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
--[[
General
--]]

-- Lines of history
vim.opt.history = 500
-- Line numbers
vim.opt.nu = true

-- Autoread when file is changed outside vim
vim.opt.autoread = true

-- enable filetype plugins
vim.cmd [[
  filetype plugin on
  filetype indent on
]]

-- Use a mapleader
--vim.opt.mapleader = ","
vim.g.mapleader   = ","

-- Fast saving
map('n', '<leader>w', ':w!<cr>', silentnoremap)

-- Convenient sudo saving of file
vim.api.nvim_create_user_command(
 'W', 'w !sudo tee % > /dev/null',
 {bang=true, desc='Save file using sudo'}
)

--[[
VIM User interface
--]]

-- Set 3 lines to the cursor - when moving vertically using j/k
vim.opt.so = 3

-- set WildMenu
--vim.opt.wildmenu = true

-- Ignore compiled files
vim.opt.wildignore = '*.o,*~,*.pyc'
vim.opt.wildignore:append('*/.git/*,*/.hg/*,*/.svn/*,*/.DS_Store')

-- Always show current position
--vim.opt.ruler = true

-- Hide buffers when they are abandoned
vim.opt.hid = true

-- Make backspace function normally
vim.opt.backspace = 'eol,start,indent'
vim.opt.whichwrap:append('<,>,h,l')

-- Ignore case when searching
vim.opt.ignorecase = true

-- Be smart about cases when searching
-- If search has uppercases, then we want to respect case
vim.opt.smartcase = true

-- Highlight search results
vim.opt.hlsearch = true

-- Make search act like search in modern browsers
vim.opt.incsearch = true

-- Turn on "magic" for regular expressions
vim.opt.magic = true

-- Show matching brackets when cursor is over one
vim.opt.showmatch = true
vim.opt.mat = 2

-- Turn off annoying bells aon errors
--vim.opt.noerrorbells = false
--vim.opt.novisualbell = false
--vim.opt.t_vb = ''
--vim.opt.tm = 500

-- Add extra margin to the left (makes changing margins less annoying
vim.opt.foldcolumn = '1'

-- Show leader commands
vim.opt.showcmd = true

-- Show colour column
vim.opt.colorcolumn = '80,120'
vim.cmd [[ highlight ColorColumn ctermbg=16 ]]

-- => Colors and Fonts

-- Enable syntac highlighting
--vim.opt.syntax = 'enable'

-- Colorscheme
-- default

-- Workaround for gutter color
vim.cmd [[
highlight! link SignColumn LineNr
autocmd ColorScheme * highlight! link SignColumn LineNr
]]

-- Change git colors
--vim.api.nvim_set_hl(0, "DiffAdd", {fg = "#bada9f", bg = "None"})
--vim.api.nvim_set_hl(0, "DiffChange", {fg = "Purple", bg = "None"})
--vim.api.nvim_set_hl(0, "DiffDelete", {fg = "Red", bg = "None"})
--vim.api.nvim_set_hl(0, "DiffText", {fg = "Yellow", bg = "None"})

-- Set utf8 as standard encoding
vim.opt.encoding = 'utf8'

-- Use Unix as the standard file type
vim.opt.ffs = 'unix,dos,mac'

--[[
Files, backups and undo
--]]
vim.opt.swapfile = false

--[[
Text, tab and indent related
--]]

-- Use spaces instead of tabs
vim.opt.expandtab = true

-- Be smart when using tabs
vim.opt.smarttab = true

-- 1 tab is 4 spaces
vim.opt.shiftwidth  = 4
vim.opt.tabstop     = 4
vim.opt.softtabstop = 4

-- Linebreak on 500 ch
vim.opt.lbr = true
vim.opt.tw = 500

-- Auto indent
vim.opt.ai = true
-- Smart indent
vim.opt.si = true
-- Wrap lines
vim.opt.wrap = true

--[[
Visual mode related
--]]
map('v', '<silent> *', ':<C-u>call VisualSelection("","")<CR>/<C-R>=@/<CR><CR>', silentnoremap)
map('v', '<silent> #', ':<C-u>call VisualSelection("","")<CR>?<C-R>=@/<CR><CR>', silentnoremap)

--[[
Moving around, tabs, windows and buffers
--]]
-- Disable highlight when <leader><cr> is pressed
map('n', '<leader><cr>', ':noh<cr>', silentnoremap)

-- Smart way to move between windows
map('', '<C-j>', '<C-W>j', silentnoremap)
map('', '<C-k>', '<C-W>k', silentnoremap)
map('', '<C-h>', '<C-W>h', silentnoremap)
map('', '<C-l>', '<C-W>l', silentnoremap)

-- Close current buffer
map('n', '<leader>bd', ':Bclose<cr>:tabclose<cr>gT', silentnoremap)
-- Close all buffers
map('n', '<leader>ba', ':bufdo bd<cr>', silentnoremap)
-- Navigate buffers
map('n', '<leader>l', ':bnext<cr>', silentnoremap)
map('n', '<leader>h', ':bprevious<cr>', silentnoremap)

-- Tab commands
map('n', '<leader>tn',  ':tabnew<cr>', silentnoremap)
map('n', '<leader>to',  ':tabonly<cr>', silentnoremap)
map('n', '<leader>tc',  ':tabclose<cr>', silentnoremap)
map('n', '<leader>tm',  ':tabmove', silentnoremap)
map('n', '<leader>t<leader>', ':tabnext', silentnoremap)

-- Switch CWD to directory of the open buffer
map('n', '<leader>cd', ':cd %:p:h<cr>:pwd<cr', silentnoremap)

-- Behaviour when switching between buffers
vim.opt.switchbuf = 'useopen,usetab,newtab'

-- Always show tabbar
vim.opt.stal = 2

-- Return to last edit position when opening files (You want this!)
vim.cmd [[
  au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
]]

-- Make mouse work nice with tmux
vim.opt.mouse = 'a'

--[[
Status Line
--]]

-- Always show status line
vim.opt.laststatus = 2

--[[
Mappings
--]]

-- Move a line of text using ALT+[jk]
map('n', '<M-j>', 'mz:m+<cr>`z', silentnoremap)
map('n', '<M-k>', 'mz:m-2<cr>`z', silentnoremap)
map('v', '<M-j>', ":m'>+<cr>`<my`>mzgv`yo`z", silentnoremap)
map('v', '<M-k>', ":m'<-2<cr>`>my`<mzgv`yo`z", silentnoremap)

-- Move line using Command+[jk] on mac
if vim.fn.has("mac") or vim.fn.has("macunix") then
  map('n', '<D-j>', '<M-j>', silentnoremap)
  map('n', '<D-k>', '<M-k>', silentnoremap)
  map('v', '<D-j>', '<M-j>', silentnoremap)
  map('v', '<D-k>', '<M-k>', silentnoremap)
end

-- Delete trailing whitespace on save
--vim.api.nvim_create_autocmd("BufWritePre", {
--  pattern = { "*" },
--  command = [[%s/\s\+$//e]]
--})

--[[
Misc
--]]

-- Scribble buffer
map('n', '<leader>q', ':e ~/buffer<cr>', silentnoremap)

-- Toggle paste mode
map('n', '<leader>pp', ':setlocal paste!<cr>', silentnoremap)

-- Send file-title to tmux
vim.opt.titlestring = [[%f %h%m%r%w%{v:progname} (%{tabpagenr()} of %{tabpagenr('$')}})]]
vim.opt.title = true

-- Toggle displaying special characters
vim.keymap.set('n', '<leader><tab>', ToggleListChars, silentnoremap)

-- Load and save session
map('n', '<leader>sm', ':mksession! vim_session.vim<cr>', silentnoremap)
map('n', '<leader>sl', ':source vim_session.vim<cr>', silentnoremap)

-- Dont't close window when deleting buffer
--vim.api.nvim_create_user_command("Bclose",
--  "<SID>BufcloseCloseIt()",
--  {bang = true}
--)




-- This is to get rid of weird artifacts with text showing up inside buffer.
vim.opt.title = false