DavidC.
DavidC.

Reputation: 677

The cursor position in a file is not persistent form a previous editing session. Only when the file is located in a long path

For a file located in a long path, I have noticed that
the cursor position (from a previous editing/opening session), is not persistent.

Having taken the actions reported here and here, the problem still appears.

Running this script.sh, is a minimal working example that would allow you to reproduce the same problem:

#!/bin/bash

# We create a `test.txt` file:
echo "Hello,

this is line

this is line 5

this is line 7
" > test_file.txt

# We declare a long path directory:
long_path_dir=testing_long_path/mksj/jfhgbus/jfhgdbhsio/kjihu_ijku_okjih/kjhui__okjikol__kjhikjn__/kjnjkmloipokl/polkjioplkmn/lkjiopk/llkkmjilpoi/kkklmn__kojuo/klkjoip_plko/ppplkojiun/klklkjmkiolp___okjn/mnhgfvghbjuio/lkmnjko/MKJNKIL_l/kjhgbhnjmki__jdjdm/kFGESCE_igjhdjd/JDJDKCNS_jdjd/jfjfj_KAJK/lkdjwnmckdjcnm/lsksjiowp___HSNKAL/mCNSJSIEO/ksjcnmsjs/KJSIL/jfhns_OSKL/ksmcmkfjnv_lksjCKSL/kfjdhnbjk_KJSH/KSJAILMK_opoijt/kdjkflmsnkjdu_LALALKJSN/NJNJKS_kskslkmcnbsm/ksjskjnjc/kioikrjsnm/kioikjui/jshncmsjkl/klkopo/NCBCNMA/mkmjnks/ksksmk/klkjh/kioskmnxl/lolOIJS/LOKSJNK/jsojs/mkmcnjkjsu/lolwos/kmkmJKJS/LOLOSPLS/lolskija/lskjmcml/lolksjfh/kjdjskn/LOLXnjijs/laSHHS/lsksmcl/lslsopolsn/lmknf/KSJAO/lolfngi/LOLSNL/lolsnzzjs/mkmdnaj/ldldpokng/lsksiks/lomxkksjm/KSLSKML/kdjdnxjfjfnk/mkmsj

# We create this directory (first remove it, in case it exists):
rm -Rf $long_path_dir
mkdir -p $long_path_dir    

# We move the test_file.txt to that directory:
mv test_file.txt  $long_path_dir

Once this script has runned, we open the test_file.txt:

vim testing_long_path/.../.../test_file.txt

and navigate to the 5th line:

enter image description here

When we quit the file :q, and open it again, the cursor is no longer located in the 5th line, but in the 1st line:

enter image description here

This also happens with emacs.

My /etc/vim/vimrc has uncommented these lines:

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

Update 1: Using @Sergio 1st suggestion: I did not have a ~/.vimrc file. I have created one in which I have written the following:

" Tell vim to remember certain things when we exit
"  '10  :  marks will be remembered for up to 10 previously edited files
"  "100 :  will save up to 100 lines for each register
"  :20  :  up to 20 lines of command-line history will be remembered
"  %    :  saves and restores the buffer list
"  n... :  where to save the viminfo files
set viminfo='10,\"100,:20,%,n~/.viminfo

function! ResCur()
  if line("'\"") <= line("$")
    normal! g`"
    return 1
  endif
endfunction

augroup resCur
  autocmd!
  autocmd BufWinEnter * call ResCur()
augroup END

When closing and opening again with vim testing_long_path/.../.../test_file.txt, the position of the cursor is not remembered.

Update 2: Using @Sergio 2nd suggestion: The output of :scriptnames is the following, in which the /etc/vim/vimrc is not listed.

enter image description here

Update 3: When opening the file via vim testing_long_path/.../.../test_file.txt, and asking for :autocmd BufWinEnter I receive:

enter image description here

Upvotes: 0

Views: 154

Answers (1)

Stefan Kamphausen
Stefan Kamphausen

Reputation: 1665

In Emacs, the mechanism to store file positions is called saveplace. It can filter the files for which positions should be stored. See the documentation of the variable save-place-forget-unreadable-files

Non-nil means forget place in unreadable files.

The filenames in save-place-alist that do not match save-place-skip-check-regexp are filtered through file-readable-p. If nil, their alist entries are removed.

Now, let's see that file-readable-p says about your file:

(defvar the-long-file)
(setq the-long-file "testing_long_path/mksj/[omitted for brevity]/mkmsj/test_file.txt")

(file-readable-p the-long-file) ;; => nil
(file-readable-p "/etc/passwd") ;; => t

I can't explain why file-readable-p returns nil but at least this explains the behavior you observed. If you want to study that function, take a look at https://github.com/typester/emacs/blob/master/src/fileio.c#L2554

Since this happens in Emacs and vim, as you described, maybe it is a limitation further down in LibC or some OS limits.

Upvotes: 1

Related Questions