I'm using CentOS and created a .vimrc file in my /home directory. I tested it out by creating a txt file and yes, that worked fine. Now, I have my project files in my /srv directory with SELinux turned on. I tried opening a file: vim README.txt and yes, my .vimrc settings are still being applied.
Now, since I'm in the /srv directory, simply doing vim means that my file is read only. So, I do sudo vim README.txt in order to be able to edit files. Now, the problem lies that once I do sudo, none of my .vimrc settings are applied. I tried creating a copy of .vimrc in the /srv folder but that didn't work either.
How do I apply .vimrc settings while using sudo?
Use sudoedit instead of sudo vim. You should be doing that anyway. Make sure your EDITOR environment variable is set to vim (probably already is, or vim is the default; you can set it in your .profile analog if need be).
As shown here, you can use the following:
sudo -E vim README.txt
From the man page:
-E The -E (preserve environment) option indicates to the security policy that the user wishes to preserve their existing environment variables. The
security policy may return an error if the -E option is specified and the user does not have permission to preserve the environment.
The accepted answer is the most secure. But this one is more flexible as I can use sudo -E operation with any operation, I don't have to configure anything else beforehand.
/root/.vimrc is the working directory of sudo vim.
You need copy your .vimrc file from /home/ec2-user/.vimrc to /root/.vimrc
The presented solutions in the other responses work but are not very practical, as you have to enter you password every time you want to edit a file.
I usually have a tmux session open within which I am rooted via sudo su, so I enter my password once at the beginning of the session and can then work for hours without having to enter it again.
I worked around the issue presented here by creating the following symbolic links :
sudo su
ln -s /home/MY-USER-NAME/.vimrc .vimrc
ln -s /home/MY-USER-NAME/.vim .vim
You might need to remove the /root/.vim/ directory first.
I hope this helps
Related
I'm trying to save my modified "menu.lst" file in vi. When I save the file, vi says: 'menu.lst' is read only.
How can I fix this?
The file you are trying to save is read-only, meaning you cannot modify its contents. It needs to be marked as writable. The process varies depending on your OS. Here are some helpful resources on how to change permissions of files:
For Windows 10: Nibbleguru: How to remove read-only attribute in Windows 10
For Linux (using chmod): TLDP: File Permissions
For macOS: Chron: How to Change File Permission From Read-Only to Read-Write on a Mac
EDIT:
As filbranden pointed out, for Grub's files, you should be opening vi using the sudo command. Grub's files are meant to be modified by root only. You should be opening your files using sudo vi menu.lst instead.
I have this on my vimrc
cnoremap w!! execute 'silent! write !sudo tee % >/dev/null' <bar> edit!
command! SaveAsRoot w !sudo tee %
cnoreabbrev sudow SaveAsRoot
For instant use, just copy to the clipboard and run:
:#+
:SaveAsRoot
The :#+ loads your clipboard into vim memory which allows you to run the given commands while not saved on your vimrc.
People suggest using sudo vi(m) on unix, but this could have unwanted consequences: all commands executed in this window are done by root and so you could accidentally do unwanted things like deleting file or just creating files owned by root.
Instead you should think about using sudoedit instead. It will copy the file to /tmp and open it in $EDITOR (if you are using vim, you should set it in your ~/.profile / ~/.bash_profile).
But beware: Something I stumbled across: the original file is only replaced when you close vim - no matter how often you save! (This is, because you are editing the file in /tmp and not the original).
It is not that I don't use sudo vim but if I do, I am extra cautious about what I do ;) I do it for example if I know that I will need to edit multiple files as root, or that I want to execute other commands from within vim as root (e.g. git)
And something even more important to me: sudo vim is using roots vimrc instead of mine, but with sudoedit I have my own config...
I looked at .vimrc configuration file does not load when editing with 'sudo', but the accepted answer doesn't work for me. I have put a test line echo "it works!" into my ~/.vimrc. When I edit a file as user, it works. When I edit a file as root it doesn't. I have linked .vimrc and .vim to the same in /root; so that, they show up there. I have tried sudoedit, sudo vim and su then vim, but nothing works.
I have done some more testing and found that vim -u /home/colin/.vimrc works while I'm root except that it doesn't find the color scheme. But, vim -u /root/.vimrc fails with:
E282: Cannot read from "/root/.vimrc"
So, then I tried to cat .vimrc and ls .vim while root in /root, and got
Too many levels of symbolic links
Once I got the Too many levels of symbolic links, I simply recreated the links. However, when the links were first created, I was in fact able to cat and ls them as root user from \root. So, I don't really know what the problem was or how it was really solved.
Here is what I did though:
su
vim -u /home/colin/.vimrc works but can't load plugins
vim -u /root/.vimrc failed with error message
cd ~ (which took me to \root since su in step 1 made me root)
cat .vimrc and ls .vim both failed and complained Too many levels of symbolic links
rm -r .vimrc .vim
ln -s /home/colin/.vimrc ./.vimrc
ln -s /home/colin/.vim ./.vim
exit
All I did is just create two simple soft links. Since you will not change the name of the .vimrc or .vim files, all you need is just add two pointers to your local user's configuration file. Try this in your terminal:
sudo ln -s ${HOME}/.vimrc /root/.vimrc;
sudo ln -s ${HOME}/.vim /root/.vim;
I have modified the .vimrc file in my user. The settings dont work when i switch to superuser.
When i check the contents of the file (using vim editor) i can see the mapped keystrokes of newlines
(with all the text extending on the rightside beyond screen on same line), but when i check the contents of .vimrc as root, i see all the text in the same window screen one below the other (this may be a very stupid difference, but that is the only difference i noticed. all the data in the file is same).
i am unable to understand what is wrong and how can i correct it.
root is a user too, when you start vim with root, vim load the root's Home/.vimrc usually it is /root/.vimrc
You can cp or ln -s your user's vimrc to /root if this is a personal desktop machine.
I hope I understood your problem.
You did not state a precise question, so for the sake of completeness :
If what you want is being able to modify files as root using your user's vim and .vimrc, you can do
sudo -e /path/to/your/file
sudo will use the editor configured in $EDITOR as the current user, to edit a temporary copy of the file that will get copied over when you write the file.
The caveat is that you will not be able to have any edition history between sessions. For example, if you modify /etc/group once, save the changes and quit, and then reopen the file again, you will not be able to undo the modification you did at first.
For Neovim users, you can do what comes next:
If your root user settings for neovim are important, make a
backup for those dotfiles.
Create a symbolic link from your default user to /root/.config directory.
sudo mv /root/.config/nvim /root/.config/nvim.bkp; sudo ln -s $HOME/.config/nvim /root/.config/
Be aware that some plugins may need some files that are not under
.config/nvim directory.
On Debian GNU/Linux there is also a shared configuration file in /usr/share/vim/vimrc. (Actually this is a soft link to /etc/vim/vimrc.) See the bottom of the man page on vim.
Vim stores the list of commands that we applied using : for the current execution.
But when I close vim and start it again, the vim command history is lost.
I tried set history = 1000 in the .vimrc file but that did not help.
Where does Vim maintain the local command history?
What is the command to retain command history?
Just an issue that caught me out the other day, which may or may not be your problem:
On some Linux systems (e.g. Ubuntu), if the very first time you run VIM, you run it as a super-user, then the $HOME/.viminfo file gets created with root owner and your local user does not have permissions to write to it. This explained why my VIM was not storing command history when it was all configured up correctly.
Bottom line: on a *nix system, locate your .viminfo file, and make sure you have read/write permissions on it.
To check whether Vim supports the 'viminfo' file (which stores the history), :echo has('viminfo'). The corresponding setting must not be empty: :set viminfo?, and :set history? should be greater than one.
If there's a problem writing the viminfo file (though Vim should complain in that case), you could try passing a different location via vim -i /tmp/viminfo
You should check the permissions of the .viminfo file. You might need to change owner of the file to your current user using chown or sudo chown.
Mr. Baint has given the answer.
Go to $HOME directory.
give ls -l .viminfo to check permissions.
change permission so that group and owner also can have write
permission. use:
sudo chown yourUserId $HOME/.viminfo
It should fix the issue.
You will have to set the viminfo option. Set it in your $MYVIMRC
Update To find out where the option was last set/changed:
:verbose set viminfo?
See http://vimdoc.sourceforge.net/htmldoc/starting.html#viminfo-file
If you exit Vim and later start it again, you would normally lose a lot of
information. The viminfo file can be used to remember that information, which
enables you to continue where you left off.
This is introduced in section |21.3| of the user manual.
The viminfo file is used to store:
The command line history.
The search string history.
The input-line history.
Contents of non-empty registers.
Marks for several files.
File marks, pointing to locations in files.
Last search/substitute pattern (for 'n' and '&').
The buffer list.
Global variables.
The viminfo file is not supported when the |+viminfo| feature has been
disabled at compile time.
You could also use Session files.
I went round in circles on this one a bit on Ubuntu and set viminfo solutions proposed above resulted in errors.
I eventually did the command "version" in the command mode and it came back with "-" for most stuff including:
-cmdline_hist
-cmdline_info
I ran the following command and it all worked fine again:
sudo apt install vim
I had the same problem. The issue was that I had vim-minimal installed (this is a default with Fedora), which does not support history. I had to uninstall it and install the full vim instead. I now have history... and syntaxic coloration !
If you are using a vimrc file, check your folders and files for existence and correctness
set history=200
set undolevels=128
set undodir=~/.vim/undodir/
set undofile
set undolevels=1000
set undoreload=10000
If the specified folder (undodir in my case) does not exist, the history will not be saved.
I am frequently opening files that are readonly, I would like to know if there is a way to make them writable from inside Vim. I do not mean :w!, I need the file to be writeable after I close Vim.
If you are on a unix based machine you can always just use the unix command.
:!chmod 777 %
http://ss64.com/bash/chmod.html
Otherwise on windows, you should look into the calcs command:
http://www.delawarepchelp.com/system/dos/calcs.htm
The issue comes from the fact that after changing the file attributes Vim will detect it and will try to reload the file and you will loose what you typed.
The best seems to create a function that redefines the autocmd FileChangedShell to do nothing when the attribute change is detected.
See this example of setting file attributes without reloading a buffer where an example is given for making all files executables.
This should do the trick for you
I found this solution elsewhere:
:set readonly!
:set modifiable
my situation was that vim was opening my file, which I chmod 777 from the commandline before opening, as readonly. Not sure if this helps you at all...
edit: forgot the second command. only the combination of the two worked.
you can use :! to execute shell commands. So you could change the user using :!chown myusername filename or change the access right using for example :!chmod o+w filename
Additionally, if the file was writeable only by another user (and you are not owner, so can't chmod it) you might map this command:
:command SuWrite %!sudo tee %
Of course, sudo -u username for other users
!sudo chmod 777 %
When it asks if you want to use the buffer, type "O" for ok.
You can use the SudoWrite command from the vim-eunuch plugin, which enables you to write to a file with sudo even if you forgot to open vim with sudo. It's a great way to eliminate that annoying behavior when you forget.