I have a file testfile.xml that I want to open with Vim and delete in command mode
5 Answers
Another way to interactively delete files is to use
:E
or
:Ex
for ex(ploring) the file directory. There you have an interactive window that will guide you. Once you select the entry you want deleted, use
Shift-D
confirm with
y
return
This is what it looks like at the top in my window in explorer mode:
" ============================================================================
" Netrw Directory Listing (netrw v140)
" C:\Users\cbkrd\Desktop
" Sorted by name
" Sort sequence: [\/]$,\.h$,\.c$,\.cpp$,*,\.o$,\.obj$,\.info$,\.swp$,\.bak$,\~$
" Quick Help: <F1>:help -:go up dir D:delete R:rename s:sort-by x:exec
" ============================================================================
-
:Eresulted inE464: Ambiguous use of user-defined command, but:Exworked fine.Serge Stroobandt– Serge Stroobandt2019-05-22 00:15:27 +00:00Commented May 22, 2019 at 0:15
There are many ways:
Using the intercative shell in vim
When in the editor -
:sh rm textfile.xmlUsing Bang(!)
As DopeGhoti suggested -
:!rm textfile.xmlFrom this link.
Add this to your
~/.vimrccommand! -complete=file -nargs=1 Remove :echo 'Remove: '.'<f-args>'.' '.(delete(<f-args>) == 0 ? 'SUCCEEDED' : 'FAILED')Then,in vim,
:Remove testfile.xmlAgain from the same link in #3
Use this command:
:call delete(expand('%')) | bdelete!
To delete the file, execute :!rm %. % expands to the file's name. Vim doesn't automatically delete the buffer, but you can manually do so with :bd.
To verify that the file has been deleted, execute !ls.
As Yash states: there are many ways. But the shortest one,
:echo delete(@%)
only appears hidden, only at the end of Yash's link:
I don't understand why you would use all those commands and functions.
Why not
:echo delete(filename)? Or, if you want to delete file and wipeout buffer, which I guess happens very rarely, you may write:echo delete(@%), and if successfull --:bw!.
With echo (instead of call) you see the result directly, 0 or -1.
:!rm % by @Justin is probably the same, but there is a screen switch and the "no longer available" message -- with delete() it is smoother.
And I don't understand what rm is filtering here...
The whole idea makes mostly sense if you want to sort out a bunch of files:
vim $(find -size -100c -type f)
If you want to delete/keep some of these small files. you can :n[ext] through them and delete them directly. The :echo delete(@%) should maybe only be mapped temporarily, because it is dangerous. I lost a help file, and renamed another, while experimenting with "%".
-
It is also cross-platform.tejasvi– tejasvi2022-01-22 10:35:30 +00:00Commented Jan 22, 2022 at 10:35
mv is safer than rm:
com! -nargs=? -complete=file T call Trash_by_viM(<f-args>)
" 0 or 1 arguments are allowed
fun! Trash_by_viM(...) abort
if a:0 > 0 " 有传参
" if a:1 之前用这行, 不行, 因为if 'abc' 等于if FALSE, if '1abc' 才等于if TRUE
exe '! mv' a:1 '~/.t/'..a:1..'__moved_by_vim'
echo "moved" a:1
el
write
exe '! mv' expand("%:p") '~/.t/'..expand("%:p:t")..'__moved_by_vim'
echo "moved" expand("%:p")
bwipeout!
en
endf
:!rm testfile.xml?viis not a tool to delete file,rmandfindare as well as backup tools.