I have the following setup:
mkdir /1
mkdir /1/2
mkdir /1/2/3
ln -s /1/2/3 /1/3
If I do cd /1/3, and then pwd, I get /1/3. If I use pwd -P, I can get /1/2/3, or pwd -L to force /1/3.
In VIM, I'm looking for a way to get the /1/3.
If I open a file in /1/3/foo.txt, and I use something like fnamemodify(bufname(winbufnr(0)), ':p:h'), it returns /1/2/3.
How can I tell it to give me the same directory that pwd would give?
It appears you can't, other than via system('pwd -L'). According to the vim_use mailing list Vim automatically resolves symlinks nowadays.
See the text around :h E773 for rationale; if Vim went by symlinks instead of resolved filename, it'd be possible to have the same file open in two buffers under two different names, and Vim would become confused trying to figure out where the swap file should be. See also in :h version7.txt:
Unix: When editing a file through a symlink the swap file would use the name
of the symlink. Now use the name of the actual file, so that editing the same
file twice is detected.
Short answer:
You may be able to use mount binding as a substitute for symlinks. See man mount.
Long answer:
I had a similar problem, as I have a short symlink to a mounted partition,
/e -> /media/iam/ext4test
I also have a symlink ~/.vimrc -> /e/configs/.vimrc.
I was running into trouble trying to pop into Netrw in the containing directory (I was landing in ~, but I couldn't see a robust way to avoid that, keeping in mind the desire to use Bookmarks, etc).
My solution was, after considering possibly changing the mount point, is that you can add mount points. So after unlink e, I used mount --bind /media/iam/ext4test /e.
Now, if I am in /e/configs/.vimrc and use :edit . (or :e. etc), it will pop me into Netrw in the containing directory.
Edit:
The command mount --bind makes transient changes. For a permanent bind mount, I add the following to /etc/fstab,
# <file system> <mount point> <type> <options> <dump> <pass>
/media/iam/ext4test /e none bind 0 0
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 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.
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
Need help. I have been editing a text file in vi , and i was able to save the changes before but now i am getting the above error whenever i am typing command to save the file. Please help .
You can, as vi (or, more likely, vim) is saying force a write by doing:
:w!
Alternatively, write the file to an alternative location:
:w! /path/to/other/location
To check where your backup files are being written (normally):
:se backup? backupdir? backupext?
Try to touch a file in that directory and see if your Operating System gives you an error to enlighten you as to why your editor cannot write there.
Had the same problem. Tried all options as above but it did not work. Then when I checked my disk space, it was full. Once I cleared some space then I was able to write back to file again.
P.S: This was in linux.
Had the same problem. It was because I installed vimconf as root. You need to change rights of files in ~/.vim directory and change owner to your user.
sudo chmod 0750 ~/.vim
sudo chown user ~/.vim
I had this same problem. Turns out it was due to running out of disk space. try creating a file using Ex) touch test.txt. If you get a message saying touch: cannot touch test.txt: No space left on device you will need to clear up space on your disk
I don't know what the cause was, but I moved by backupdir from . to ~/.vim/backups in my .vimrc and it solved it for me:
set backupdir=~/.vim/backups
I'd imagine some sort of tool was using the folder the file I was editing it in (Visual Studio 2013, in my case), but I'm not sure.
I've fixed this with:
sudo chown {user} ~/.cache/vim/* -R
where the "{user}" field is your user-name.
from within vi, try:
:w!
:help w! gives the following information:
*:w!*
:[range]w[rite]! [++opt] {file}
Write the specified lines to {file}. Overwrite an
existing file.
In my case my disk was full.
Here are some commands to verify this and find where the disk space is being taken. In my case it was the PHP log at over 20GB.
# see general disk space usage
df -h
# see current file and directory disk space usage. You can go to / and work your way in
du -sh *
Backup location can be given in .vimrc, e.g.:
set backupdir=~/.vim/backup
You may need to create this directory yourself.
Another possibility is that you put your backups in a centralized location (e.g. /tmp) and you edited a particular file as root. Then, the backup file will be owned by root and un-writeable by you as a mere mortal later.
In this case, the suggestion above to touch /tmp/atestfile won't show the problem! You'll have write permissions, no problem, but not to the particular backup file vim is trying to write.
Remove the file as root (sudo rm).
Note that the reason w! works is because vim writes the file without writing a backup file (you're insisting that it write despite the error).
I just started using nvim and I found my issue was that my borrowed premade vimrc file had a preset source in it.
grep -rnw ~/.config/nvim/ -e backup
.config/nvim/lua/custom/vimrc:132:set backupdir=~/.backup/,/tmp/
Like others here, creating that backup directory cleared my error message (~/.backup/)
I have the following setup:
mkdir /1
mkdir /1/2
mkdir /1/2/3
ln -s /1/2/3 /1/3
If I do cd /1/3, and then pwd, I get /1/3. If I use pwd -P, I can get /1/2/3, or pwd -L to force /1/3.
In VIM, I'm looking for a way to get the /1/3.
If I open a file in /1/3/foo.txt, and I use something like fnamemodify(bufname(winbufnr(0)), ':p:h'), it returns /1/2/3.
How can I tell it to give me the same directory that pwd would give?
It appears you can't, other than via system('pwd -L'). According to the vim_use mailing list Vim automatically resolves symlinks nowadays.
See the text around :h E773 for rationale; if Vim went by symlinks instead of resolved filename, it'd be possible to have the same file open in two buffers under two different names, and Vim would become confused trying to figure out where the swap file should be. See also in :h version7.txt:
Unix: When editing a file through a symlink the swap file would use the name
of the symlink. Now use the name of the actual file, so that editing the same
file twice is detected.
Short answer:
You may be able to use mount binding as a substitute for symlinks. See man mount.
Long answer:
I had a similar problem, as I have a short symlink to a mounted partition,
/e -> /media/iam/ext4test
I also have a symlink ~/.vimrc -> /e/configs/.vimrc.
I was running into trouble trying to pop into Netrw in the containing directory (I was landing in ~, but I couldn't see a robust way to avoid that, keeping in mind the desire to use Bookmarks, etc).
My solution was, after considering possibly changing the mount point, is that you can add mount points. So after unlink e, I used mount --bind /media/iam/ext4test /e.
Now, if I am in /e/configs/.vimrc and use :edit . (or :e. etc), it will pop me into Netrw in the containing directory.
Edit:
The command mount --bind makes transient changes. For a permanent bind mount, I add the following to /etc/fstab,
# <file system> <mount point> <type> <options> <dump> <pass>
/media/iam/ext4test /e none bind 0 0