Re: sourcing
On Tue 5-Dec-06 1:19pm -0600, C.G.Senthilkumar. wrote: > On Tue, 5 Dec 2006, Alan G Isaac wrote: >> On Mon, 4 Dec 2006, Bill McCarthy apparently wrote: >>> Instead of using an autocmd, you could place those maps in a >>> file called tex.vim in your local ftplugin directory. Place >>> this single line in such a file: >>> map :echo 'It worked!' > I did this. It worked. I didn't have a problem. One thing I meant to mention in a recent post is that you should use your local after/ftplugin/ instead of your local ftplugin/. In is possible for a map from $VIMRUNTIME to overwrite your maps in local ftplugin/. -- Best regards, Bill
Re: sourcing
On Tue 5-Dec-06 12:56pm -0600, Alan G Isaac wrote: > On Mon, 4 Dec 2006, Bill McCarthy apparently wrote: >> Instead of using an autocmd, you could place those maps in a >> file called tex.vim in your local ftplugin directory. Place >> this single line in such a file: >> map :echo 'It worked!' > > OK, I did this. > But there is still a problem. > > Suppose I have defined:: > > imap ;fn > %\footnote{%}%-i > > in my tex_ai.vim file in my local ftplugin directory. > I create the buffer ``:e c:\temp.tex`` > then I leave the buffer by deleting it ``:bd`` > and then later I decide to reopen it ``:e c:\temp.tex``. > Now I get a bunch of E227 errors, saying the mapping already > exists. This must mean that there Vim tries to redefine the > mapping. Interesting. I changed my simply test to map:echo 'It worked!' and had no problem. My first thought was that you may be sourcing twice (such as an autocmd performing 'setl ft=tex', but then I read on. > BUT I had tried to rule this out: at the top of my plugin > I have:: > > if exists("b:loaded_tex_ai") > finish > endif > let b:loaded_tex_ai=1 > > But of course ``b:loaded_tex_ai`` is gone once I use ``:bd``. > But the mappings are apparently still around! > I do not really understand the variable convetions. > Should I have created ``s:loaded_tex_ai`` instead? > Might that work? I don't see that behavior. The :bd appears to clear both buffer local mappings and variables - as it appears to say in the docs. -- Best regards, Bill
Re[2]: sourcing
On Tue, 5 Dec 2006, Alan G Isaac wrote: On Mon, 4 Dec 2006, Bill McCarthy apparently wrote: Instead of using an autocmd, you could place those maps in a file called tex.vim in your local ftplugin directory. Place this single line in such a file: map :echo 'It worked!' I did this. It worked. I didn't have a problem. Suppose I have defined:: imap ;fn %\footnote{%}%-i However my maps were map! in my tex_ai.vim file in my local ftplugin directory. I create the buffer ``:e c:\temp.tex`` then I leave the buffer by deleting it ``:bd`` and then later I decide to reopen it ``:e c:\temp.tex``. I tried this out, I didn't reproduce your errors. BUT I had tried to rule this out: at the top of my plugin I have:: if exists("b:loaded_tex_ai") finish endif let b:loaded_tex_ai=1 I didn't do this however. Also, I use slackware. So, is there any OS dependent settings in vim that could be causing this problem for you? I know I'm not helping you. But just wanted to share my results. Thanks, Senthil.
Re: sourcing
Alan G Isaac wrote: On Mon, 4 Dec 2006, Bill McCarthy apparently wrote: Instead of using an autocmd, you could place those maps in a file called tex.vim in your local ftplugin directory. Place this single line in such a file: map :echo 'It worked!' OK, I did this. But there is still a problem. Suppose I have defined:: imap ;fn %\footnote{%}%-i in my tex_ai.vim file in my local ftplugin directory. I create the buffer ``:e c:\temp.tex`` then I leave the buffer by deleting it ``:bd`` and then later I decide to reopen it ``:e c:\temp.tex``. Now I get a bunch of E227 errors, saying the mapping already exists. This must mean that there Vim tries to redefine the mapping. BUT I had tried to rule this out: at the top of my plugin I have:: if exists("b:loaded_tex_ai") finish endif let b:loaded_tex_ai=1 But of course ``b:loaded_tex_ai`` is gone once I use ``:bd``. It is not gone, it is just marked as "unlisted". ":ls!" (with exclamation mark) will show you that the so-called "deleted" buffer is still around. But the mappings are apparently still around! I do not really understand the variable convetions. Should I have created ``s:loaded_tex_ai`` instead? Might that work? Global plugins normally use a global variable. This has the following advantages: - If you have more than one version of a single plugin, only one version will run. - You can disable the plugin by setting the variable. Filetype-plugins should run once per applicable buffer, at the filetype event, or at some event dependent on the Filetype event (like Syntax). They should not set anything that would "carry over" to different buffers. In particular, mappings should have the modifier and options should be set using ":setlocal". These should use a buffer-variable, like you did. The problem does not arise if I use ``:bw`` instead of ``:bd``. But then I lose other information too. Only ":bwipeout" really deletes everything Vim knows about the buffer. What is the right way to manage this? Hints or suggestions? Thank you, Alan Isaac What you can do is to merely "unload" the buffer (write it to disk and edit something else in its stead). Then if and when you later decide to edit the first buffer again, there shouldn't be any errors. Best regards, Tony.
Re[2]: sourcing
On Mon, 4 Dec 2006, Bill McCarthy apparently wrote: > Instead of using an autocmd, you could place those maps in a > file called tex.vim in your local ftplugin directory. Place > this single line in such a file: > map :echo 'It worked!' OK, I did this. But there is still a problem. Suppose I have defined:: imap ;fn %\footnote{%}%-i in my tex_ai.vim file in my local ftplugin directory. I create the buffer ``:e c:\temp.tex`` then I leave the buffer by deleting it ``:bd`` and then later I decide to reopen it ``:e c:\temp.tex``. Now I get a bunch of E227 errors, saying the mapping already exists. This must mean that there Vim tries to redefine the mapping. BUT I had tried to rule this out: at the top of my plugin I have:: if exists("b:loaded_tex_ai") finish endif let b:loaded_tex_ai=1 But of course ``b:loaded_tex_ai`` is gone once I use ``:bd``. But the mappings are apparently still around! I do not really understand the variable convetions. Should I have created ``s:loaded_tex_ai`` instead? Might that work? The problem does not arise if I use ``:bw`` instead of ``:bd``. But then I lose other information too. What is the right way to manage this? Hints or suggestions? Thank you, Alan Isaac
Re: (un)sourcing
C.G.Senthilkumar. wrote: Hi, I use the following autocmd in my ~/.vimrc to load a source file with my personal vim mappings while editing a tex file: autocmd FileType tex source ~/Dot.vilatexrc This works fine. However, the mappings continue to exist even after I open a different filetype without restarting vim. Is there an "unsource" command? If yes, how do I tell vim to do that each time I open another filetype. I have to add that, the appropriate syntax plugins and indent files are loaded for the new filetype. So, I'm happy with that. Any pointers will help. Thanks in advance. Senthil. there is no "unsource" command, but you can build your script so as to avoid unwanted side-effects: - use :map not plain :map similarly for map!, imap, cnoremap, etc. - use :setlocal not plain :set - if you need to set a global option for the duration of your script, save it on entry and restore it on exit, e.g. :let save_cpo = &cpo :set cpo&vim ... :let &cpo = save_cpo :unlet save_cpo etc. Note that a script which is only to be sourced at the FileType autocommand for tex files can be called (on Windows) ~/vimfiles/after/ftplugin/tex.vim (on Unix/Linux) ~/.vim/after/ftplugin/tex.vim It will then run immediately after $VIMRUNTIME/ftplugin/tex.vim without the need for you to set up an autocommand. The caveats about ":map " and ":setlocal" still apply. If your script has a different name, and you don't want to rename it, you can simply source it from a script with the above name; or if your system supports soft links, you can link it from ~/.vim/after/ftplugin/tex/ i.e., you can set it up with (e.g.) the following shell commands (which are shown for Unix): mkdir -p ~/.vim/after/ftplugin/tex cd ~/.vim/after/ftplugin/tex ln -sv ~/Dot.vilatexrc vilatex.vim At the FileType autocommand event for tex files, Vim will lookup (among others) scripts named ~/.vim/after/ftplugin/tex/*.vim : it will find ~/.vim/after/ftplugin/tex/vilatex.vim, which is a soft link pointing to ~/Dot.vilatexrc, and source the latter. see ":help ftplugin-name" Best regards, Tony.
Re: sourcing
On Mon 4-Dec-06 6:54pm -0600, you wrote: > On Mon, 4 Dec 2006, Bill McCarthy wrote: >> On Mon 4-Dec-06 5:56pm -0600, C.G.Senthilkumar. wrote: >>> I use the following autocmd in my ~/.vimrc to load a source file with my >>> personal vim mappings while editing a tex file: >>> >>> autocmd FileType tex source ~/Dot.vilatexrc >>> >>> This works fine. However, the mappings continue to exist even after I >>> open a different filetype without restarting vim. Is there an "unsource" >>> command? If yes, how do I tell vim to do that each time I open another >>> filetype. >> You could make those mappings local. When you delete a >> buffer with local mappings, those mappings are cleared. >> >> See :help map-local > :help map-local > *:map-local* > *:map-* *E224* *E225* > If the first argument to one of these commands is "" it will apply to > mappings locally to the current buffer only. Example: > > :map ,w /[.,;] > ... > > > What does the mean? > Is it a literal string or the name of the file I'm editing or something > else? It is a type of map it this context. The map will only apply to the current buffer. When you read a file into a buffer, for example, the file type is set which triggers the FileType event. There are examples given of such a map. I'll give another below. Instead of using an autocmd, you could place those maps in a file called tex.vim in your local ftplugin directory. Place this single line in such a file: map :echo 'It worked!' Now edit a tex file and try . Moving to another window not containing a tex file and has its normal behavior. -- Best regards, Bill
Re: sourcing
On Mon 4-Dec-06 5:56pm -0600, C.G.Senthilkumar. wrote: > I use the following autocmd in my ~/.vimrc to load a source file with my > personal vim mappings while editing a tex file: > > autocmd FileType tex source ~/Dot.vilatexrc > > This works fine. However, the mappings continue to exist even after I > open a different filetype without restarting vim. Is there an "unsource" > command? If yes, how do I tell vim to do that each time I open another > filetype. You could make those mappings local. When you delete a buffer with local mappings, those mappings are cleared. See :help map-local -- Best regards, Bill
(un)sourcing
Hi, I use the following autocmd in my ~/.vimrc to load a source file with my personal vim mappings while editing a tex file: autocmd FileType tex source ~/Dot.vilatexrc This works fine. However, the mappings continue to exist even after I open a different filetype without restarting vim. Is there an "unsource" command? If yes, how do I tell vim to do that each time I open another filetype. I have to add that, the appropriate syntax plugins and indent files are loaded for the new filetype. So, I'm happy with that. Any pointers will help. Thanks in advance. Senthil. -- Today's fortune: Don't kiss an elephant on the lips today.
Re: sourcing vimrc files
On 5/14/2006 8:41 PM, Gerald Lai wrote: > Encase your function like this: > > if !exists("*Source_vimrc") > function Source_vimrc() > ... > endfunction > endif Thanks, Gerald. This worked perfectly. Also, thanks to everyone else for their suggestions. I appreciate the feedback and read up on the suggested commands, but this ended up being the easiest. :-) -- Jared
Re: sourcing vimrc files
On Sun, May 14, 2006 at 06:41:39PM -0700, Gerald Lai wrote: > > Encase your function like this: > > if !exists("*Source_vimrc") > function Source_vimrc() > ... > endfunction > endif > > The reason you're getting the error is because you're sourcing the file > (vimrc) that produced/will override the function (Source_vimrc) you're > using at the moment. > > A few words of advice to source your vimrc cleanly: > > 1. Define your functions in vimrc with "function!" so that when > sourced again, the functions will be overwritten. > > 2. Keep your autocmds in an augroup so that if it's sourced again, you > can delete the augroup and redefine it instead of adding more of > the same autocmds. > > 3. If you're using the FuncUndefined autocmd, delete those functions > that have been defined before. Those are all good suggestions (although testing exists("*Source_vimrc") seems a little complicated). One more possibility is to put all function definitions, along with anything else that never needs to be re-done, below a few lines like these: " Skip the rest of the file if it has already been re-loaded. if exists("s:deja_vu") finish endif let s:deja_vu = 1 function Source_vimrc() " no need for the !, this should never be read twice. ... BTW, another post on this thread pointed out that your vimrc file knows its own name. As of vim 7.0, this name is also stored in the $MYVIMRC environment variable. HTH --Benji Fisher
Re: sourcing vimrc files
Hello, Jared <[EMAIL PROTECTED]> wrote: > I'd like to map a hotkey to re-source my vimrc files (system + user). I > originally tried this simple approach: > > nmap :source $VIM\vimrc > \ :source $VIM\_vimrc > \ :source $HOME\_vimrc Your .vimrc knows its own filename. Here is an exerpt from my .vimrc (vimrc_core.vim actually). " --- >% -- " VIM - Editing and updating the vimrc: {{{ " As I often make changes to this file I use these commands " to start editing it and also update it: let vimrc=expand(':p') :VimrcHelp ' ,vu = "update" by reading this file [N] nnoremap ,vu :source =vimrc :VimrcHelp " ,ve = vimrc editing (edit this file) [N] nnoremap ,ve :call OpenVimrc() function! s:OpenVimrc() if (0==strlen(bufname('%'))) && (1==line('$')) && (0==strlen(getline('$'))) " edit in place exe "e ".g:vimrc else exe "sp ".g:vimrc endif endfunction " }}} " --- >% -- HTH, -- Luc Hermitte http://hermitte.free.fr/vim/
Re: sourcing vimrc files
On Sun, May 14, 2006 at 07:49:11PM -0500, Jared wrote: > I'd like to map a hotkey to re-source my vimrc files (system + user). I > originally tried this simple approach: > > nmap :source $VIM\vimrc > \ :source $VIM\_vimrc > \ :source $HOME\_vimrc > > But that failed because source aborts when the file doesn't exist. You could probably use :runtime instead of :source. :help :runtime [...] -- with kind regards Thor Andreassen
Re: sourcing vimrc files
On Sun, 14 May 2006, Jared wrote: [snip] Longer and more complicated, but now I can check to see if the file exists before sourcing it. However, this also causes a problem: when I try to source the file containing this function, it gives me an error saying that it cannot replace the function because it is currently in use. If I remove the !, it still gives me an error because it already exists. Any ideas how to work around this? Or if you have a different/better way of doing this, I'm certainly open to suggestions. :-) This is on Windows XP. Thanks. Encase your function like this: if !exists("*Source_vimrc") function Source_vimrc() ... endfunction endif The reason you're getting the error is because you're sourcing the file (vimrc) that produced/will override the function (Source_vimrc) you're using at the moment. A few words of advice to source your vimrc cleanly: 1. Define your functions in vimrc with "function!" so that when sourced again, the functions will be overwritten. 2. Keep your autocmds in an augroup so that if it's sourced again, you can delete the augroup and redefine it instead of adding more of the same autocmds. 3. If you're using the FuncUndefined autocmd, delete those functions that have been defined before. For examples 2 & 3, augroup vimrc "delete augroup autocmd! ... silent! delfunction MyFuncInOtherFile autocmd FuncUndefined MyFuncInOtherFile source $HOME/.vim/autoload/MyFuncInOtherFile.vim ... autocmd ... ... augroup END HTH :) -- Gerald
Re: sourcing vimrc files
On 5/14/06, Jared <[EMAIL PROTECTED]> wrote: I'd like to map a hotkey to re-source my vimrc files (system + user). I originally tried this simple approach: nmap :source $VIM\vimrc \ :source $VIM\_vimrc \ :source $HOME\_vimrc Try :silent! source But that failed because source aborts when the file doesn't exist. I then modified it to a function call, like so: function! Source_vimrc() if filereadable($VIM.'\vimrc') source $VIM\vimrc endif if filereadable($VIM.'\_vimrc') echo "test1" source $VIM\_vimrc echo "test2" endif if filereadable($HOME.'\_vimrc') source $HOME\_vimrc endif endfunction nmap :call Source_vimrc() Longer and more complicated, but now I can check to see if the file exists before sourcing it. However, this also causes a problem: when I try to source the file containing this function, it gives me an error saying that it cannot replace the function because it is currently in use. If I remove the !, it still gives me an error because it already exists. You're asking for an inf. recursion having it source itself. Put it in a file in your plugin directory. Any ideas how to work around this? Or if you have a different/better way of doing this, I'm certainly open to suggestions. :-) This is on Windows XP. Thanks. -- Jared
sourcing vimrc files
I'd like to map a hotkey to re-source my vimrc files (system + user). I originally tried this simple approach: nmap :source $VIM\vimrc \ :source $VIM\_vimrc \ :source $HOME\_vimrc But that failed because source aborts when the file doesn't exist. I then modified it to a function call, like so: function! Source_vimrc() if filereadable($VIM.'\vimrc') source $VIM\vimrc endif if filereadable($VIM.'\_vimrc') echo "test1" source $VIM\_vimrc echo "test2" endif if filereadable($HOME.'\_vimrc') source $HOME\_vimrc endif endfunction nmap :call Source_vimrc() Longer and more complicated, but now I can check to see if the file exists before sourcing it. However, this also causes a problem: when I try to source the file containing this function, it gives me an error saying that it cannot replace the function because it is currently in use. If I remove the !, it still gives me an error because it already exists. Any ideas how to work around this? Or if you have a different/better way of doing this, I'm certainly open to suggestions. :-) This is on Windows XP. Thanks. -- Jared