8

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.

7

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
    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 Dec 26 '17 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 a 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.

  • 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 Dec 26 '12 at 17:52
  • same with vim * +1 – amphibient Dec 26 '12 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 Dec 26 '12 at 17:56
  • I think it is distro-specific -- it doesn't behave like that on Solaris but does on RHEL – amphibient Dec 26 '12 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.