BanditHijo.dev

Lightline, Alternatif Vim Statusline Bagian 2 (feat. Defx, Tagbar)

Created at: 2020-10-30
Author by: BanditHijo

Latar Belakang

Post ini saya tulis sebagai update catatan bagian pertama yang berjudul:
Lightline, Alternatif Vim Statusline.

Saya memutuskan untuk membuat bagian kedua, karena sudah cukup banyak perubahan yang saya tambahkan dari bagian pertama. Terutama pada bagian pengecualian statusline pada Defx buffer.

Kenapa ada pengecualian pada Defx?

Menurut saya, saya tidak memerlukan statusline yang sangat informatif pada Defx.

Agak lucu rasanya, kalau pada Defx, statusline ditampilkan Git branch, line & column poisiton, dll.

Maka, pada buffer Defx, Lightline statusline tidak diperlukan untuk menampilkan status secara lengkap.

Instalasi

Saya menggunakan vim-plug.

$HOME/.config/nvim/init.vim
1Plug 'itchyny/lightline.vim'

Konfigurasi

Contoh-contoh konfigurasi dapat teman-teman lihat pada halaman GitHub dari Lightline.

Saya akan langsung menunjukkan konfigurasi yang saya lakukan pada bagian kedua ini.

$HOME/.config/nvim/init.vim
1" LightLine
2
3let g:lightline = {
4\ 'colorscheme': 'codedark_bandit',
5\ 'active': {
6\ 'left' : [[ 'mode', 'paste' ],
7\ [ 'gitbranch' ],
8\ [ 'filename' ]],
9\ 'right': [[ 'trailing' ],
10\ [ 'lineinfo' ],
11\ [ 'percent' ],
12\ [ 'filetype', 'fileencoding', 'fileformat' ] ]
13\ },
14\ 'tab': {
15\ 'active' : ['tabnum'],
16\ 'inactive' : ['tabnum']
17\ },
18\ 'tabline': {
19\ 'left' : [['buffers']],
20\ 'right' : [['string1'], ['string2', 'smarttabs']]
21\ },
22\ 'separator': {
23\ 'left': '', 'right': ''
24\ },
25\ 'subseparator': {
26\ 'left': '\u2502', 'right': '\u2502'
27\ },
28\ 'component': {
29\ 'filename': '%<%{LightlineFileName()}'
30\ },
31\ 'component_function': {
32\ 'gitbranch' : 'LightlineFugitive',
33\ 'readonly' : 'LightlineReadonly',
34\ 'fileformat' : 'LightlineFileformat',
35\ 'filetype' : 'LightlineFiletype',
36\ 'fileencoding' : 'LightlineFileEncoding',
37\ 'lineinfo' : 'LightlineLineInfo',
38\ 'percent' : 'LightlinePercent',
39\ 'mode' : 'LightlineMode',
40\ },
41\ 'component_expand': {
42\ 'buffers' : 'lightline#bufferline#buffers',
43\ 'string1' : 'String1',
44\ 'string2' : 'String2',
45\ 'smarttabs' : 'SmartTabsIndicator',
46\ 'trailing' : 'LightlineTrailingWhitespace',
47\ },
48\ 'component_type': {
49\ 'buffers' : 'tabsel',
50\ 'trailing' : 'warning'
51\ },
52\ 'mode_map': {
53\ 'n' : ' N0RMAL',
54\ 'i' : ' INSERT',
55\ 'R' : ' REPLACE',
56\ 'v' : ' VISUAL',
57\ 'V' : ' V-LINE',
58\ "\<C-v>" : ' V-BL0CK',
59\ 'c' : ' COMMAND',
60\ 's' : ' SELECT',
61\ 'S' : ' S-LINE',
62\ "\<C-s>" : ' S-BL0CK',
63\ 't' : ' TERMINAL',
64\ }
65\}
66
67function! LightlineReadonly()
68 return &readonly ? '' : ''
69endfunction
70
71function! LightlineFugitive()
72 if &filetype !=? 'defx' && &filetype !=? 'tagbar' && &filetype !=? 'taglist'
73 if exists('*fugitive#head')
74 let branch = fugitive#head()
75 return branch !=# '' ? ' ' . branch : ''
76 endif
77 return fugitive#head()
78 else
79 return ''
80 endif
81endfunction
82
83function! LightlineFileformat()
84 if &filetype !=? 'defx' && &filetype !=? 'tagbar' && &filetype !=? 'taglist'
85 return winwidth(0) > 70 ? (&fileformat . ' ' . WebDevIconsGetFileFormatSymbol()) . ' ' : ''
86 else
87 return ''
88 endif
89endfunction
90
91function! LightlineFiletype()
92 if &filetype !=? 'defx' && &filetype !=? 'tagbar' && &filetype !=? 'taglist'
93 return winwidth(0) > 70 ? (strlen(&filetype) ? &filetype . ' ' . WebDevIconsGetFileTypeSymbol() : 'no ft') : ''
94 else
95 return ''
96 endif
97endfunction
98
99function! LightlineFileEncoding()
100 if &filetype !=? 'defx' && &filetype !=? 'tagbar' && &filetype !=? 'taglist'
101 return &fileencoding
102 else
103 return ''
104 endif
105endfunction
106
107function! LightlineLineInfo()
108 if &filetype !=? 'defx' && &filetype !=? 'tagbar' && &filetype !=? 'taglist'
109 let current_line = printf('%3s', line('.'))
110 let current_col = printf('%-3s', col('.'))
111 let lineinfo = ' ' . current_line . ':' . current_col
112 return lineinfo
113 else
114 return ''
115 endif
116endfunction
117
118function! LightlinePercent()
119 if &filetype !=? 'defx' && &filetype !=? 'tagbar' && &filetype !=? 'taglist'
120 return printf(' %3s', (line('.') * 100 / line('$'))) . '%'
121 else
122 return ''
123 endif
124endfunction
125
126function! LightlineFileName()
127 let filename = expand('%')
128 let modified = &modified ? '' : ''
129 let readonly = &readonly
130 if &filetype !=? 'defx' && &filetype !=? 'tagbar' && &filetype !=? 'taglist'
131 if filename ==# ''
132 return '[No Name]'
133 endif
134
135 let terms = split(filename, ':')
136 if terms[0] ==# 'term'
137 return '[' . terms[-1] . ']'
138 endif
139
140 return filename . ' ' . (readonly ? '' : modified)
141 else
142 return expand('%:t') ==# '__Tagbar__.1' ? '[tagbar]' :
143 \ expand('%:t') ==# '__Tag_List__' ? '[taglist]' :
144 \ &filetype ==# 'defx' ? '[defx]' :
145 \ ''
146 endif
147endfunction
148
149function! LightlineMode()
150 return expand('%:t') ==# '__Tagbar__.1' ? ' TAGBAR' :
151 \ expand('%:t') ==# '__Tag_List__' ? ' TAGLIST' :
152 \ &filetype ==# 'defx' ? ' DEFX' :
153 \ lightline#mode()
154endfunction
155
156function! String1()
157 return ' BANDITHIJO.GITHUB.IO'
158endfunction
159
160function! String2()
161 return 'BUFFERS'
162endfunction
163
164function! SmartTabsIndicator()
165 let tabs = lightline#tab#tabnum(tabpagenr())
166 let tab_total = tabpagenr('$')
167 return tabpagenr('$') > 1 ? ('TABS ' . tabs . '/' . tab_total) : ''
168endfunction
169
170function! LightlineTrailingWhitespace()
171 if &filetype !=? 'defx'
172 let status = lightline#trailing_whitespace#component()
173 return status == 'trailing' ? '!' : ''
174 else
175 return ''
176 endif
177endfunction
178
179" autoreload
180command! LightlineReload call LightlineReload()
181
182function! LightlineReload()
183 call lightline#init()
184 call lightline#colorscheme()
185 call lightline#update()
186endfunction
187
188set showtabline=2 " Show tabline, 2 show, 1 hide
189set guioptions-=e " Don't use GUI tabline

Penjelasan

Seperti yang dapat dilihat di atas, cukup banyak function yang saya definisikan.

Saya membuat function agar lebih leluasa untuk memodifikasi isi dari statusline apabila dirasa tampilan default yang dsediakan kurang mencukupi.

Function-function modifikasi ini nantinya ditempatkan pada 'component_function': {..}.

Credit

Terima kasih kepada mas Yeri, untuk catatan di blognya.

Pesan Penulis

Sepertinya, segini dulu yang dapat saya tuliskan.

Untuk konfigurasi Lightline milik saya yang lebih terbaru, dapat teman-teman kunjungi di sini.

Mudah-mudahan dapat bermanfaat.

Terima kasih.

(^_^)

Referensi

  1. github.com/itchyny/lightline.vim
    Diakses tanggal: 2020/10/30

  2. hagen.dev/kristoffer/dotfiles - [nvim] Lightline config
    Diakses tanggal: 2020/10/30