8

If I enable bash's input mode using set -o vi, then press Esc followed by v, I get a vi window which allows me to edit a temporary file which is executed once I leave. In that window I would like to enjoy Vim syntax highlighting for Bash scripts. It doesn't suffice to execute :syntax enable. The problem might be related to the fact that the temporary file has no .sh ending nor a #!/bin/bash head which could be used to determine the filetype.

4
  • What does echo $EDITOR output? Might be that you are actually calling vi or vim in compatible mode instead of vim. vim can usually pick up the filetype from heuristics like the shebang line. Commented Aug 18, 2011 at 23:26
  • would this solution be applicable? stackoverflow.com/questions/5487338/… Essentially it sets the filetype to a default if one isn't detected. Commented Aug 18, 2011 at 23:27
  • The problem might be related to the fact that the temporary file has no .sh ending Heh, you may have answered your own question.... Commented Aug 18, 2011 at 23:29
  • 1
    Sometimes identifying the problem doesn't render its solution trivial... Commented Aug 19, 2011 at 20:49

2 Answers 2

8

I'd use the shorter formulation:

au BufRead,BufNewFile bash-fc-* set filetype=sh

I believe this type of autocmd is the canonical way to handle filetype assignments (at least, my .vimrc has a number of them).

@Eric Fortis, please chime in or correct me if there's a reason you did it differently.

Sign up to request clarification or add additional context in comments.

Comments

7

Add this to your .vimrc

if expand('%:t') =~?'bash-fc-\d\+'
  setfiletype sh
endif

the temporary files are of the form bash-fc-3537253897, so the regex matches if the file begins with bash-fc- and applies the filetype.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.