force vim to overwrite external changes - vim

I use Vim 7.4 (Mac OS) to edit and run Lua scripts. I've mapped a key in my .vimrc to save the current buffer and run an external script.
The key map in .vimrc:
map V :w!<CR> :!python "$HOME/tools/client/concli.py" --lua %<CR>
It works fine but every once in a while the files are 'touched' by Xcode (touch shell command). Then when I hit the mapped key vim warns me that the file has been changed externally and I have to confirm to write to it.
This is quite annoying since the files are often touched. How could I force vim to overwrite external changes without prompting? I tried 'w!' without success.
Thank you, Laurent

Indeed, the overwrite confirmation cannot be turned off with :w!, and :set autoread doesn't help in this case, neither. What does work is instructing Vim to explicitly check for changes before the write:
:checktime | w

I believe
set autoread
should do it. It tells Vim to automatically re-reads the file changed outside Vim.

I saw this in a mailing list. Apparently it is called if the file has changed timestamp, after a call to an external shell command.
function! ProcessFileChangedShell()
if v:fcs_reason == 'mode' || v:fcs_reason == 'time'
let v:fcs_choice = ''
else
let v:fcs_choice = 'ask'
endif
endfunction
autocmd FileChangedShell call ProcessFileChangedShell()
But it did not consistently fire for me. (Depending whether or not I had edited the file since the change, which in my case was external.)
There are some more tricks on the VimTips wiki which may help.

Add this to your ~/.vimrc file:
set autoread
nnoremap <C-u> :checktime<CR>
Now whenever you want vim to reload external changes, just click CTRL-U :)

Related

Use Autosave in Vim using no plugin?

How to autosave in Vim using no plugin?
I have found the following post:
Auto-save in VIM as you type
which gives the following answer:
autocmd TextChanged,TextChangedI <buffer> silent write
which works perfectly except that when I put that in my .vimrc it doesn't work, so how can I put that line in my .vimrc such that it works everytime I open Vim ?
EDIT: It seems putting that line in .vimrc works WHEN I DON'T USE A SESSION !
So I will rephrase my question:
How can I get the above work in combination with mksession to open preconfigured session in Vim using vim -S ?
As stated in the comments by D. Ben Knoble, you need to use * instead of <buffer>:
autocmd TextChanged,TextChangedI * silent write

Vim filetype if statement not working

In my ~/.vimrc, I have an if statement that is supposed to determine if the open file is a python file or not:
if (&ft=='python')
map <F9> <esc>:w<enter>:!python3 '%'<enter>
endif
The goal is to bind the python execution command to F9 if the file is a python file, but when I press F9 in a .py file nothing happens. I took out the if statement, and it worked. What is going wrong?
Try something like that instead:
au FileType python map <buffer> <F9> <esc>:w<bar>!python3 '%'<cr>
Your .vimrc config file runs only once on startup. So if you put an if test at this time, it won't work, because no python file is then currently edited.
But you can use .vimrc to setup an automatic behaviour : something that vim will do each time it will encounter a special condition. The condition can be in your case : "A new file is being editing, and its file type is 'python'". See :h :au
In cases like yours, it's useful to add the <buffer> parameter in the :map command : it limits the scope of your mapping to the current buffer only : so when you will press F9 on a non-python buffer, the mapping will not be trigerred.
EDIT:
In my first answer, I also deleted the <esc> in your command, but maybe I'm wrong about this, because it can cause problems in visual mode, so I put it again. You have to test it in visual mode, I didn't do it.

ctags not working as expected with Vim plus general setup problems (C programming)

I have installed cvim and NodeTree plugins and generated an exuberant ctags file for my build tree.
This is what my ~/.vim/.vimrc file looks like:
:noremap :TlistToggle
:let Tlist_Show_One_File = 1
:let Tlist_Exit_OnlyWindow = 1
:let Tlist_Use_Right_Window = 1
set tags=./tags;/
set number
set tabstop=4
set incsearch
When I start editing a file, I notice that Ctrl ] does not work and I have to resort to typing ta: funcname - which gets tiring after a while. Interestingly enough, Ctrl T pops me off the tag stack as expected - I don't understand whats going on - how do I fix this?
Incidentally, vim (appears to) completely ignores the contents of my .vimrc file and I always have to type the same commands in the editor, so as to get the settings I want - very annoying.
Last but not the least, I used to be able to type :make in the editor window, drop to the console and then have the build results displayed in a little window which I can then go to and select a line (with an error or warning say), and then have the editor automagically take me to the offending line - unfortunately, I don't remember the plugin (or commands) I used to allow me to build from within vim.
So, how do I:
Fix my vim setup so that I can move to definitions/declarations using Ctrl-]
Fix my .vimrc file so that contents are actually applied to my vim session.
Find the appropriate plugin to install to allow builds (using make) from within vim
You're asking about a weird mix of problems.
Fix my vim setup so that I can move to definitions/declarations using Ctrl-]
The tags functionality is working; I suspect that you have a mapping blocking Ctrl-]. Try
:verbose nmap <C-]>
and
:nunmap <C-]>
Fix my .vimrc file so that contents are actually applied to my vim session.
:echo $MYVIMRC
will tell you the location of the .vimrc that Vim uses. Also, check the output of :scriptnames which scripts get loaded, and read :help vimrc to understand the logic Vim applies.
Find the appropriate plugin to install to allow builds (using make) from within vim
That's built into Vim. With the appropriate 'makeprg' set (it defaults to make), you can run :make. Vim parses the output (through the 'errorformat' option), and you can open the quickfix list via :copen.
Your vimrc is:
~/.vim/.vimrc
If you run Vim 7.4, it should be:
~/.vim/vimrc
or
~/.vimrc
If you run Vim 7.3 or older, it should be:
~/.vimrc
And... what Ingo said.

