I'm customizing vim (vimdiff) as a testing tool for diffing certain files, and would like to disable saving the files completely - however, I haven't been able to stop :w! .. I've tried starting vim/vimdiff with -R option, and I have also tried setting cpoptions to contain W, which means:
cpo-W
W Don't overwrite a readonly file. When omitted, ":w!"
overwrites a readonly file, if possible.
..but vim seems not to care about this:
echo yo > foo
vim -R foo
:set cpoptions=aABceFsW
:set cpoptions?
cpoptions=aABceFsW
:w
E45: 'readonly' option is set (add ! to override)
:w!
"foo" 1L, 3C written
..bug, or did I misunderstand it?
You just told Vim that it should run in "readonly mode". The file itself is not readonly. If you chmod -w foo, then Vim would not allow to write the file on :w! while W is present in cpoptions.
You could try to start Vim with -M:
vim -M foo
This disallows any changes to the file and prevents writing the file. See :help -M.
Another solution might be:
vim -m foo
This opens the file and you can edit it (you get the warning W10: Warning: Changing a readonly file), but you are not able to write it. The option write is reset (to nowrite) and prevents writing to any file. So this is global.
If you finally decide, that you need to write it anyway, you have to set the option with the command :set write. As said, this is global, so you are than able to write any file.
It's much better to work on the file permissions, or to use the -M option as stated by #user7369280, but the funny thing is you can also launch vi with certain options that will prevent you to use the classic w or wq commands for example:
vi my_file -c 'cabbrev w <esc>' -c 'cabbrev wq <esc>'
It basically disables the commands w and wq with redefining them with the command escape (which doesn't do much)
I vim somefile.txt often without using sudo. Is it possible to use sudo within an open vim session so you don't have to close the file and reopen? New to Vim and Linux in general so any help is very appreciated. Thank you.
Well seems like I was late to the party. As said you should use :w !sudo tee % to switch to sudo. If you would like to change the permissions of the file so you will not need sudo again you could use :!chmod +w % in command mode with %getting replaced by the filename to change the permissions of the file afterwards, as stated here: How to change file permission from within vi.
I have this in my .vimrc, so that a :W will write with sudo access.
" :W sudo saves the file
command W w !sudo tee % > /dev/null`
You can use the SudoEdit plugin; it provides a :SudoWrite command.
There's also a trick of :write to a process of !sudo tee, passing the current file name; it is explained here.
I'm attempting to create a directory from within a netrc explorer window, however, upon creating the directory netrw returns error (netrw) unable to make directory. The directory is actually created in the background, but my cursor is brought to the error message and netrw does not update the directory listing.
Had the same issue, my problem was that I had set the wrong shell in my vimrc, changing that fixed it for me.
To find the shell you're using do this in your terminal:
echo $SHELL
/bin/zsh
Then I change my .vimrc to use the correct shell:
set shell=/bin/zsh
Then to verify Vim is using the right shell you can do:
echo &shell
/bin/zsh
From my testing, it's clear that nvim doesn't source your .bash_profile when opening a new terminal buffer. I would like to force this to happen every time a new terminal buffer is created.
To confirm this behavior:
open your .bash_profile in nvim
export a new variable like
ISSOURCED
write out the file
open a terminal buffer
run echo $ISSOURCED
I've also checked that bash is running in interactive mode (it is) by executing if tty -s; then echo interactive; fi, based on this answer
However it is not a login shell, based on executing shopt -q login_shell && echo 'Login shell' || echo 'Not login shell', based on this answer. This means that it would normally source $HOME/.bashrc. Unfortunately I keep my bashrc in a different location and source it from my .bash_profile, so it isn't being picked up.
See my answer below for my current workaround and information about why it's less than ideal.
Add this to your ~/.vimrc:
set shell=bash\ -l
When invoked with -l (--login), bash reads your ~/.profile at startup (among other files) and thus everything sourced from there.
When invoked with -i (--interactive), bash reads your ~/.bashrc at startup (among other files) and thus everything sourced from there.
$ man bash or :h shell and :h shellcmdflag for more info.
Taken from: https://stackoverflow.com/a/9092644/1071756
I set up a mapping to open a new terminal:
nnoremap <leader>z :new<CR>:terminal<CR>
To source my .bash_profile, I changed it to this:
nnoremap <leader>z :new<CR>:terminal<CR>source $HOME/.bash_profile<CR>c<CR>
The problem with this solution is that it breaks if you try to open a terminal buffer in any way other than with this mapping
I am not able to run gvim from cygwin. When I try to open a new file with :
gvim filename
gvim opens a file with 'No name' and displays error as :
Error detected while processing command line
E492: Not editor command: C:\cygwin\home\chandan\l
Press enter or type command to continue
More problematic is that I can't open existing file in the path
>which gvim shows /usr/bin/gvim
I have put alias gvim=/cygdrive/c/Program\Files\(x86\)/Vim/vim73/gvim.exe still
I wrote cyg-wrapper.sh for this sole purpose.
It supports:
symbolic links
options that must not be interpreted as pathnames (see -c with gvim, or any flags starting with a minus sign).
In other words, it enables us to type:
gvim /etc/profile -c /PS1 -c "echo 'correctly opened'"
# or even:
cd ~/tmp ; ln -s ~/bin/cyg-wrapper.sh
gvim -d http://hermitte.free.fr/cygwin/cyg-wrapper.sh cyg-wrapper.sh
explorer -e
explorer "$vim"
explorer http://hermitte.free.fr/
Note: I use the following function in my .profile to run gvim with cyg-wrapper.sh
gvim() {
opt=''
if [ `expr "$*" : '.*tex\>'` -gt 0 ] ; then
opt='--servername LATEX '
fi
cyg-wrapper.sh "C:/Progra~1/Edition/vim/vim73/gvim.exe" --binary-opt=-c,--cmd,-T,-t,--servername,--remote-send,--remote-expr --cyg-verbose --fork=2 $opt "$#"
}
EDIT: Currently (Sept 26 2014, using Vim 7.4), Windows gVim uses C:\Windows\gvim.bat to launch gVim from the command line. Replacing the gvim.exe path in the gvim() function with this script allows launching gvim without changing the path to match the current Vim version (which may actually be in Progra~2); however, it appears to also open a superfluous cmd.exe window.
Found this thread, I find the answer from jens unacceptable. We're not asking to be told not to do it. I didn't like the other answers either there was always some quirk, like settings not used or an extra command line window popping up. I Did some digging and this works for me. No extra command line box for nothing and it uses my proper gvim settings.
alias gvim='HOME=/cygdrive/p/ cygstart /cygdrive/c/Program\ Files\ \(x86\)/Vim/vim74/gvim.exe'
You simply need to alter the HOME to your own. To find out what to put there run gvim from windows then put in ":echo $HOME" and hit enter in my case it shows P:\ so that translates to /cygdrive/p/
Also if your gvim.exe is in a different directory/version you'll need to adjust.
Now when I type 'gvim script.sh' at a cygwin command prompt it launches gvim with the file, all nice and neat!
UPDATE
I found a slightly better way to do this. Using the alias was tying up my session that I ran the gvim from, I wanted it to launch as a separate process, using "gvim &" is inelegant as it lists job number when launching and displays a "done" line when completed. I'm too fussy so I figured out how to get that all tidy by using a function.
Just add this to your .bash_functions file, it builds on the previous section regarding home directory and backslash use.
gvim() {
ORIGHOME=$HOME
HOME=/cygdrive/p/
/cygdrive/c/Program\ Files\ \(x86\)/Vim/vim74/gvim.exe $1 & disown
HOME=$ORIGHOME
} 2>/dev/null
UPDATE 2 for babun users!
Ok since having wrestled with this originally I have ended up scrapping the original cygwin install in favor of babun which seems to be a less troublesome setup for those wanting linux functionality in windows without a full blown virtual. Of course my gvim launch script broke, and I had to do the following in my .zshrc file (babun uses zsh, at first I resisted and switched it to bash but then relented as I figured they must have reason, and they did, I like it)
gvim() {
OLD_HOME=$HOME
OLD_VIMRUNTIME=$VIMRUNTIME
export HOME=/cygdrive/c/Users/gmitchell/
export VIMRUNTIME="C:\Program Files (x86)\VIM\vim74"
TARGET=$(cygpath -w $1)
(/cygdrive/c/Program\ Files\ \(x86\)/Vim/vim74/gvim.exe $TARGET &)
export HOME=$OLD_HOME
export VIMRUNTIME=$OLD_VIMRUNTIME
}
*Note: the surrounding curved braces ( ) stops the job id from messing up your clean shell, and you no longer need the "disown"
P.S. The only remaining annoyance with this now is that you cannot "exit" the babun shell until all gvim instances you've launched are closed. Maybe someone can figure that own out. I've tried. When you type exit it just hangs there until you've exited all gvims.
Do yourself a favor, don't attempt to run a windows gvim from cygwin. The set of environment variables is likely completely different, and the pain of backslash directory separators, blanks in filenames, inability to understand /cygwin paths makes this an exercise in futility.
Then, what? Install the cygwin version of gvim and forget about all these problems.
Here is all you need to do:
alias gvim="run.exe /cygdrive/c/Programming/Tools/Vim/vim74/gvim.exe"
Works also with Notepad++, like so:
alias np="run.exe /cygdrive/c/Programming/Tools/Notepad++/notepad++.exe"
I have the same problem
because of the $SHELL var
so, I simple do like this
alias gvim='cmd /c "set SHELL=cmd & start gvim"'
It works for me, may be this will be help you too.
and maybe you want use the same alias for vim.
I put the following function in my .bashrc:
function gvim
{
GVIM_CMD=/cygdrive/c/path/to/gVimPortable.exe
if [[ -z "$1" ]]
then
$GVIM_CMD
else
$GVIM_CMD `cygpath -w $1`
fi
}
This allows me to launch gVim from anywhere.
It works fine for files with spaces, too...
This is a take off on low351's answer. I was unhappy with leaving HOME changed in the cygwin terminal, since I use that locally for a cygwin HOME, not my windows home. But gvim is all setup using Windows HOME. I added this to my .zshrc:
# run windows gvim from command line
gvim() {
local OLD_HOME=$HOME
HOME=/cygdrive/c/Users/jason/
local TARGET=$(cygpath -w $1)
/cygdrive/c/Program\ Files\ \(x86\)/Vim/vim74/gvim.exe $TARGET & disown
HOME=$OLD_HOME
} 2>/dev/null
really, just storing and restoring HOME. Being local, OLD_HOME goes away when the function returns, so it doesn't pollute the environment. If you're editing a cygwin file, running it through cygpath -w will make it a file path that windows gvim understands. It also lets you use window paths (e.g. C:/tmp/foo.txt) but w/ unix '/' separators, so you can dispense with /cygdrive/c. I believe this is all compatible w/ bash. This gives the following output:
> gvim
[2] 5060
>
It seems like the main problem is the HOME and VIM variables which are appropriate to the cygwin environment get exported when running Windows gvim, causing problems because Windows gvim knows nothing of cygwin. This fixes it:
alias gvim='env -u HOME -u VIM /cygdrive/c/Program\ Files\ \(x86\)/Vim/vim74/gvim.exe'
I have Windows gvim set as the default application (in Windows) for many filetypes, so Windows gvim frequently gets opened via the open alias. This fixes that use case:
alias open='env -u HOME -u VIM cygstart'
I just renamed gvim.bat which comes with the standard Win32 vim installation to gvim and put it into /usr/bin.
I am also sharing settings beetween Win32 GVim and Cygwin VIM referring to this article:
http://vim.wikia.com/wiki/Synchronize_configuration_to_many_computers
This way i can have both Vim and Win32 Gvim running with the same plugins and settings.
Since I've tried all of these and had issues I'll show what I'm using. I wrote it in ruby and it handles everything I throw at it (files, directories, or nothing) without any errors/popups/etc.
I saved it as gvim in ~/Dropbox/bin/cygwin/ (which is in my PATH)
#!/usr/bin/env ruby
args=''
filepath=''
arg=ARGV[0]
if arg
if File.exist? arg
if File.file? arg
args+="--remote-tab-silent "
end
filepath=`cygpath -w '#{arg}'`.strip
filepath="'#{filepath}'"
end
else
end
exe="'/c/Program\ Files\ \(x86\)/vim/vim73/gvim.exe' #{args} #{filepath}"
spawn exe
Here's the script I use for ~/bin/gvim. It runs it as if I called gvim FOO from the "Run" dialog (thanks to the batch file being invoked by explorer.exe). Can't play too nicely with the command line, but a self-deleting batch file comes to the rescue.
#!/bin/bash
TEMPFILE_NAME=gvim-`date +%s`-${RANDOM}.bat
TEMPFILE=$TMP/$TEMPFILE_NAME
TEMPFILE_W=`cygpath --windows --absolute --long-name "$TEMPFILE"`
TARGET=
if [ "$1" = "" ] ; then
TARGET=`cygpath --windows --absolute --long-name "."`
else
TARGET=`cygpath --windows --absolute --long-name "$#" | tr '\n' ' '`
fi
WIN_GVIM=`where gvim.exe | tr -d '\r\n'`
WIN_GVIM=`cygpath "$WIN_GVIM" --windows --absolute --long-name`
unix2dos > $TEMPFILE << EOF
start "gvim" "$WIN_GVIM" $TARGET
DEL %~f0
EOF
chmod +x $TEMPFILE
explorer.exe "$TEMPFILE_W"
Special thanks to this answer for the explorer.exe technique.