Thursday, October 9, 2008

Start a new shell from vi editor - vim tips

Have you ever faced the situation when you edited a file in vim editor and the time you tried to save, you realized the file is read only for you, so what you do that time ?

a) you just exit without saving; change the permission of the file, then edit the file again in vim and then save


b) from another terminal you change the permission of the file, and do the save and exit

This is what I do in this situation:

There is an command in vi to create a new shell from vi , its called sh. So go to ex mode and type


sh looks up the user's default shell (e.g. the Bourne shell or the C shell) and starts a new shell. In that shell change the permission of the file, type exit, you will be in the same vi session; now save your file.


kurt said...

hi dude,
you are very handy with all kinds of bash commands.i've subscribed your always keep me posted when you have any new ideas,thanks


Braden said...

That's brilliant. Another happy feed subscriber. Thanks.

Anonymous said...

great tip, thanks

Anonymous said...

I like the blog. With respect to VI; you don't have to issue :sh to be able to write to the file via new terminal. You can do one of the following:

:! chmod 700 filename.txt


Unknown said...

"Anonymous" thanks for your comment.

But I am afraid, with a file permission of
-r--r--r-- for a file, any of your commands (:w! or :wq! or ":! chmod 700 filename.txt") is going to work, (you can give a try, and I have doubt on the last ex command you gave, chmod...)

Thanks for going through my blog.


Unknown said...

You can also hit ctrl-Z to suspend the vim process, which should bring you back to the shell where you started vim. Then you can make your permission changes, and use the 'fg' command to un-suspend vim.

Unknown said...

@Sahir, ya thats also a very good idea of handling this. Thanks.

@Kurt,Bawb,yarbles thanks for commenting, keep reading unstableme. Thank you.


atoztoa said...

In most of the cases w! works fine.

BTW, :! is actually the shell. You can execute any shell command from there, including chmod.


Unknown said...

ATOzTOA, you are 100% correct; most of the time w! works and in :! we can change the ownership(or as you say any other command); thanks mate; good comment.

© Jadu Saikia