diff --git a/lua/commands.lua b/lua/commands.lua index 19d76cd..68902d9 100644 --- a/lua/commands.lua +++ b/lua/commands.lua @@ -11,9 +11,6 @@ vim.api.nvim_create_user_command('TabWidth', function(opts) vim.opt.softtabstop = width end, { nargs = 1 }) --- TODO: :Rename --- vim.loop.fs_rename - -- :Remove the file associated with the current buffer, then delete the buffer vim.api.nvim_create_user_command('Remove', function(opts) local path = vim.fn.expand('%:p') @@ -45,3 +42,35 @@ vim.api.nvim_create_user_command('Remove', function(opts) end, { bang = true }) -- TODO: :Move + +-- :Rename the file associated with current buffer +vim.api.nvim_create_user_command('Rename', function(opts) + local from = vim.fn.expand('%') + local buffer = vim.api.nvim_get_current_buf() + local to = nil + local dir = vim.fn.expand('%:h') + if dir == '.' then + to = opts.args + else + to = dir .. '/' .. opts.args + end + vim.loop.fs_rename(from, to, function(err, success) + if not success then + error(err) + else + vim.schedule(function() + vim.cmd('edit ' .. to) + vim.api.nvim_buf_delete(buffer, {}) + end) + end + end) +end, { + nargs = 1, + complete = function() + return { vim.fn.expand('%:t') } + end +}) + +-- TODO: :Chmod + +-- TODO: :Mkdir