I sometimes open a read-only file in vi, forgetting to do chmod +w
before opening it. Is there way to change the file from within vi?
Something like !r chmod +w [filename]
?
Is there a shortcut to refer to the currently open file without spelling it's long name?
I know this is an old post, but with Vim Version8 a function has been included with which you can change file permissions.
According to the version8.txt file:
This function can then be called via the "call" command in Vim.
This is done as follows:
The structure of the "permissions" string takes the same form as described in the Vim documentation:
A minimal example:
This adds read, write and execute permissions to the "foo.txt" file in the current directory.
If you have the rights to write to the file, then you can just use exclamation mark to force it:
If you don't have the rights and need to change user, but still want to write to the file, sometimes you may go for something like
and if vi still doesn't want to write it,
Have you tried
The % represents the current filename.
You could also map a key to this like Ctrl-W.
Note that you type Ctrl-V Ctrl-M to get the
<CR>
Just use
in command mode. % will be replaced by the current file name.
After editing your file with vim, press "esc" and then ":". Then type the following command:
Then press "Enter". Then type
to successfully exit from the editor.