@@ -8,6 +8,7 @@ Have a cool recipe to share? Open a pull request and add it to this doc!
88- [ Show CWD in the winbar] ( #show-cwd-in-the-winbar )
99- [ Hide gitignored files and show git tracked hidden files] ( #hide-gitignored-files-and-show-git-tracked-hidden-files )
1010- [ Open Telescope file finder in the current oil directory] ( #open-telescope-file-finder-in-the-current-oil-directory )
11+ - [ Add custom column for file extension] ( #add-custom-column-for-file-extension )
1112
1213<!-- /TOC -->
1314
@@ -167,3 +168,79 @@ local bufnr = vim.api.nvim_get_current_buf()
167168-- ... some operation that changes the current buffer ...
168169local dir = require (" oil" ).get_current_dir (bufnr )
169170```
171+
172+ ## Add custom column for file extension
173+
174+ ``` lua
175+ local oil_cfg = require " oil.config"
176+ local oil_constant = require " oil.constants"
177+ local oil_column = require " oil.columns"
178+
179+ local FIELD_TYPE = oil_constant .FIELD_TYPE
180+ local FIELD_NAME = oil_constant .FIELD_NAME
181+
182+ local function adjust_number (int )
183+ return string.format (" %03d%s" , # int , int )
184+ end
185+
186+ local function format (output )
187+ return vim .fn .fnamemodify (output , " :e" )
188+ end
189+
190+ oil_column .register (" extension" , {
191+ render = function (entry , _ )
192+ local field_type = entry [FIELD_TYPE ]
193+ local name = entry [FIELD_NAME ]
194+
195+ if field_type == " file" then
196+ if name then
197+ local extension = format (name )
198+
199+ if not extension :match " %s" then
200+ return extension
201+ end
202+ end
203+ end
204+ end ,
205+ parse = function (line , _ )
206+ return line :match " ^(%S+)%s+(.*)$"
207+ end ,
208+ create_sort_value_factory = function (num_entries )
209+ if
210+ oil_cfg .view_options .natural_order == false
211+ or (oil_cfg .view_options .natural_order == " fast" and num_entries > 5000 )
212+ then
213+ return function (entry )
214+ return format (entry [FIELD_NAME ]:lower ())
215+ end
216+ else
217+ local memo = {}
218+
219+ return function (entry )
220+ if memo [entry ] == nil and entry [FIELD_TYPE ] == " file" then
221+ local name = entry [FIELD_NAME ]:gsub (" 0*(%d+)" , adjust_number )
222+
223+ memo [entry ] = format (name :lower ())
224+ end
225+
226+ return memo [entry ]
227+ end
228+ end
229+ end ,
230+ })
231+
232+ require (" oil" ).setup ({
233+ columns = {
234+ " size" ,
235+ " extension" ,
236+ " icon" ,
237+ },
238+ view_options = {
239+ sort = {
240+ { " type" , " asc" },
241+ { " extension" , " asc" },
242+ { " name" , " asc" },
243+ },
244+ },
245+ })
246+ ```
0 commit comments