LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-24-2020, 10:17 AM   #1
papazulu
LQ Newbie
 
Registered: Mar 2013
Posts: 11

Rep: Reputation: Disabled
Question VIM save options to protect file from too easy delete?


Hi,

I am looking for a save option in VIM (not crazy high level), just something simple that keeps me from easily deleting a file I have created.

For instance, if I save file 'test' in VIM all I have to do to delete it is ' rm test '. How can I save file 'test' so that simply typing ' rm test ' does not delete the file forever?

Some suggestions on other non VIM methods for protecting files are welcome. But the main idea here is something not extreme. Something simple and low level. Over the post month I have been relearning Linux and managed to accidentally delete some longish C programming files that I had to retype out. I want there to be an option in VIM that saves a file so that it asks me before deleting.

Thank you,

Jon.
 
Old 09-24-2020, 10:21 AM   #2
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,616

Rep: Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554

Keep the files in a Git repo, and get Vim to auto-commit whenever you save.

Here's a result from a relevant search that provides a few solutions:
https://superuser.com/questions/286290/is-there-any-way-to-hook-saving-in-vim-up-to-commiting-in-git

 
Old 09-24-2020, 10:48 AM   #3
papazulu
LQ Newbie
 
Registered: Mar 2013
Posts: 11

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by boughtonp View Post
Keep the files in a Git repo, and get Vim to auto-commit whenever you save.

Here's a result from a relevant search that provides a few solutions:
https://superuser.com/questions/286290/is-there-any-way-to-hook-saving-in-vim-up-to-commiting-in-git

Thank you for your reply but this is precisely the opposite of the sort of solution that I am looking for. I am looking for something simple that is native to VIM. I am not currently doing C projects at that level. I am learning C at the moment. Anyway it just seems wayyyy too easy to delete something in Linux. And I know there are methods of protecting files in Linux,
. . . BUT again what I am really really looking for is something native to VIM that can quickly easily save a file into some semi-protected state. If there not that sort of thing then OH WELL bummer. Just seems like there should be. ' rm file ' and its gone forever is really pathetic.

Again thank you for your reply.
 
Old 09-24-2020, 11:14 AM   #4
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,616

Rep: Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554

This isn't a Vim problem - you're the one typing "rm file" - don't use rm and you've not got the problem.

https://duckduckgo.com/?q=linux+prev...ental+deletion


Quote:
I am not currently doing C projects at that level.
You don't need to be doing any projects at any level to use version control, and since you're worried about losing code then you absolutely should be using it!

Using Git is a simple way to solve this problem and others.

 
Old 09-24-2020, 01:04 PM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,241

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
Quote:
Originally Posted by papazulu View Post
BUT again what I am really really looking for is something native to VIM that can quickly easily save a file into some semi-protected state.
setfperm is what my searches are returning.

http://stevenrosenberg.net/blog/appl...from_within_vi

It does, of course, need you to understand how nix file permissions work.

Last edited by dugan; 09-24-2020 at 03:22 PM. Reason: Corrected grammar.
 
Old 09-24-2020, 02:13 PM   #6
papazulu
LQ Newbie
 
Registered: Mar 2013
Posts: 11

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by dugan View Post
setfperm is what my searches are returning.

http://stevenrosenberg.net/blog/appl...from_within_vi

It does, of course, need you to understand nix file permissions work.
Thank you for reply.

What I have done is just create directory Trash in /home/me and from now on will just move files to there that I think I am probably done with. Right now I am trying to figure out how to create a command 'mvt' that automatically moves a file to ~/Trash without me having to type ~/Trash.

Thing with me is (coming from Windows) I will quite often delete files that I am only mostly sure I want gone, usually as a 'tidying up' measure and I just like that short term option of going "ohh wait, I actually still want that file." I usually go two weeks before emptying Recycle Bin. I realize Linux desktops also have this but when I am in Linux I almost never use GUI.

So how would I create a custom command 'mvt' that automatically moves 'trash' files to directory ~/Trash ? That is my next Linux learning project.
 
Old 09-24-2020, 03:13 PM   #7
Tonus
Senior Member
 
Registered: Jan 2007
Location: Paris, France
Distribution: Slackware-15.0
Posts: 1,407
Blog Entries: 3

Rep: Reputation: 514Reputation: 514Reputation: 514Reputation: 514Reputation: 514Reputation: 514
I'd suggest to alias rm in your shell to
Code:
rm -i
With this small hack you will be asked to confirm unless you use the -f flag (force).
 
Old 09-24-2020, 03:24 PM   #8
papazulu
LQ Newbie
 
Registered: Mar 2013
Posts: 11

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Tonus View Post
I'd suggest to alias rm in your shell to
Code:
rm -i
With this small hack you will be asked to confirm unless you use the -f flag (force).
Hi Tonus, here is a good thread on the dangers of aliasing rm.

https://apple.stackexchange.com/ques...-the-trash-can

