2

Say I have stuff.txt, filled with stuff, in the current directory. And I want to get rid of it, and make a new stuff.txt in vim. Obviously I could do

rm stuff.txt
vi stuff.txt

But that's two steps, the horror! Is there a way to consolidate this into one vim/vi call without invoking rm? Perphaps some -option on vim that I somehow missed when looking in the manual?

Obvious workaround is to create something like this, in a file called, for example, new.sh:

#!/bin/bash
rm $1
vim $1

and then do from the command line, ./new.sh stuff.txt, but that seems a bit un-eleagant. I'm on ubuntu using the standard bash.

iammax
  • 453
  • 2
  • 5
  • 18
  • `> stuff.txt` and `vim !$`? – Cyrus Sep 05 '17 at 18:32
  • Would 2 commands but shortcuts work? You can leverage bash and get the last argument either via `!$` (e.g. `vim !$`) or use `M-.` See post: [How to use arguments from previous command?](https://stackoverflow.com/q/4009412/438329) – Peter Rincker Sep 05 '17 at 18:43

3 Answers3

5

You can start vim like this:

vim -c '%d' stuff.txt

Here -c option is:

-c <command> Execute <command> after loading the first file

%d will delete all lines from file after opening the file.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    Or `vim -c '%d' -c 'w' stuff.txt`, and after `:q!` or `:e!` you won't get stuff back. When you also want something against `u` (undo), try `vim -c '%d' -c 'w' -c 'e' stuff.txt`. – Walter A Sep 05 '17 at 18:43
  • 1
    Being able to use undo is probably the best option, not to mention with persistent history it becomes even more useful. – Peter Rincker Sep 05 '17 at 20:33
1
rmed () {
    local EDITOR=${EDITOR:-vi}
    [ -f "$1" ] && rm "$1"
    command $EDITOR "$1"
}

This is a shell function that will remove the given file, then open $EDITOR (or vi if $EDITOR is not set) with a file of the same name.

$ rmed somefile
Kusalananda
  • 14,885
  • 3
  • 41
  • 52
0

You write a function/alias/script to do what you want.
I have a script called vix, that will start writing a shell script (shebang line and chmod +x) when the argument is not existing or only chmod and edit it when it exists.
Perhaps you should use virm because I do not like the sound of rmvi.

Walter A
  • 19,067
  • 2
  • 23
  • 43