Why 'sudo vim' Could Hurt Your Productivity
2016-08-30 - By Robert Elder
The .viminfo File Is Created As Root
Updated 2016-12-11: Made a correction to ownership changing command to make it work on a larger range of systems.
The first time you use vim, the file '~/.viminfo' is created, and if you use 'sudo vim' the first time you use vim after installing it on a fresh system, the permissions on ~/.viminfo will have the owner set to root instead of the default user.
That's a problem, because when vim is started as a normal user it can't write to this file, and every time you close vim it forgets:
- The last thing in the copy/paste buffer
- The last search term
- Any of your recent regexes
- Lots of other stuff
If you're a vim power-user you are probably used to pressing 'up' to get your last regex, or any other hard to type ex command:
Or you probably enjoy being able to open a file and press 'n' to jump directly do whatever you searched for last. Instead you now get 'E35: No previous regular expression.':
Solution
Run this command:
sudo chown $(id -nu):$(id -ng) ~/.viminfo
More Details
When you use 'sudo', the current user changes, but the value of $HOME does not. Since $HOME doesn't change '~' expands to '/home/user' instead of '/root', and root's .viminfo gets created at '/home/user/.viminfo'. If you did 'sudo -i vim', then it would go to '/root/.viminfo' instead because the '-i' flag on sudo also changes the $HOME environment variable to be '/root'.
I felt this was worth sharing because it took me quite a while to figure this problem out. It's the kind of thing that doesn't show up well if you Google for the symptoms, and I originally assumed it must have been related to different vim versions and just put up with it. It wasn't until I did a bit more digging that I realized it was so easily fixable all this time!
2021-12-21 Update
I recently received an email suggesting that making a habit of using 'sudoedit' instead of 'sudo vim' would eliminate this problem entirely. I attempted to verify this alternative solution, but I found that now in my version of Ubuntu 20.04 the original problem for which I wrote this article no longer seems to happen. Specifically, using 'sudo vim' no longer seems to cause the same permissions based problem. In addition, I also noted that 'sudoedit' on my system appears to be symlinked to 'sudo' so it may not make a difference on my system, but I will include this note for completeness since it may make a different on other *nix systems.
Using A Piece Of Paper As A Display Terminal - ed Vs. vim
Published 2020-10-05 |
$1.00 CAD |
Use Vim Inside A Unix Pipe Like Sed Or AWK
Published 2016-04-05 |
XKCD's StackSort Implemented In A Vim Regex
Published 2016-03-17 |
Can You Use 'ed' As A Drop-in Replacement For vim, grep & sed?
Published 2020-10-15 |
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21 |
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01 |
The Most Confusing Grep Mistakes I've Ever Made
Published 2020-11-02 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|