summaryrefslogtreecommitdiff
path: root/src/.vimrc
blob: 1443db54fe39f8e03f29c9b97d4f2eb40aec3ec0 (plain)
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

" -------------------------------
" Global options
" -------------------------------

"colorscheme ice
colorscheme default

" Colors
if has('termguicolors')
  set termguicolors
endif

" Cursor can rest beyond the last character
set virtualedit=onemore

" Highlight
set hlsearch

" Options
syntax on
set nowrap
set number
set relativenumber
set tabstop=8
set shiftwidth=8
set softtabstop=8
set noexpandtab
set autoindent
set smartindent
set scrolloff=999
set mouse=a


" Line numbers
hi LineNr       ctermfg=7 ctermbg=NONE guifg=#505050 guibg=NONE
hi CursorLineNr ctermfg=7 ctermbg=NONE guifg=#505050 guibg=NONE


" -------------------------------
" Toggle between default and kernel-style settings
" -------------------------------

let g:vim_kernel_mode = 1  " 0 = personal, 1 = kernel

function! ToggleListStyle()
    if g:vim_kernel_mode
        " Kernel style settings
        set list               " show tabs, trailing spaces, eol
        set listchars=tab:>-,trail:~,eol:$
        let g:vim_list_mode = 1
        echo "List mode"
    else
        " Restore your personal settings
        set nolist
        set listchars=
        let g:vim_list_mode = 0
        echo "No list mode"
    endif
    " toggle the mode for next call
    let g:vim_kernel_mode = !g:vim_kernel_mode
endfunction


" -------------------------------
" Statusbar
" -------------------------------

function! VisualModeLineCount()
  if mode() =~# 'v'
    return '['.(line("'>") - line("'<") + 1).' lines selected]'
  endif
  return ''
endfunction

function! ModifiedFlag()
  if &modified
    return '[Modified]'    " Your custom text
  endif
  return ''                 " Empty if not modified
endfunction

function! ReadOnlyFlag()
  if &readonly
    return '[Read-only]'    " Your custom text
  endif
  return ''                 " Empty if not modified
endfunction


" Function to display current column / total columns in current line
function! ColAndTotal()
  return printf('%d/%d', col('.'), col('$'))
endfunction

" Full-width statusline
set laststatus=2

" LEFT Side
set statusline=
set statusline+=%f                          " File name
set statusline+=\ [%l:%{ColAndTotal()}]     " Line and column / total columns
set statusline+=\ [%L/%p%%]                 " Total lines and percent
set statusline+=%{VisualModeLineCount()}    " Selected lines in visual mode
set statusline+=%{ModifiedFlag()}           " Custom modified flag

" CENTER filler (optional)
set statusline+=%=

" RIGHT side
set statusline+=\ [%y]                      " Filetype
set statusline+=%{ReadOnlyFlag()}

" Status bar transparent
hi StatusLine ctermbg=0 cterm=NONE

" Disable expandtab for make files
autocmd FileType make setlocal noexpandtab

" System clipboard
if has("clipboard")
  set clipboard=unnamedplus
endif

" ===============================
" Kernel-style 80-column guide for C files
" ===============================

" Use ftplugin for C files only
augroup c_kernel_style
    autocmd!

    " Trigger for C files
    autocmd FileType c call s:kernel_c_setup()
augroup END

function! s:kernel_c_setup()
    " Highlight the 80th column
    setlocal colorcolumn=80

    " Optional: show cursor position
    setlocal ruler

    " Optional: soft wrap for comments
    "setlocal textwidth=80
    "setlocal formatoptions+=t

    " Optional: subtle color for colorcolumn (works in GUI or truecolor terminals)
    hi ColorColumn ctermbg=236 guibg=#3a3a3a
endfunction


" -------------------------------
" Keymaps
" -------------------------------

" F2: toggle all numbers
nnoremap <F2> :if &number \| set nonumber norelativenumber \| else \| set number relativenumber \| endif<CR>

" F4: absolute numbers only
"nnoremap <F4> :set relativenumber!<CR>

" F5: Search highlight
nnoremap <F5> :nohlsearch<CR>

" F6: to toggle kernel style
nnoremap <F6> :call ToggleListStyle()<CR>

" F7: remove all trailing line
nnoremap <F7> :%s/\s\+$//e<CR>

" F8 will run checkpatch.pl and populate quickfix list
nnoremap <F8> :w<CR>:cexpr system('checkpatch.pl --no-tree --file ' . expand('%:p'))<CR>:cwindow<CR>

" Quickfix navigation with Shift + > / Shift + <
" Note: Vim sees these as > and < in normal mode
nnoremap > :cnext<CR>
nnoremap < :cprev<CR>

if &term =~ 'tmux\|xterm\|konsole'
  " Map HOME key to move to beginning of line in normal mode
  noremap <Esc>[1~ 0
  noremap <Esc>[H 0
  noremap <Esc>OH 0
endif