5

I have a file testfile.xml that I want to open with Vim and delete in command mode

8

There are many ways:

  1. Using the intercative shell in vim

    When in the editor -

    :sh
    rm textfile.xml
    
  2. Using Bang(!)

    As DopeGhoti suggested -

    :!rm textfile.xml
    
  3. From this link.

    Add this to your ~/.vimrc

    command! -complete=file -nargs=1 Remove :echo 'Remove: '.'<f-args>'.' '.(delete(<f-args>) == 0 ? 'SUCCEEDED' : 'FAILED')
    

    Then,in vim,

    :Remove testfile.xml
    
  4. Again from the same link in #3

    Use this command:

    :call delete(expand('%')) | bdelete!
    
3

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
" ============================================================================
  • :E resulted in E464: Ambiguous use of user-defined command, but :Ex worked fine. – Serge Stroobandt May 22 at 0:15
0

To delete the file temporarily

:!rm %

% is the currently opened file

Ctrl+Z, stops the process

check with ls, file no longer on the disk

fg brings back the file on vim

you can save it, using regular vim commands

New contributor
Justin is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.