diff --git a/lua/oil/columns.lua b/lua/oil/columns.lua index 7e217af..97d5e3c 100644 --- a/lua/oil/columns.lua +++ b/lua/oil/columns.lua @@ -279,6 +279,10 @@ M.register("type", { end, }) +local function pad_number(int) + return string.format("%012d", int) +end + M.register("name", { render = function(entry, conf) error("Do not use the name column. It is for sorting only") @@ -289,7 +293,11 @@ M.register("name", { end, get_sort_value = function(entry) - return entry[FIELD_NAME] + if config.view_options.natural_order then + return entry[FIELD_NAME]:gsub("%d+", pad_number) + else + return entry[FIELD_NAME] + end end, }) diff --git a/lua/oil/config.lua b/lua/oil/config.lua index 8753903..b368387 100644 --- a/lua/oil/config.lua +++ b/lua/oil/config.lua @@ -90,6 +90,9 @@ local default_config = { is_always_hidden = function(name, bufnr) return false end, + -- Sort file names in a more intuitive order for humans. Is less performant, + -- so you may want to set to false if you work with large directories. + natural_order = true, sort = { -- sort order can be "asc" or "desc" -- see :help oil-columns to see which columns are sortable diff --git a/lua/oil/view.lua b/lua/oil/view.lua index 8352d70..a5032ee 100644 --- a/lua/oil/view.lua +++ b/lua/oil/view.lua @@ -519,7 +519,14 @@ end ---@return fun(a: oil.InternalEntry, b: oil.InternalEntry): boolean local function get_sort_function(adapter) local idx_funs = {} - for _, sort_pair in ipairs(config.view_options.sort) do + local sort_config = config.view_options.sort + + -- If empty, default to type + name sorting + if vim.tbl_isempty(sort_config) then + sort_config = { { "type", "asc" }, { "name", "asc" } } + end + + for _, sort_pair in ipairs(sort_config) do local col_name, order = unpack(sort_pair) if order ~= "asc" and order ~= "desc" then vim.notify_once(