9

Is there something I can put in my exrc file to prevent vi from going to the last line I was on last time I had the file open and just set the cursor at the top by default?

I think it is distro-specific -- it doesn't behave like that on Solaris but does on RHEL.

6 Answers 6

8

This feature is implemented as autocommand. It is set up in /etc/vimrc - see snippet below. Remove it from there or add command to remove that autocommand to your vimrc file. (I am using fedora - on rhel it should be very similar)

if has("autocmd")
  augroup fedora
  autocmd!
  "...
  " When editing a file, always jump to the last cursor position
  autocmd BufReadPost *
  \ if line("'\"") > 0 && line ("'\"") <= line("$") |
  \   exe "normal! g'\"" |
  \ endif
  "...
  augroup END
endif

If you do not have permissions or do not want to change /etc/vimrc, the command to put in your local .vimrc to remove the autocommand is :au! <group> <cmdname>, in this case :au! fedora BufReadPost.

1
  • 2
    I know this is an old answer but this doesn't work for me on RHEL. I put in au! redhat BufReadPost in my ~/.vimrc but it still set the cursor location to the last position. Eventually, I just put a call cursor(1,1) to force the cursor to be at the start of the file.
    – Munir
    Commented Dec 26, 2017 at 17:02
2

This is more of a comment than a real answer. Solaris is not a Linux distro. It is a version of Unix operating system. vi on RedHat which is a Linux distro (Linux != Unix) is symbolically linked to vim. Solaris vi refers to genuine vi editor created by Bill Joy. So we are not talking here about the same editors. I have no idea how vim behaves as I am using nvi on my OpenBSD box which is another clone of genuine vi. I would suggest that you install vi editor on your RedHat box first and try to reproduce the problem. Typing vi filename.txt should open the file and put the prompt at the begging of the first line. Typing vi + filename.txt should open file in the last line. However vi can be open with many other options like vi +n filename.txt which puts you at the beginning of n-th line or vi +/regexp filename.txt which puts you at the beginning of the line which contains first occurrence of the regular expression (regexp), view filename.txt read only mode. Please refer to the man pages for the complete list. Do not forget to unlink vi command from vim when you install the real vi.

1

There might be some other option available for this, but you can use the following work around

vim <filename> +1

+1 indicates, to move the cursor to line 1.

4
  • i got line 2: E682: Invalid search pattern or delimiter when i put just vim +1 in my .exrc -- i don't want to have to specify a file, i.e. i want it applied to ANY file
    – amphibient
    Commented Dec 26, 2012 at 17:52
  • same with vim * +1
    – amphibient
    Commented Dec 26, 2012 at 17:54
  • It is a option to be entered at command-line and not a one to put in vimrc file. As mentioned this is a work around.
    – mtk
    Commented Dec 26, 2012 at 17:56
  • I think it is distro-specific -- it doesn't behave like that on Solaris but does on RHEL
    – amphibient
    Commented Dec 26, 2012 at 17:58
0

To make sure, that this function is added by a plugin try to open the file with

vim --noplugin <filename>

If the cursor doesn't jump to the position of the last edit you should open the file again without the --noplugin-switch and study the output of :scriptnames. :scriptnames lists all previously sourced scripts.

If you are a semi-experienced vim user you can exclude most of the scripts by means of the path. For instance, all scripts located under &rtp/{ftplugin,spell,colors,syntax} (&rtp := runtimepath) are out of question.

I can imagine, that the script which implements this feature has a related name.

0

I had the opposite problem and got here as a result. I was wondering why my cursor would go back to the top of the file. I'm used to switching between buffers and having to find my position each time would not be efficient at all!

None of the answers here speak of the problem, so I thought I would add my own answer, because that would solve your problem. All I have to do is add a C or C++ comment on the first line after a space! I don't know whether this is expected to be a feature or a bug, but well... the cursor comes right back at the top in that case.

+---- see the space here?
|
v
 // first line of file

Removing the comment or the space fixes my problem.

0

I was using the solution posted by otokan for several years until it stopped working recently. I found a new solution posted at the following site:

https://askubuntu.com/questions/905074/how-can-i-disable-vim-jump-to-last-position-feature-in-ubuntu-17-04

There are two solutions posted at the above link:

Solution 1: In the file /usr/share/vim/vim80/defaults.vim, find the autocmd that enables this behavior, and comment it out. This autocmd looks as follows:

" When editing a file, always jump to the last known cursor position.
" Don't do it when the position is invalid, when inside an event handler
" (happens when dropping a file on gvim) and for a commit message (it's
" likely a different one than last time).
autocmd BufReadPost *
  \ if line("'\"") >= 1 && line("'\"") <= line("$") && &ft !~# 'commit'
  \ |   exe "normal! g`\""
  \ | endif

On my system (Crux 3.5 Linux), the autocmd was actually in the file /usr/share/vim/defaults.vim (no "vim80" subdirectory), so you may need to search around under /usr/share/vim for the defaults.vim file that defines this autocmd.

Solution 2: Add the following line to your .vimrc file:

:augroup vimStartup | au! | augroup END

I am currently using solution 2.

1
  • Within Vim, use echo $VIMRUNTIME to see the actual runtime directory. You don't need to search for it.
    – muru
    Commented Aug 3, 2020 at 2:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .