on a new install of Ubuntu 16.04.3 LTS, I am having trouble with vim jumping to the last position on file reopen. For some reason the mark '" only works when I do sudo vim file, otherwise it doesn't. Because of this, the following in my vimrc file (/etc/vim) isn't doing anything
if has("autocmd")
au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
endif
But this line works fine with sudo vi file? I've been at this for a few hours now and it's really bothering me. This works on other machines just fine though (without sudo) such as a Mac I have.
The last position in a file is stored in a mark. Marks are saved in ~/.viminfo. If you have a .viminfo in your home directory that is owned by a different user, vim can't write to it.
In your case you had a ~/.viminfo owned by root. Running sudo vim worked because vim was running as root, but your normal user didn't have permissions to update the file.
By deleting the root-owned ~/.viminfo you clear the way for vim to re-create the file on the next run, this time as your normal user.
Related
EDIT
The scenario changed. Sorry, for posting question a bit too early.
When opening a file (vim filename.ext, or :e filename.txt, etc.), when the CWD is ~/Sites/A, vim changes to another CWD (~/Sites/B).
I found out, that vim does this because of a file in my ~/.vim/view/ folder, called ~=+Sites=+B=+src=+filename.txt=.
The file inside ~/.vim/view/ contains:
let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0
argglobal
setlocal fdm=manual
setlocal fde=0
setlocal fmr={{{,}}}
setlocal fdi=#
setlocal fdl=0
setlocal fml=1
setlocal fdn=20
setlocal fen
silent! normal! zE
let s:l = 200 - ((13 * winheight(0) + 11) / 23)
if s:l < 1 | let s:l = 1 | endif
exe s:l
normal! zt
200
normal! 030|
lcd ~/Sites/B
let &so = s:so_save | let &siso = s:siso_save
doautoall SessionLoadPost
" vim: set ft=vim :
Why is the line lcs ~/Sites/B standing in this file? Inside ~/Sites/B never existed a file called filename.ext.
For the sake of completeness, this is my question, before I localized the error:
When i vimgrep something in a codebase located in ~/Sites/a (e.g. :vimgrep DateCell src/**/*) vim's current working directory changes to another directory ~/Sites/B.
Every subsequent call to vimgrep or another working-directory-aware-command fails or returns wrong results, because it is operating in the ~/Sites/B directory.
What could cause this?
What I do, to achieve the error / wrong behaviour:
cd ~/Sites/A
vim
vimgrep something src/**/*
:!pwd shows ~/Sites/B
:quit
pwd shows ~/Sites/A
EDIT
It turns out, that the directory change also happens if I don't use vimgrep. I still don't know what produces the error.
BTW: It is always the same directory (~/Sites/B), vim changes to. No matter in which directory i am working.
Vim Version: 8.0.2
OS Version: OS X 10.10.5
The ~=+Sites=+B=+src=+filename.txt= is a saved view for ~/Sites/B/src/filename.txt. The question is: Why does it get loaded when you open a file (with the same name, or any, not entirely clear from your question) from another directory?
Normally, views have to be created and restored manually. You seem to have something in your configuration (:autocmd) that automatically restores saved views, and that something has a bug in its path lookup (or, less likely, your filesystem is messed up and you have symlinks there). Find that, and turn it off / fix it.
When I quit vim, the display reverts to whatever I was seeing before I entered. On non-vim vi, you keep the vi screen intact except for the bottom line.
Is there an option in vim that allows that latter behaviour?
Vim uses a terminal feature called the alternate screen to write its UI there, and restore the original shell contents (where Vim was launched from) on exit. This is controlled by two ANSI escape sequences (see how does vi restore terminal content after quitting it).
You can disable that from within Vim by clearing the corresponding terminal settings. Put the following into your ~/.vimrc:
set t_ti= t_te=
Alternatively, you could also disable this capability in the terminal; at least the multiplexers screen and tmux allow this. See Prevent Applications Like Vim and Less Clearing Screen on Exit for details.
May be this is what you are looking for, just paste the below commands in your .vimrc. I got it from some where in the net.
" Return to last edit position when opening files
autocmd BufReadPost *
\ if line("'\"") > 0 && line("'\"") <= line("$") |
\ exe "normal! g`\"" |
\ endif
I may have accidentally changed some setting but now I can't figure out what it is.
Behavior:
Whenever I edit an existing file, Vim starts in replace (R) mode. This is new as of this week.
Whenever I do vim nonExistantFile.txt, Vim starts in normal mode
Steps so far to diagnose:
/etc/vimrc has nothing relevant, and specifically does not contain startreplace
/home/<myuser>/.vimrc has nothing relevant, and specifically does not contain startreplace
alias shows nothing vim related
Platform info:
Vim version 7.3.1314
Windows 7 64-Bit
32 Bit Cygwin and Vim binary
Other potentially relevant information:
Some time ago (before this started happening) I copied /etc/vimrc from my CentOS 6.4 machine into the Cygwin /etc/vimrc
I edited some binary files recently
I'm pretty stumped, I can't think of anything else to try.
Update:
I have narrowed the problem down to this /etc/vimrc snippet
Oddly commenting out EITHER of these two commands solves the problem
Vimrc snippet (note: this does have unix line endings):
" Only do this part when compiled with support for autocommands
if has("autocmd")
"<snip>
" When editing a file, always jump to the last cursor position
autocmd BufReadPost *
\ if line("'\"") > 0 && line ("'\"") <= line("$") |
\ exe "normal! g'\"" |
\ endif
" don't write swapfile on most commonly used directories for NFS mounts or USB sticks
autocmd BufNewFile,BufReadPre /media/*,/mnt/* set directory=~/tmp,/var/tmp,/tmp
"<snip>
endif
I mapped nnoremap <ESC> :nohlsearch<CR> in my .vimrc and that caused this behaviour.
Mapping this to <Enter> solved the problem for me.
check ~/.exrc files for initial settings, there must be a flag setting your vi into REPLACE mode at startup!
http://alvinalexander.com/unix/edu/un010003/
cheers!
I added a few newlines and swapped the two commands in /etc/vimrc that seemed to be causing the problem... and this seemed to fix it. I have no idea why.
The following seemed to fix this problem for me. I think that there was some autocommand code that interfered with the cursor position restore, so clearing out previous autocommands as below fixed the problem for me:
function! ResCur()
if line("'\"") <= line("$")
normal! g`"
return 1
endif
endfunction
augroup resCur
autocmd!
autocmd BufWinEnter * call ResCur()
augroup END
http://vim.wikia.com/wiki/Restore_cursor_to_file_position_in_previous_editing_session
I can open a gvim session and drag&drop a folder while holding the shift key to change pwd to the dropped folder. This works fine but I was wondering if it's possible to do the same without first opening a gvim (I can drop a folder on a gvim icon on my desktop to start a gvim session with the given folder but it does not change the working directory even when I hold shift key).
If relevant, I'm using an ubuntu system with unity desktop and also NERDTree plugin.
Add this to your .vimrc:
au VimEnter * if isdirectory(expand('%')) | cd % | endif
I have the following command run as a vim plugin under ~/.vim/plugin/autohighlight.vim
:autocmd CursorMoved * exe printf('match IncSearch /\<%s\>/', expand('<cword>'))
The thing is, it throws a bunch of errors anywhere except when I'm editing a file. (file Explorer, other windows)
Is there a way to tell the script to only take effect when a file is edited and not anywhere else in VIM?
You can try this one:
autocmd CursorMoved * :if filewritable(#%)==1 |
\ call matchadd('IncSearch', '\V\<'.escape(expand('<cword>').'\>', '\'), 10, 1) |
\endif
It will do highlighting only if current buffer name is a writable file (not directory). It will fail if you are editing something which uses BufWriteCmd to perform saving (for example, if you are editing file inside a zip archive).
By the way, can you provide these errors? I was able to get an error when I used :e ., but it was not related to the fact that you are observing a directory, it appeared just because you forgot to do escaping. If you have written escape(expand('<cword>'), '\/') instead of expand('<cword>') then such error would not appear.