Skip to main content
added 68 characters in body
Source Link
Vivian De Smedt
  • 21.1k
  • 5
  • 22
  • 40

Following the suggestion of @filbranden you could do:

function! Rename(old_path, new_path) abort
  let c=&confirm
  set noconfirm
  try
    execute "saveas" fnameescape(a:new_path)
  catch /File exists/
    echo 'fail to rename to: ' . fnameescape(a:new_path)
    return
  finally
    let &confirm = c
  endtry
  call delete(a:old_path)
endfunction

Remark: I propose to temporarily disable the noconfirm option such that the exception is raised.

Following the suggestion of @filbranden you could do:

function! Rename(old_path, new_path) abort
  try
    execute "saveas" fnameescape(a:new_path)
  catch /File exists/
    echo 'fail to rename to: ' . fnameescape(a:new_path)
    return
  endtry
  call delete(a:old_path)
endfunction

Following the suggestion of @filbranden you could do:

function! Rename(old_path, new_path) abort
  let c=&confirm
  set noconfirm
  try
    execute "saveas" fnameescape(a:new_path)
  catch /File exists/
    echo 'fail to rename to: ' . fnameescape(a:new_path)
    return
  finally
    let &confirm = c
  endtry
  call delete(a:old_path)
endfunction

Remark: I propose to temporarily disable the noconfirm option such that the exception is raised.

deleted 2 characters in body
Source Link
Vivian De Smedt
  • 21.1k
  • 5
  • 22
  • 40

Following the suggestion of @filbranden you could do:

function! Rename(old_path, new_path) abort
  try
    execute "saveas" fnameescape(la:new_path)
  catch /File exists/
    echo 'fail to rename to: ' . fnameescape(la:new_path)
    return
  endtry
  call delete(la:old_path)
endfunction
 

Following the suggestion of @filbranden you could do:

function! Rename(old_path, new_path) abort
  try
    execute "saveas" fnameescape(l:new_path)
  catch /File exists/
    echo 'fail to rename to: ' . fnameescape(l:new_path)
    return
  endtry
  call delete(l:old_path)
endfunction
 

Following the suggestion of @filbranden you could do:

function! Rename(old_path, new_path) abort
  try
    execute "saveas" fnameescape(a:new_path)
  catch /File exists/
    echo 'fail to rename to: ' . fnameescape(a:new_path)
    return
  endtry
  call delete(a:old_path)
endfunction
Source Link
Vivian De Smedt
  • 21.1k
  • 5
  • 22
  • 40

Following the suggestion of @filbranden you could do:

function! Rename(old_path, new_path) abort
  try
    execute "saveas" fnameescape(l:new_path)
  catch /File exists/
    echo 'fail to rename to: ' . fnameescape(l:new_path)
    return
  endtry
  call delete(l:old_path)
endfunction