iammax
iammax

Reputation: 473

Open blank new file with the same name as existing file in one step, in VIM?

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.

Upvotes: 3

Views: 867

Answers (3)

Walter A
Walter A

Reputation: 20032

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.

Upvotes: 0

Kusalananda
Kusalananda

Reputation: 15633

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

Upvotes: 1

anubhava
anubhava

Reputation: 786241

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.

Upvotes: 6

Related Questions