vim and sudo vim using different settings on openSUSE - linux

I want vim to save folded code after I've closed the file. I've added the following code to both /etc/vimrc and ~/.vimrc:
au BufWinLeave ?* mkview
au BufWinEnter ?* silent loadview
When I open a file as a regular user (vim file) it works as expected. If I instead open with sudo vim file it doesn't save folded code.
I know /etc/vimrc is being read. My theme is set at the bottom of that file and is working fine. I ran into a similar problem last week installing the vim surround plugin. When that was installed in ~/.vim it only applied to vim file. To get it working with sudo I had to install it separately in /usr/share/vim/current. What could be causing this?

Prolog
In fact you answered your question, so this is indeed the same issue you had before. It is caused by the algorithm Vim uses to find its configuration (see :help initialization). But I wouldn't call your solution a correct one. See :help $VIM for information how you can use your configuration in sudo environment (e.g. being root).
As for your current issue.
See the following quotes from the documentation. :help :mkview:
:mkvie[w][!] [file] ...
When [file] is omitted or is a number from 1 to 9, a
name is generated and 'viewdir' prepended.
...
And :help 'viewdir':
'viewdir' 'vdir' string (default for ... for Unix: "~/.vim/view", ...
So after doing sudo all views are stored by Vim at /root/.vim.
Solution
Make viewdir point to your ~/.vim/view directory, with something like the following in /root/.vimrc:
:set viewdir=/home/user/.vim/view
Though, this approach has some issues itself:
Updating views saved by root will cause permission errors.
You wont be able to pass stored views for files under /root or /home/user, because Vim generates file names with tilde instead of full paths.
The first issue can be solved by running chown or chmod on new view file right after executing :mkview command. It should be something like:
execute '!chown user:group' eval('&viewdir').'/'.substitute(expand('%:p:~'), '/', '+=', 'g').'='
But I don't know good solution for the second issue, can only suggest writing a script to convert file names to full paths.

Related

Vim(Gvim) - How do I install a script on Windows 7?

I want to install this closetag.vim script:
http://vim.sourceforge.net/scripts/script.php?script_id=13
It says
place this file in your standard vim scripts directory and source it
while editing the file you wish to close tags in.
And this is shown as an example:
:let g:closetag_html_style=1
:source ~/.vim/scripts/closetag.vim
1) What is my standard vim scripts directory on W7?
I have neither .vim nor scripts folder on my system. And if I have\am expected to create one (or ones) where should it (they) be placed? %ProgramFiles%\Vim\vim80 or %ProgramFiles%\Vim\vimfiles or maybe somewhere else?
1.1) Also, this might be a silly thing to ask about, but why do I keep seeing that tilde in path almost every time I read about Vim. Does it mean that Vim is used primarily by Mac/Linux people? Why is that?
2) What does it mean to source the script? Run a command like this let g:closetag_html_style=1 in command mode in Vim?
Btw what does style=1 mean here?
And if I want it to work by default for all html\xhtml\xml files, what do I do? Put this command to _vimrc file?
Thank you so much!
You could find the answers to all your questions just by reading the plugin description carefully. Unfortunately, it is both poorly written and factually incorrect.
What is my standard vim scripts directory on W7?
On Windows, you are supposed to put custom and third-party scripts in various places under:
C:\Users\username\vimfiles\
But that's not what the author means by "standard vim scripts directory". What he is referring to is this:
C:\Users\username\vimfiles\scripts\
which is not standard at all.
Also, this might be a silly thing to ask about, but why do I keep seeing that tilde in path almost every time I read about Vim. Does it mean that Vim is used primarily by Mac/Linux people? Why is that?
Yes, Vim is primarily used by UNIX-like systems users. Because of history.
What does it mean to source the script? Run a command like this let g:closetag_html_style=1 in command mode in Vim?
No. Read your question again.
Btw what does style=1 mean here?
Nothing.
But :let g:closetag_html_style=1 means "set the g:closetag_html_style option to true".
And if I want it to work by default for all html\xhtml\xml files, what do I do? Put this command to _vimrc file?
No. This is explained on the plugin's page:
For greater convenience, load this script in an autocommand:
:au Filetype html,xml,xsl source ~/.vim/scripts/closetag.vim
Which is wrong on many levels.
Here is what you actually have to do to use that script:
Save the closetag.vim script to the following location:
C:\Users\username\vimfiles\scripts\closetag.vim
Create vimfiles\ and/or vimfiles\scripts\ if they don't exist.
Add the lines below to C:\Users\username\_vimrc:
augroup closetag
autocmd!
autocmd Filetype html,xhtml,xml,xsl runtime scripts/closetag.vim
augroup END
let g:closetag_html_style = 1
Reference:
:help startup
:help :source
:help :runtime
:help :let
:help autocommand

How to jump back to netrw when splitbelow is on

If I open a file in a current directory using netrw and then reopen the netrw using :Sexplore the netrw window opens below (I have splitbelow set), but the cursor remains in the file being edited and I cannot move to the netrw window using CTRL-W combinations.
Am I doing something wrong or is this a bug?
Steps to reproduce
run vim .
netrw opens, highlight a file in the current directory (let's say test.txt) and press Return, file test.txt opens
run :Sexplore cmd
netrw opens below but the cursor remains in the file opened previously.
My .vimrc
set nocompatible
set splitbelow
VIM version
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Dec 8 2015 23:11:08)
MacOS X (unix) version
Included patches: 1-944
This is how it looks like:
I cannot reproduce with version 154a (use :echo g:loaded_netrwPlugin to check your netrw version).
Try with the latest version.
Edit:
It is possible that the problem is cause by some other plugin interference or settings. You could try following the approach described at Vim FAQ 2.5:
2.5. I have a "xyz" (some) problem with Vim. How do I determine it is a
problem with my setup or with Vim? / Have I found a bug in Vim?
First, you need to find out, whether the error is in the actual
runtime files or any plugin that is distributed with Vim or whether it
is a simple side effect of any configuration option from your .vimrc
or .gvimrc. So first, start vim like this:
vim -u NONE -U NONE -N -i NONE
This starts Vim in nocompatible mode (-N), without reading your
viminfo file (-i NONE), without reading any configuration file (-u
NONE for not reading .vimrc file and -U NONE for not reading a .gvimrc
file) or even plugin.
(...)
If the error does not occur when starting Vim this way, then the
problem is either related to some plugin of yours or some setting in
one of your local setup files. You need to find out, what triggers the
error, you try starting Vim this way:
vim -u NONE -U NONE -N
If the error occurs, the problem is your .viminfo file. Simply delete
the viminfo file then. If the error does not occur, try:
vim -u ~/.vimrc --noplugin -N -i NONE
This will simply use your .vimrc as configuration file, but not load
any plugins. If the error occurs this time, the error is possibly
caused by some configuration option inside your .vimrc file. Depending
on the length of your vimrc file, it can be quite hard to trace the
origin within that file.
The best way is to add :finish command in the middle of your .vimrc.
Then restart again using the same command line. If the error still
occurs, the bug must be caused because of a setting in the first half
of your .vimrc. If it doesn't happen, the problematic setting must be
in the second half of your .vimrc. So move the :finish command to the
middle of that half, of which you know that triggers the error and
move your way along, until you find the problematic option. If your
.vimrc is 350 lines long, you need at a maximum 9 tries to find the
offending line (in practise, this can often be further reduced, since
often lines depend on each other).
Obviously you will have to load netrw manually (which can be done by sourcing plugin/netrwPlugin.vim -- if you are using some plugin manager you will have to move the plugin to ~/.vim/ or change your runtime path due to the autoload functions) and set nocompatible and splitbelow.
Actually there is an adapted version of this procedure in :help netrw-debug:
Step 2: assuming that you've installed the latest version of netrw,
check that your problem is really due to netrw. Create a file called
netrw.vimrc with the following contents:
set nocp
so $HOME/.vim/plugin/netrwPlugin.vim
Then run netrw as follows:
vim -u netrw.vimrc --noplugins [some path here]
Perform whatever
netrw commands you need to, and check that the problem is still
present. This procedure sidesteps any issues due to personal .vimrc
settings and other plugins. If the problem does not appear, then you
need to determine what setting in your .vimrc is causing the conflict
with netrw or which plugin.
Step 3: If the problem still is present, then get a debugging trace
from netrw:
(...)

Trouble with vimrc file recognition with MacVim

I'm having difficulty getting MacVim (7.3-64) to recognize my .vimrc and .gvimrc files since upgrading to OS X 10.7.3. Previous, I've simply symlinked my .vimrc and .gvimrc using these commmands:
$ ln -s ~/.vim/vimrc /usr/local/Cellar/macvim/7.3-57/MacVim.app/Contents/Resources/vim/.vimrc
$ ln -s ~/.vim/gvimrc /usr/local/Cellar/macvim/7.3-57/MacVim.app/Contents/Resources/vim/.gvimrc
However, when I currently symlink my rc files, I can not get MacVim to recognize them. I've installed MacVim via Homebrew. Does anyone have any suggests as to what the problem could be?
The ~/.vim/ folder (for colorschemes and scripts) and the ~/.vimrc file (for custom settings and mappings) are guaranteed to work on every UNIX-like systems but neither of these are required for Vim (and MacVim) to work properly.
Start without symlinks or whatever : no .vim folder and no .vimrc or .gvimrc in your home folder. Does MacVim work? You are supposed to see a window with some introductory text, do you see that?
Quit MacVim and turn to the original .(g)vimrc files you want to use: where are they located? Where did you get them from? What is their content? Do you actually, really need them? Do their names actually start with a dot (do you see them or not in the Finder)? What is their encoding (in vim, :set fileencoding? and in the terminal, $ file /path/to/original/.vimrc)? Please, paste the content of the .vimrc file you want to use in your question.
If you are absolutely certain you need these files to work efficiently in Vim and assuming you actually have a custom .vimrc somewhere on your Mac, open Terminal.app and type this command (without the $):
$ cp /path/to/original/.vimrc ~/.vimrc
to copy your .vimrc to the canonical location.
Now launch MacVim. What do you see? You are supposed to see a window with some introductory text, is that what you see?
If MacVim doesn't work correctly with your ~/.vimrc you might want to comment its content, relaunch MacVim, uncomment a few lines and so on until you eventually find a bad setting.
Just put your .vimrc and .gvimrc into your home directory, i.e. ~/ It's picked up there allright.
BTW, you don't need to link to the homebrew dir. Those rc files are default files which you can override with your local user files.

