Monday, January 27, 2020

Re: Conditional configuration based upon filenames

Gary:
Thanks for all the useful tips.  I'll look into each of them in turn.



On Saturday, January 25, 2020 at 3:31:01 PM UTC-8, Gary Johnson wrote:
On 2020-01-25, cjsmall wrote:
> I want to do some conditional setup and key assignments based upon the
> name of the file currently being edited.  Here is what I've got so far as the
> .vimrc file in the target directory:
>
> so /u/jeff/lib/vi/vimrc.html
> so /u/jeff/lib/vi/vimrc.tab4
> " =============================================
> :if (@% == ".vimrc")
>     " Alt-F1: Insert contents from hidden file .vim_01
>     nnoremap  <Esc>[29;3~ :read .vim_01<CR>
>     inoremap  <Esc>[29;3~ <ESC>:read .vim_01<CR>
> :elseif (@% == "editor.html")
>     " Alt-F1: Insert contents from hidden file .edit_01
>     nnoremap  <Esc>[29;3~ :read .edit_01<CR>
>     inoremap  <Esc>[29;3~ <ESC>:read .edit_01<CR>
> :elseif (@% == "gimp.html")
>     " Alt-F1: Insert contents from hidden file .gimp_01
>     nnoremap  <Esc>[29;3~ :read .gimp_01<CR>
>     inoremap  <Esc>[29;3~ <ESC>:read .gimp_01<CR>
>     "
>     " Alt-F2: Insert contents from hidden file .gimp_02
>     nnoremap  <Esc>[1;3Q :read .gimp_02<CR>
>     inoremap  <Esc>[1;3Q <ESC>:read .gimp_02<CR>
>     "
>     " Alt-F3: Insert contents from hidden file .gimp_03
>     nnoremap  <Esc>[1;3R :read .gimp_03<CR>
>     inoremap  <Esc>[1;3R <ESC>:read .gimp_03<CR>
> :endif
> " =============================================
>
> This allows me to remap my Alt-function keys on a file-by-file basis to
> insert snippets of file-dependent code.  This works fine when editing
> a single file, for example:
>
> vim gimp.html
>
> but it does not work when multiple files are specified as in:
>
> vim editor.html gimp.html
>
> The conditional assignments are made upon entering  the first file,
> but do not automatically update when moving on to the second file.
>
> Three questions:
>
> 1) How can I make this update automatically as I switch between files?

You can use autocommands, for example:

    " Alt-F1: Insert contents from hidden file .vim_01
    au BufNewFile,BufRead .vimrc nnoremap <buffer> <Esc>[29;3~ :read .vim_01<CR>
    au BufNewFile,BufRead .vimrc inoremap <buffer> <Esc>[29;3~ <ESC>:read .vim_01<CR>

Note the use of <buffer> which restricts the mapping to the buffer
that was current when the mapping was defined.

See

    :help autocommands
    :help 40.3
    :help :map-<buffer>

> 2) I could do this by forcing every file to resource the .vimrc file upon
> entry.  However, the vim: modeline only works for set options.  Is there
> a similar method of executing a command (e.g., :source .vimrc) upon
> entering a file?

You can use an autocommand to source a file as well, but I wouldn't
source your .vimrc.  That generally causes headaches and there are
better solutions.

> 3) Some files have symbolic links and may be accessed through
> alternate names.  Is there a variable that vim has that can produce
> the "real" filename?

Take a look at

    :help resolve()

> Assuming that cannot be done, how do I write a vim logical OR
> conditional; something like:
>
> :if ((condition 1) || (condition 2))
>
> I know this must be in the :help documentation, but so far I haven't
> stumbled upon it.

Expressions are documented in eval.txt.  For a logical or, see

    :help expr-||

HTH,
Gary

--
--
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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/f127866e-2861-41ff-9555-eae2cfe99303%40googlegroups.com.

No comments: