3

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.

2

3 Answers 3

6

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.

2
  • 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
    Commented Sep 5, 2017 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. Commented Sep 5, 2017 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
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.

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