Butterfly Effect-Vim autocmd use exception caused by ' sudo-s ... '

Source: Internet
Author: User

1. Background information

Since joining Redhat, I have switched the default login user Veli of the home desktop (Ubuntu 16.04 LTS) to Huanli, primarily to align with the company's computer configuration to facilitate future WFH, but have triggered a vim usage exception. In my. VIMRC, there is a configuration code that

ifHas ("Autocmd")  "When editing a file is always the known cursor position.Autocmd Bufreadpost *    \ ifLine"' \ '") >0&& Line ("' \ '") <= Line ("$") |\ exe"normal g ' \ ""|\ endifendif"Has ("Autocmd")

The function of this code is to remember the position of the previous cursor, and then open the file automatically to jump there. This is a very useful feature for programmers who prefer to use VIM. When I switched to Huanli user, this function inexplicably failed the Orz ...

2. Fault debugging

201 -based on different versions of VIM comparison test: Vim version should be supported by default Autocmd function, but in order to exclude the version problem (the current version is 7.4.1689), so download a vim8.0 source package, compile and install to/usr/local , and found that the problem still exists.

202 -A comparison test based on different hosts: using the same version of Vim on a notebook, finding that the laptop is working properly, working on the desktop is abnormal, and thus determining that the Vim version (7.4.1689) is not a problem, it must be an exception caused by a certain setting on the desktop . The procedure is as follows:

(1) At the same time on the notebook and desktop terminal with vim open a file, e.g.

$ vim/tmp/foo.c

Execute in VIM

Autocmd Bufreadpost *

Compare and analyze the output and find the exact same. The sample output is as follows:

This shows that Autocmd is working correctly on a desktop that has a problem with vim . The problem, then, must be that the records produced by Autocmd are not preserved .

203 -VIM verbose output analysis, found the key files. Viminfo

$ vim-v/tmp/foo.cchdir (/tmp) Fchdir () to previous dir ... <snip>, ..... Searching for "/usr/share/vim/vimfiles/after/pack/*/start/*" Searching for "/home/huanli/.vim/after/pack/*/start/*" Not found in ' Packpath ': "pack/*/start/*"Reading viminfo file "/home/huanli/.viminfo" info oldfilesPress ENTER o R Type command to continue

Therefore, we can boldly make the following guesses,Autocmd in the normal work, the last cursor position has been autocmd recorded, but because unknown reason is not saved to the. Viminfo.

204 -View. Viminfo permissions, found Owner:group is root:root (instead of Huanli:huanli), and found the vim of the abnormal root cause.

ls  -L ~/. Viminfo165331:/home/huanli/.viminfo

Obviously, the normal user Huanli does not have permission to modify the. viminfo file, so the Autocmd memory results cannot be saved to the next time vim opens the file for use. The solution is surprisingly simple,

sudo chown Huanli:huanli/home/huanli/.viminfo

But what is the culprit that caused this vim anomaly? That is, why does the . viminfo file be saved under the/home/huanli directory instead of the/root when using root for vim editing?

205 -View the environment variable when using root user home, Discover Home=/home/huanli instead of/root

Env grep HOME~home=/roothome=/home/huanli

Well, it turns out that home is not set to/root, although ~home is/root. So, the culprit found, it is called so alias.

grep ' ~home ' aliasso ='sudo-s ~home=/root'

This is related to my usual usage habits, because I set the color PS1 and alias so in the normal user (Huanli), so it's easy to switch to the root user and keep the color PS1.

So, This is the typical butterfly effect, a "sudo-s ~home=/root" caused by the vim use of the exception!!

3. Concluding remarks

The whole trouble shooting process is actually full of fun, although it is more difficult. I almost gave up at the end of the 202 step, because to coax the baby to sleep. Then calmly think about it, estimated to be related to storage (in Inspur to do the storage or useful). So through the most critical ' vim-v ' found root cause (P.S verbose output for software debugging is a must kill skill). software debugging and fault diagnosis is actually similar to the doctor, the basic idea is to use the elimination method. Of course, you also need to be bold to guess, reliable guess, the more experienced, guess the faster , guess the more reliable. the older the doctor the more valuable, theoretically speaking, the programmer is ah, hehe ...

Butterfly Effect-Vim autocmd use exception caused by ' sudo-s ... '

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.