Start vim without vimrc / change vimrc with open files

I have 2 .vimrc configuration files ~/.vimrc and ~/.vimsqlrc.
Is there a way I can source either of them (switch from one to another) while I have some files already opened?
As an extension, how do I turn off the loading of vimrc (i.e, don't use any vimrc) while I have files open?
Your ~/.vimrc is read and executed only once. If you want to nullify it with another file, you'll have to change the value of every single option and unmap every single mapping in, of course, both files. This sounds like a very bad and unnecessarily complex idea.
If you want another environment, just use another environment:
$ vim <-- starts Vim normally, reading ~/.vimrc
$ vim -u ~/.vimsqlrc <-- starts Vim using your alternative vimrc
$ vim -u NONE <-- starts Vim without any vimrc
$ vim -u NORC <-- starts Vim without any vimrc, but with plugins
but I'm afraid you'll have to stop and restart Vim for that.
Anyway, your question has a very strong XY problem smell. Do you want to have specific settings for *.sql files?
If that's your goal, you can put your settings in ~/.vim/after/ftplugin/sql.vim like this:
setlocal autoindent
nnoremap <buffer> <F6> :echo "F6"<CR>
Using setlocal for options and <buffer> for mappings ensures that your settings are only applied for *.sql files.

Vim :e starting directory?

I code in Vim, not an IDE.
My source code is often nested 2-3 directories deep.
~/foo$ find
xyz
bar/abc
bar/def
~/foo$ vim
// inside of vim
:e bar/abc
... some work ...
:e <-- is there a way I can have this :e start in ~/foo/bar instead of ~/foo ?
Basically, I want :e to start the directory in "pathname of last edited file"
Thanks!
There's a lot of reasons not to like autochdir as it messes up some plugins and if you end up doing :e ../../../foo.txt you are not gaining anything. Just as an idea try this cmap I knocked up
:cnoremap red edit <c-r>=expand("%:h")<cr>/
then you can type :red and get
:e /the/path/to/your/current/files/dir/
(edit: perhaps use z instead of red as there are commands that start with red)
To expand the topic, also check out the FuzzyFinder plugin and some custom mappings to rapidly jump to common files you are always editing. Eg
10 or so of your regular files should be no more than 2 keystrokes away. It helps if they are systematically named
Here's an idea I use for django.
http://code.djangoproject.com/wiki/UsingVimWithDjango#Mappings
Try the autochdir option. It will automatically change the current working directory to whatever file was most recently opened or selected. In .vimrc:
set autochdir
For more info, :help autochdir
To always change the working directory to the current file's directory I have this in my .vimrc:
if has("autocmd")
autocmd BufEnter * :lcd %:p:h
endif " has("autocmd")
Sorry, but vim's :edit command takes a path which is interpreted relative to the present working directory of the vim instance.
You do have a :cd command which you could use to :cd bar then work for a while, then :cd ...
Hope that help some.
Some time ago I asked questions related to this on the vim mailing list: http://www.mail-archive.com/vim_use#googlegroups.com/msg03266.html Maybe you will find useful tips in that thread.
I tested a lot of plugins, but since CLI based GUIs are not my taste, I simply ended up using standard vim with a few configuration settings.
As honk pointed out, this line sets the working directory to the same as the file your working on:
autocmd BufEnter * lcd %:p:h
My other tip is to use the wildmenu. It makes it easier to get an overview of the files in your current directory when you go :e and then TAB. I'm a python programmer so the last line shows how to hide auto generated files that the python interpreter spits out, but you could use it to hide java .class files or c .obj files or whatever.
set wildmode=list:longest
set wildignore=*.pyc,*pyo
:cd changes directory
:pwd prints the current one.
why not just :E? Explore directory of current file.
:help :E
This isn't exactly what you wanted, but check out NERDTree.
On vim/gVim I just have cd C:/blah/blah at the top of my vimrc. I imagine it works on all platforms.
I personally use vagrant for each project so one CD is enough, but I think you can get vim to use different config files too, -u flag I think.
Or map a key to each project you have so pressing Ctrl+F1 does cd path/to/project/1 and Ctrl+F2 does cd path/to/project/2 perhaps?
Note: I don't use any plugins

Resources