Monday, September 17, 2012

Re: autochdir vs command-t

Hi Tony!

On Mo, 17 Sep 2012, Tony Mechelynck wrote:

> On 17/09/12 09:57, Christian Brabandt wrote:
> >On Mon, September 17, 2012 08:40, shawn wilson wrote:
> >>On Mon, Sep 17, 2012 at 1:46 AM, Tony Mechelynck
> [...]
> >>> let <SID>curdir = getcwd()
> >>> map <F5> :if getcwd() == <SID>curdir <Bar> lcd %:h <Bar> else
> >>><Bar>
> >>>exe 'lcd' <SID>curdir <Bar> endif<CR>
> >>>
> >>
> >>thanks for that. though, for some reason, it is erroring:
> >>
> >>Error detected while processing /home/swilson/.vimrc:
> >>line 74:
> >>E475: Invalid argument: <SID>curdir = getcwd()
> >>Press ENTER or type command to continue
> >>
> >>while i found this
> >>(http://vimdoc.sourceforge.net/htmldoc/map.html#<SID>) i was unable to
> >>figure out what the issue is.
> >
> >Replace the <SID> by g:
>
> That's what I had thought of at first, but then I thought it would
> cause problems if any plugin were using the same variable name.

Well, I don't think, any plugin should pollute the global namespace by
so obvious names. If any does, I would consider this a bug and it should
be fixed.

> Can't you define script-local variables in such a way that they be
> usable in mappings defined in that same script, the way you can with
> functions?
>
> Oh, after rereading ":help <SID>": use s: in :let but <SID> in the
> mapping? Or define the mapping to call a function and use the s:
> variable inside the function?

I think, using a function is simpler:

au VimEnter * :let s:curdir = getcwd()

fu! <sid>AdjustCWD()
if getcwd() == s:curdir
lcd %:h
else
exe 'lcd' s:curdir
endif
return "\e" " abort mapping
endfu
nmap <expr> <f5> <sid>AdjustCWD()

or slightly shorter:

au VimEnter * :let s:curdir = getcwd()
fu! <sid>AdjustCWD()
if getcwd() == s:curdir
lcd %:h
else
exe 'lcd' s:curdir
endif
endfu

map <silent> <f5> :<c-u>call <sid>AdjustCWD()<cr>


regards,
Christian
--
"Was einem angehört, wird man nicht los, und wenn man es
wegwürfe."
-- Goethe, Maximen und Reflektionen, Nr. 112

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: