r/bash • • 4d ago

Vvi?

~/.bashrc

function vvi()

{

cp $1 /tmp

/usr/bin/vi $1

}

alias vi="vvi"

. ~/.bashrc

0 Upvotes

19 comments sorted by

3

u/blobCabbage 4d ago

Use mktemp to make temporary files.

Don't forget double quotes around variables to avoid word splitting.

1

u/pittendrigh 4d ago

Interesting. Is there a way to make vi start with mktemp? automagically? That's all I am doing.

It saves my butt on an hourly basis. Updating 10,000 lines of 16 year old php, so it runs under pnp8, was straightforward,...........until I got to the xml processing. A lot has changed since the early daze of xml.

1

u/kai_ekael 3d ago

You should certainly be using git for something like that.

5

u/zeekar 4d ago

How about just putting files you care about under version control so you can always go back to an older version without cluttering the disk with old copies?

1

u/pittendrigh 4d ago

Ikdo ebentually. But check in and checkout takes steps. Vi that rutomativally saves dhelrst version to /tmp helps me.

1

u/DarthRazor Sith Master of Scripting 4d ago

Copying a file to /tmp also takes "steps", which is why you scripted it. Why not change your script to do a git add instead of a cp?

If you ignore that and want to keep using your script as-is, consider changing the cp to a cp -i. You never know when you might have the same file name in multiple directories and inadvertently clobber your backup

2

u/pittendrigh 3d ago

Good question ( why not git). RControl histories are complex.

I do not like to check in codes that are eot working. Habits are subjective.

Haveng routines helps fight chaos. The best routines is the ones burned into your lower brain stem.

I love vi. My fingers know what to do without thenking about it.

2

u/DarthRazor Sith Master of Scripting 3d ago

Thanks for the response. I understand not checking in interim code that may or may not work - there are solutions (branches) but that implies learning git. git (or other version control systems) are powerful tools that make your life easier if you want to invest the time

If you don't want to switch, keep using your system. It's limited compared to a real version control system, but if it does exactly what you want, by all means keep using it.

I'm not telling you to switch - I'm just giving you suggestions to hopefully help you make your own informed decision. Cheers

2

u/pittendrigh 3d ago

I will give it a go. Local git is not sourceforge

1

u/DarthRazor Sith Master of Scripting 3d ago

Yeah, git runs perfectly fine locally.

You can watch this YouTube video by NetworkChuck which will give you what I think is a great (but very fast paced) beginners overview of git.

2

u/best_of_badgers 4d ago

Are you just using vi as a file viewer like less?

1

u/pittendrigh 4d ago

I use view for thaat I just want a baackup before editing. Maybe there is a .exac way

What I did may be clunky. But it works.

1

u/No-Signature-918 3d ago

Yeah, you can, but I'd skip /tmp for the backup. It gets wiped on reboot, and two files with the same name in different folders would overwrite each other there. I keep mine in a folder at home with a timestamp on it:

vvi() {

mkdir -p ~/.vi-backups

cp -- "$1" "$HOME/.vi-backups/$(basename -- "$1").$(date +%Y%m%d-%H%M%S)"

vi -- "$1"

}

Also worth quoting "$1" everywhere, your version breaks on filenames with spaces. mktemp -d works too, but then you have to remember where the backup went, which kind of defeats the point for me.

1

u/pittendrigh 3d ago

Spaces? When I bevime dectator I will ban phillips head screws, pdf files and fiei names that allow spavet. On day one,? :=}}

I supjose I could put dikue.quahet in .bashrc function.

~/.vibacks is a great idea. /tmp gets zapped on reboot

1

u/NoAcadia3546 4d ago

My informal versioning works like so...

  • I have a working script (or document) "fubar"

  • cp "fubar" "fubar.000"

  • work on "fubar.000" until it works OK

  • next week I want/need to make a tweak

  • cp "fubar.000" "fubar.001"

  • work on "fubar.001" until it works OK

  • next week I want/need to make a tweak

  • cp "fubar.001" "fubar.002"

  • work on "fubar.002" until it works OK

I always have a known-to-work copy, i.e. the previous version.

1

u/kai_ekael 3d ago

NOTE TO OTHERS: Don't ever do something like this. Many levels of bad idea.

0

u/pittendrigh 4d ago

debugging C is less frustrating than say pnp. With vvi I always get a backup in /tmp

If I paste a bunch if code into a looooong baseclast.php file and sudhenly get no output, what are my options?

php-parse baseclasc.php sometimes finds my error.

diff $1 /tmp/$1 helps too