---
title: "เปลี่ยนชื่อไฟล์ใน VIM"
description: "นี่คือฟังก์ชันสุดสะดวกที่ใช้เปลี่ยนชื่อไฟล์ใน VIM (ใช้งานได้ทั้งใน MacVIM และ Neovim ด้วย)"
canonical: https://oozou.com/til/rename-files-in-vim-55
locale: th
date: 2020-10-13
topic: vim
---

# เปลี่ยนชื่อไฟล์ใน VIM

> นี่คือฟังก์ชันสุดสะดวกที่ใช้เปลี่ยนชื่อไฟล์ใน VIM (ใช้งานได้ทั้งใน MacVIM และ Neovim ด้วย)

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

นี่คือฟังก์ชันสุดสะดวกที่ใช้เปลี่ยนชื่อไฟล์ใน VIM (ใช้งานได้ทั้งใน MacVIM และ Neovim ด้วย)

```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>
```

หมายเหตุ: ในที่นี้ใช้ *`<leader>`* ซึ่งใน config ของผู้เขียน map ไว้กับปุ่ม comma แต่คุณจะใช้ key combination ไหนก็ได้ตามที่ชอบ

---

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

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