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
Related
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.
I have these in my vimrc:
if has("autocmd")
augroup Misc
" Jump to the last known position when reopening a file
autocmd BufReadPost *
\ if line("'\"") > 1 && line("'\"") <= line("$") |
\ exe "normal! g`\"" |
\ endif
augroup END
augroup FTOptions
autocmd!
autocmd FileType cpp,java setlocal commentstring=//\ %s
autocmd FileType markdown setlocal textwidth=78 formatprg=par\ -w78
augroup END
In markdown files I'd like to do the formatting using par. It works well, but I get a warning if "par" is not installed on the system. I'm wondering if there's a way to check and only set "formatprg=par\ -78" when par is installed, otherwise use VIM's default formatting.
I would be grateful for any suggestion.
if executable('par')
" stuff
endif
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
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`\""
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