Saturday, January 25, 2020

Conditional configuration based upon filenames

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?

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?

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

Thanks.

--
--
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/dcffb6ae-286c-4507-ad24-31b956f2f7d5%40googlegroups.com.

No comments:

Post a Comment