LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Arch (https://www.linuxquestions.org/questions/arch-29/)
-   -   Vim 8.1: cursor position not saved. (https://www.linuxquestions.org/questions/arch-29/vim-8-1-cursor-position-not-saved-4175667704/)

stf92 01-14-2020 12:16 PM

Vim 8.1: cursor position not saved.
 
Hi: I am within vim editing a file and the cursor is in a given position. Now I exit (:wq command). The next time I edit that file, that is when I invoke vim with the same file name, the cursor appears in line 1 column 1. This happens every time I run vim on a file already edited with vim. How can this be? I thought vim always saved the cursor position and, in fact, this was the case when I ran vim under Slackware.

Mechanikx 01-14-2020 08:27 PM

The cursor position is saved in ~/.viminfo. Maybe it's not able to write to this file for some reason? Try checking the file permissions of ~/.viminfo. Maybe the new distro you're running vim under doesn't have the write permission set.

Geist 01-14-2020 10:08 PM

It's an autocommand...defined in the /defaults.vim script on the system wide vim directory (for me it's /usr/share/vim/vim81/defaults.vim )

If yours lacks this, this is the command you could just put in your vimrc.

Code:

autocmd BufReadPost *
  \ if line("'"") >= 1 && line("'"") <= line("$") |
  \  exe "normal! g`"" |
  \ endif

If this doesn't work, it might be related to "+eval", as per the defaults.vim file:
Code:

" Only do this part when Vim was compiled with the +eval feature.
Or something entirely different.

But try it for yourself, if you want.

stf92 01-15-2020 12:37 PM

My /usr/share/vim/vim81/defaults.vim has this block in it:
Code:

" Only do this part when compiled with support for autocommands.
if has("autocmd")

  " Enable file type detection.
  " Use the default filetype settings, so that mail gets 'tw' set to 72,
  " 'cindent' is on in C files, etc.
  " Also load indent files, to automatically do language-dependent indenting.
  " Revert with ":filetype off".
  filetype plugin indent on

  " Put these in an autocmd group, so that you can revert them with:
  " ":augroup vimStartup | au! | augroup END"
  augroup vimStartup
    au!

    " 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

  augroup END

endif " has("autocmd")

Perhaps vim was compiled without support for autocommands?

pan64 01-15-2020 02:15 PM

Code:

vim --version
will tell you the configuration, you need to look for +autocmd (or -autocmd).

ehartman 01-15-2020 02:32 PM

Quote:

Originally Posted by pan64 (Post 6078731)
you need to look for +autocmd (or -autocmd).

Another setting (within vim itself) is the one for 'viminfo', if that outputfile is not enabled
Code:

(viminfo=)
as in my installation, of course no info is saved for a next session.

stf92 01-16-2020 06:47 AM

Quote:

Originally Posted by pan64 (Post 6078731)
Code:

vim --version
will tell you the configuration, you need to look for +autocmd (or -autocmd).

It says +autocmd.

pan64 01-16-2020 07:54 AM

that means it is enabled. So there should be an error somewhere


All times are GMT -5. The time now is 06:36 PM.