---
title: "Rename files in VIM"
description: "Here's a handy function that I use to rename files in VIM (works in MacVIM and Neovim too)"
canonical: https://oozou.com/til/rename-files-in-vim-55
locale: en
date: 2020-10-13
topic: vim
---

# Rename files in VIM

> Here's a handy function that I use to rename files in VIM (works in MacVIM and Neovim too)

Published: 2020-10-13 · Topic: [vim](https://oozou.com/til-topics/vim) ([markdown](https://oozou.com/til-topics/vim.md))

Here's a handy function that I use to rename files in VIM (works in MacVIM and Neovim too)

```vim
function! RenameFile()
    let old_name = expand('%')
    let new_name = input('New file name: ', expand('%'), 'file')
    if new_name != '' && new_name != old_name
        exec ':saveas ' . new_name
        exec ':silent !rm ' . old_name
        redraw!
    endif
endfunction
nnoremap <leader>n :call RenameFile()<cr>
```

NOTE: I've used *`<leader>`* which is mapped to comma in my config. but you can use whatever key combiniation you like.

---

Contact OOZOU: hello@oozou.com · +66 2544 0000 · https://oozou.com/contact

More for AI agents: https://oozou.com/llms.txt · every page has a markdown twin at its URL plus ".md"
