Vimrc not saving cursor position though enabled - vim

My vim does not save the cursor position (at least not reliable) though I am using:
autocmd BufReadPost *
\ if line("'\"") > 0 && line("'\"") <= line("$") |
\ exe "normal g'\"" |
\ endif
In my vimrc.
My complete vimrc is here.
I already reenabled nobackup and noswapfile but the problem remains.
What could the reason be for this?

You need to use the motion g`" to jump to the last known line and column. You are using g'", which jumps to the first character on that line.
The executed command in your code should be:
exe "normal g`\""

Related

How do I get VIM to always open the last opened file?

I want vim to always open the last opened file instead of an empty un-named file with VIM - Vi IMproved... message.
How can I do that?
Add this autocmd into your .vimrc which will be triggered when executing vim from shell without arguments:
autocmd VimEnter * if
\ argc() == 0 &&
\ bufname("%") == "" |
\ exe "normal! `0" |
\ endif
Execute '0 at the startup of vim.

Re-open vim to same line after compiling tex

I have in my .vimrc:
map = :w !pdflatex %
to quickly write and compile tex without leaving vim. However, when it has compiled and I get back to vim, I am at the top of the document. Is there a way to have this mapping return me to the line at which I left off? Thanks!
You can uncomment this in your vimrc to keep your previous position.
" Uncomment the following to have Vim jump to the last position when
" reopening a file
if has("autocmd")
au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
endif

How to get vim to sync syntax highlighting when opening file

I've got vim setup to jump to the last known cursor position when opening a file, but sometimes that position is in the middle of some javascript or css on a html page, and the syntax highlighting doesn't catch that. I can manually run
:syntax sync minlines=200
to fix the highlighting, but I'd like vim to run that for me whenever I open a file.
I tried to do this in my vimrc, but it doesn't fix the syntax highlighting when I open a file.
" Jump to last known cursor porition
autocmd BufReadPost *
\ if line("'\"") > 1 && line("'\"") <= line("$") |
\ exe "normal! g`\"" |
\ endif
" Sync syntax highlighting
autocmd BufReadPost * syntax sync minlines=200
I close and reopen vim, so it should be getting the new .vimrc settings.
Not sure what I'm missing here. Thanks.
Ok, I got it using the BufWinEnter event instead of BufReadPost
" Jump to last known cursor porition
autocmd BufReadPost *
\ if line("'\"") > 1 && line("'\"") <= line("$") |
\ exe "normal! g`\"" |
\ endif
" Sync syntax highlighting
autocmd BufWinEnter * syntax sync minlines=200

How to modify the last-position-jump vimscript to not do it for git commit messages

Here's the script for convenience:
" Uncomment the following to have Vim jump to the last position when
" reopening a file
if has("autocmd")
au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$")
\| exe "normal! g'\"" | endif
endif
The feature is excellent but not when used with certain cases where Vim is invoked as editor: For instance often I write a two line git commit message so the next time I commit it's gonna drop me on the second line and I have to adjust for this.
Similarly many other systems allow you to specify Vim to be used as an editor as part of some process that involves editing a file.
How can we detect this and modify our last-position-jump script? Git's actually kind of special because it uses the .git/COMMIT_EDITMSG file which stays the same across commits which is why this is happening. With a temp file it wouldn't occur on files that Vim hasn't seen before.
This probably makes the task nearly trivial (augment the script with a check if current file == COMMIT_EDITMSG...)
But, a really perfect answer is if we can detect whether Vim is invoked from the command line or if it was opened by a script. Is there a way to tell? I know unix programs can determine if they are running in a term/pseudoterm or not, though either way they do end up receiving input from the terminal.
If you don't want to modify the global rule (like in FDinoff's answer) with all sorts of exceptions, you can also undo the jump by putting the following into ~/.vim/after/ftplugin/gitcommit.vim:
:autocmd BufWinEnter <buffer> normal! gg0
Though I haven't experienced any problems with the above command, even on buffer switches, a more robust version makes the autocmd fire once:
augroup DisableLastPositionJump
autocmd! BufWinEnter <buffer> execute 'normal! gg0' | autocmd! DisableLastPositionJump BufWinEnter <buffer>
augroup END
Git commit messages have the filetype gitcommit.
Just add a check to see if the file is not of the gitcommit filetype for deciding when to jump to the last position.
if has("autocmd")
au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$") && &filetype != "gitcommit"
\| exe "normal! g'\"" | endif
endif
If you don't want VIM to remember where you were from the previous commit, set the following in ~/.vimrc:
autocmd FileType gitcommit call setpos('.', [0, 1, 1, 0])
Source: http://vim.wikia.com/wiki/Always_start_on_first_line_of_git_commit_message

vim restores cursor position; exclude special files

The following code is inside my .vimrc and generally restores the last cursor position of a file I opened with vim:
autocmd BufReadPost *
\ if line("'\"") > 1 && line("'\"") <= line("$") |
\ exe "normal! g`\"" |
\ endif
I really like this feature and want to leave it turned on, except for one file: When commiting with git, vim gets fired up and I can edit the commit message with it. However the commit message file exists before vim starts (and is prefilled), so vim sees it as an existing file and restores the last cursor position (which is usually not where I would like to start to type).
So is there a possibility to modify the above script to exclude the COMMIT_EDITMSG file?
After reading through the manual on auto commands, I noticed that it seems not possible to define the pattern they match to in such a way that they exclude a special pattern. And I also wasn't able to use some variable that contains the current filename, so that I simply expand the existing if to exclude the file.
However, based on the comment by Pavel Shved (about the gg moving to the top of the file) I thought that in the same way it should be possible to simply overwrite the effect of the position restoring, by simply moving it to the top later again. So I came up with this:
autocmd BufReadPost COMMIT_EDITMSG
\ exe "normal! gg"
Placing this after the previous autocmd BufReadPost simply chains the event execution, so vim, after executing the first and restoring the position, reads this and matches it on the excluded filename and uses the gg to move the cursor to the top, basically overwriting the effect of the original autocmd.
And that works just fine :)
I understand you have already come up with a solution but I had the same question and came up with an alternative that doesn't require chaining.
function! PositionCursorFromViminfo()
if !(bufname("%") =~ '\(COMMIT_EDITMSG\)') && line("'\"") > 1 && line("'\"") <= line("$")
exe "normal! g`\""
endif
endfunction
:au BufReadPost * call PositionCursorFromViminfo()
You should probably investigate mksession. You can set up VimEnter/VimLeave auto commands to "do the right thing" when you have specified files on the command line (as when git invokes vim). There are numerous scripts for this floating around, see http://vim.wikia.com/wiki/Working_with_multiple_sessions for example.
Try this, its move cursor to last position
set hidden
Put the following lines instead of your lines in the .vimrc:
au BufWinLeave * mkview
au BufWinEnter * silent loadview
Adding the following to .vimrc worked for me:
au FileType gitcommit au! BufEnter COMMIT_EDITMSG call setpos('.', [0, 1, 1, 0])
(from https://vim.fandom.com/wiki/Always_start_on_first_line_of_git_commit_message)

Resources