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.
Related
I know how to configure vim to select a particular filetype based upon an extension:
au BufNewFile,BufRead *.foo set filetype=foo
However, a common case for me is to wind up opening a file with an additional suffix. One specific scenario is resolving version control conflicts during a merge. I'll have up files like "foo.cpp.orig" or "foo.java.merge", etc. I'd like to configure vim so that if it opens a file ending in ".orig", etc. for it to strip that suffix and use remaining file extension to select the filetype.
And yes, I do know I could do something like
au BufNewFile,BufRead *.java.* set filetype=java
But that is less than ideal because I have to manually add an entry for all the possible filetypes I might be editing.
" Ignored extensions
if exists("*fnameescape")
au BufNewFile,BufRead ?\+.orig,?\+.bak,?\+.old,?\+.new,?\+.dpkg-dist,?\+.dpkg-old,?\+.dpkg-new,?\+.dpkg-bak,?\+.rpmsave,?\+.rpmnew
\ exe "doau filetypedetect BufRead " . fnameescape(expand("<afile>:r"))
au BufNewFile,BufRead *~
\ let s:name = expand("<afile>") |
\ let s:short = substitute(s:name, '\~$', '', '') |
\ if s:name != s:short && s:short != "" |
\ exe "doau filetypedetect BufRead " . fnameescape(s:short) |
\ endif |
\ unlet! s:name s:short
au BufNewFile,BufRead ?\+.in
\ if expand("<afile>:t") != "configure.in" |
\ exe "doau filetypedetect BufRead " . fnameescape(expand("<afile>:r")) |
\ endif
elseif &verbose > 0
echomsg "Warning: some filetypes will not be recognized because this version of Vim does not have fnameescape()"
endif
actually, Vim already considered it. the above code is from filetype.vim
After looking at the code Dyno pointed me to, I think what I want is to add
au BufNewFile,BufRead ?\+.merge
\ exe "doau filetypedetect BufRead " . fnameescape(expand("<afile>:r"))
to my .vimrc. It isn't quite as nice as having some variable to set to extend the list of known patterns, but isn't as much of a hack as modifying the original filetype.vim.
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
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`\""
Is there a .vimrc command for opening blank files in insert mode? Non blank files would still open in command mode.
You can try an autocommand:
au BufNewFile * startinsert
So, with:
vim oldfile
will enter in normal mode, but with:
vim newfile
will enter in insert mode.
Note that
vim
without files will be in normal mode too. Perhaps you would need an additional autocommand for it. (EDIT: See commments for a Ben's solution to this one)
To enter insert mode only on empty new buffers:
autocmd BufNewFile * if wordcount()['chars'] == 0 | startinsert | endif
To do that only when opening vim:
autocmd VimEnter * if wordcount()['chars'] == 0 | startinsert | endif
Is there a way to disable warnings in VIM?
In particular, I want to disable Warning 12 when a file turns from read-only to writable. I have a script which opens the file for edit in perforce, but vim thinks the file has changed and issues a warning.
Thanks
I have the following in my .vimrc; you should only need the second one. It echoes the message to the status bar instead of popping up a dialog.
autocmd FileChangedRO * echohl WarningMsg | echo "File changed RO." | echohl None
autocmd FileChangedShell * echohl WarningMsg | echo "File changed shell." | echohl None
Try :help FileChangedShell for more information.
add the following line to your .vimrc file :
set autoread
This will disable "read-only to writeable" warnings
I've been using FileChangedRO for a while now to automatically checkout files when starting to edit them and found the W12 warning annoying as well. The problem is that p4 edit updates the file attributes to remove the read only flag. If as part of the initial edit you also change the file, Vim sees this as a conflict since it's no longer read only. Here's the solution I use which is a bit more conservative about using FileChangedShell in case the file was changed externally for some other reason.
let s:IgnoreChange=0
autocmd! FileChangedRO * nested
\ let s:IgnoreChange=1 |
\ call system("p4 edit " . expand("%")) |
\ set noreadonly
autocmd! FileChangedShell *
\ if 1 == s:IgnoreChange |
\ let v:fcs_choice="" |
\ let s:IgnoreChange=0 |
\ else |
\ let v:fcs_choice="ask" |
\ endif