What is the issue with vim -u /path_to/vimrc?

I share an user with other people.
Everyone has created a directory into home directory and everyone is working in his "own" directory.
I want to use my own setting when I use vim and I don't want to bother others with my preferences.
I created my .vimrc file into $HOME/my_directory
I've defined an alias my_vim="vim -u /full_path_to_home/my_directory/.vimrc"
When I edit a file with my_vim, I don't have the right colors.
I have the same problem when I use the command
:source /full_path_to_home/my_directory/.vimrc
If I copy my .vimrc file into $HOME directory, everything is fine.
Where is the problem ?
From :help vimrc
If Vim was started with "-u filename",
the file "filename" is used.
All following initializations until 4.
are skipped.
So by specifying a vimrc file, its ignoring the system-wide vimrc (/erc/vimrc/) where syntax highlighting and other things are configured. You can work around this problem by adding the following code to the top of your vimrc:
if filereadable("/etc/vimrc")
source /etc/vimrc
endif
If this sort of thing comes up a lot, I would recommend changing your $HOME to point to the current $HOME/my_directory whenever you log in.

Vim :e starting directory?

I code in Vim, not an IDE.
My source code is often nested 2-3 directories deep.
~/foo$ find
xyz
bar/abc
bar/def
~/foo$ vim
// inside of vim
:e bar/abc
... some work ...
:e <-- is there a way I can have this :e start in ~/foo/bar instead of ~/foo ?
Basically, I want :e to start the directory in "pathname of last edited file"
Thanks!
There's a lot of reasons not to like autochdir as it messes up some plugins and if you end up doing :e ../../../foo.txt you are not gaining anything. Just as an idea try this cmap I knocked up
:cnoremap red edit <c-r>=expand("%:h")<cr>/
then you can type :red and get
:e /the/path/to/your/current/files/dir/
(edit: perhaps use z instead of red as there are commands that start with red)
To expand the topic, also check out the FuzzyFinder plugin and some custom mappings to rapidly jump to common files you are always editing. Eg
10 or so of your regular files should be no more than 2 keystrokes away. It helps if they are systematically named
Here's an idea I use for django.
http://code.djangoproject.com/wiki/UsingVimWithDjango#Mappings
Try the autochdir option. It will automatically change the current working directory to whatever file was most recently opened or selected. In .vimrc:
set autochdir
For more info, :help autochdir
To always change the working directory to the current file's directory I have this in my .vimrc:
if has("autocmd")
autocmd BufEnter * :lcd %:p:h
endif " has("autocmd")
Sorry, but vim's :edit command takes a path which is interpreted relative to the present working directory of the vim instance.
You do have a :cd command which you could use to :cd bar then work for a while, then :cd ...
Hope that help some.
Some time ago I asked questions related to this on the vim mailing list: http://www.mail-archive.com/vim_use#googlegroups.com/msg03266.html Maybe you will find useful tips in that thread.
I tested a lot of plugins, but since CLI based GUIs are not my taste, I simply ended up using standard vim with a few configuration settings.
As honk pointed out, this line sets the working directory to the same as the file your working on:
autocmd BufEnter * lcd %:p:h
My other tip is to use the wildmenu. It makes it easier to get an overview of the files in your current directory when you go :e and then TAB. I'm a python programmer so the last line shows how to hide auto generated files that the python interpreter spits out, but you could use it to hide java .class files or c .obj files or whatever.
set wildmode=list:longest
set wildignore=*.pyc,*pyo
:cd changes directory
:pwd prints the current one.
why not just :E? Explore directory of current file.
:help :E
This isn't exactly what you wanted, but check out NERDTree.
On vim/gVim I just have cd C:/blah/blah at the top of my vimrc. I imagine it works on all platforms.
I personally use vagrant for each project so one CD is enough, but I think you can get vim to use different config files too, -u flag I think.
Or map a key to each project you have so pressing Ctrl+F1 does cd path/to/project/1 and Ctrl+F2 does cd path/to/project/2 perhaps?
Note: I don't use any plugins

Resources