Search code examples
vimsudoemmet

How to use vim plugin:emmet-vim with sudo?


emmet-vim was installed in normal user this way.

mkdir .vim
mkdir .vim/plugin
mkdir .vim/autoload

git clone https://github.com/mattn/emmet-vim.git
cd emmet-vim
cp plugin/emmet.vim ~/.vim/plugin/
cp autoload/emmet.vim ~/.vim/autoload/
cp -a autoload/emmet ~/.vim/autoload/

Open vim input :scriptnames with a normal user.

  1: /usr/share/vim/vimrc
  2: /usr/share/vim/vim74/debian.vim
  3: ~/.vimrc
  4: ~/.vim/plugin/emmet.vim
  5: /usr/share/vim/vim74/plugin/getscriptPlugin.vim
  6: /usr/share/vim/vim74/plugin/gzip.vim
  7: /usr/share/vim/vim74/plugin/matchparen.vim

Open sudo vim and input :scriptnames with the root user.

  1: /usr/share/vim/vimrc
  2: /usr/share/vim/vim74/debian.vim
  3: /usr/share/vim/vim74/plugin/getscriptPlugin.vim
  4: /usr/share/vim/vim74/plugin/gzip.vim
  5: /usr/share/vim/vim74/plugin/matchparen.vim

All the features provided by emmet can be used in normal user.
All the features provided by emmet can not be used in root user.
How to use vim plugin:emmet-vim with sudo ?


Solution

  • Use sudoedit. Make sure your $EDITOR variable is properly set.

    sudoedit is the equivalent of running sudo -e which creates a temporary copy of the file. After changes are made the file is copied back to the original location. This allows you to use your own editor profile instead of the root user's.

    From the man page:

    -e' The -e (edit) option indicates that, instead of running a command, the user wishes to edit one or more files. In lieu of a command, the string "sudoedit" is used when consulting the security policy. If the user is authorized by the policy, the following steps are taken:

    1. Temporary copies are made of the files to be edited with the owner set to the invoking user.

    2. The editor specified by the policy is run to edit the temporary files. The sudoers policy uses the SUDO_EDITOR, VISUAL and EDITOR environment variables (in that order). If none of SUDO_EDITOR, VISUAL or EDITOR are set, the first program listed in the editor sudoers(5) option is used.

    3. If they have been modified, the temporary files are copied back to their original location and the temporary versions are removed.

    If the specified file does not exist, it will be created. Note that unlike most commands run by sudo, the editor is run with the invoking user's environment unmodified. If, for some reason, sudo is unable to update a file with its edited version, the user will receive a warning and the edited copy will remain in a temporary file.