LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   VIM save options to protect file from too easy delete? (https://www.linuxquestions.org/questions/linux-newbie-8/vim-save-options-to-protect-file-from-too-easy-delete-4175682637/)

papazulu 09-24-2020 10:17 AM

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.

boughtonp 09-24-2020 10:21 AM


 
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


papazulu 09-24-2020 10:48 AM

Quote:

Originally Posted by boughtonp (Post 6169212)
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.

boughtonp 09-24-2020 11:14 AM


 
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.


dugan 09-24-2020 01:04 PM

Quote:

Originally Posted by papazulu (Post 6169220)
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.

papazulu 09-24-2020 02:13 PM

Quote:

Originally Posted by dugan (Post 6169257)
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.

Tonus 09-24-2020 03:13 PM

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).

papazulu 09-24-2020 03:24 PM

Quote:

Originally Posted by Tonus (Post 6169300)
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?

boughtonp 09-24-2020 04:34 PM

Quote:

Originally Posted by papazulu (Post 6169304)
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.


papazulu 09-24-2020 08:05 PM

Quote:

Originally Posted by boughtonp (Post 6169314)
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

Tonus 09-24-2020 09:11 PM

Quote:

Originally Posted by papazulu (Post 6169304)
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.

papazulu 09-24-2020 10:13 PM

Quote:

Originally Posted by Tonus (Post 6169369)
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


dugan 09-24-2020 11:17 PM

Quote:

Originally Posted by papazulu (Post 6169304)
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

Tonus 09-25-2020 01:13 PM

Quote:

Originally Posted by papazulu (Post 6169376)
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.


All times are GMT -5. The time now is 03:49 AM.