[SOLVED] VIM save options to protect file from too easy delete?
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 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.
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.
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.
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
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.