What I am now really wanting to do now is create a custom command ' mvt ' that automatically moves files to ~/Trash

I have created a Trash directory in my /home/me and of course I can use 'mv file1 file2 ~/Trash' but for Linux learning purposes I would like to create a custom command where no matter what directory I am in I can just type -$ mvt file1 file2 , and it automatically moves them to ~/Trash

Any suggestions?
 
Old 09-24-2020, 04:34 PM   #9
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,616

Rep: Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554
Quote:
Originally Posted by papazulu View Post
What I am now really wanting to do now is create a custom command ' mvt ' that automatically moves files to ~/Trash
Create a function, and put it into a startup file so it's created when shell starts.

 
Old 09-24-2020, 08:05 PM   #10
papazulu
LQ Newbie
 
Registered: Mar 2013
Posts: 11

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by boughtonp View Post
Create a function, and put it into a startup file so it's created when shell starts.

Thank you boughtonp.

Ha! Check this out. Earlier I inquired about this of Joe Collins of youtube in a comment on one of his videos and already he made this reply video answering my question. This guy is amazing! This is a somewhat different solution than to what I inquired about in my original post here but satisfies the general spirit of it.

BASH Script to put Files In the Trash
 
Old 09-24-2020, 09:11 PM   #11
Tonus
Senior Member
 
Registered: Jan 2007
Location: Paris, France
Distribution: Slackware-15.0
Posts: 1,407
Blog Entries: 3

Rep: Reputation: 514Reputation: 514Reputation: 514Reputation: 514Reputation: 514Reputation: 514
Quote:
Originally Posted by papazulu View Post
Hi Tonus, here is a good thread on the dangers of aliasing rm.

https://apple.stackexchange.com/ques...-the-trash-can
Could you elaborate ? I see nothing following the link that would make the point regarding what I suggested.
 
Old 09-24-2020, 10:13 PM   #12
papazulu
LQ Newbie
 
Registered: Mar 2013
Posts: 11

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Tonus View Post
Could you elaborate ? I see nothing following the link that would make the point regarding what I suggested.
rm has a certain important behavior, so important that it is considered a bad idea to alias it to something else that can lead a user of the command to become even slightly less careful than he/she would be if they had not gotten used to the new aliased behavior. If one becomes used to being prompted when typing 'rm' then (so the argument in the aforementioned thread goes), they could find themselves on a machine one day that does not have 'rm' aliased and expect a prompt and not get one.

Anyway I found a good solution:

Below is a script command I named 'mvt' that can be used thus: -$ mvt file1 file2

This command 'mvt' moves files to a folder called Trash in Home directory, and creates the folder if it is not there already.

HERE IS THE SCRIPT. Note once created it will need to have 'chmod +x' applied to it remove 'permission denied' error.
I have mine in /bin WITHOUT the .sh file extension

Code:
#!/bin/bash

if [ ! -d "$HOME/Trash" ] ; then
    mkdir "$HOME/Trash"
fi

for FILE in "$@"
    do
	mv "$FILE" ~/Trash/
    done
 
Old 09-24-2020, 11:17 PM   #13
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,241

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
Quote:
Originally Posted by papazulu View Post
What I am now really wanting to do now is create a custom command ' mvt ' that automatically moves files to ~/Trash
https://github.com/sindresorhus/trash-cli
 
Old 09-25-2020, 01:13 PM   #14
Tonus
Senior Member
 
Registered: Jan 2007
Location: Paris, France
Distribution: Slackware-15.0
Posts: 1,407
Blog Entries: 3

Rep: Reputation: 514Reputation: 514Reputation: 514Reputation: 514Reputation: 514Reputation: 514
Quote:
Originally Posted by papazulu View Post
rm has a certain important behavior, so important that it is considered a bad idea to alias it to something else that can lead a user of the command to become even slightly less careful than he/she would be if they had not gotten used to the new aliased behavior. If one becomes used to being prompted when typing 'rm' then (so the argument in the aforementioned thread goes), they could find themselves on a machine one day that does not have 'rm' aliased and expect a prompt and not get one.
The alias I wrote hasn't got the caveat you're talking about : having to confirm is annoying, you then get used to use -f flag and be careful.

Don't believe everything you read on the internet.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Embrace, extend, and protect? Microsoft joins the Open Invention Network to 'protect Linux and open source' LXer Syndicated Linux News 0 10-11-2018 12:21 AM
[SOLVED] Fedora shows 'man vim' but when execute 'vim' got "bash: vim: command not found..." ? flash_os Linux - Newbie 19 01-03-2015 11:56 PM
LXer: My Nerd Life: Too Loud, Too Funny, Too Smart, Too Fat LXer Syndicated Linux News 0 01-24-2014 05:21 AM
Switching from vim to vim -g from inside vim iDragoon Linux - Software 4 05-15-2009 11:46 AM
Editor comparison: vim VS vim-lite, Cleaning vim Ruler2112 *BSD 4 04-13-2009 04:26 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration