Conditional configuration based upon filenames

16 views
Skip to first unread message

cjsmall

unread,
Jan 25, 2020, 5:46:34 PM1/25/20
to vim_use
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.

Gary Johnson

unread,
Jan 25, 2020, 6:31:01 PM1/25/20
to vim_use
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

cjsmall

unread,
Jan 27, 2020, 2:31:19 PM1/27/20
to vim_use
Gary:
Thanks for all the useful tips.  I'll look into each of them in turn.
Reply all
Reply to author
Forward
0 new messages