Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax highlighting in Bash vi-input mode

Tags:

bash

vim

vi

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.

like image 652
highsciguy Avatar asked Aug 18 '11 23:08

highsciguy


2 Answers

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.

like image 167
John Hart Avatar answered Nov 06 '22 01:11

John Hart


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.

like image 42
Eric Fortis Avatar answered Nov 06 '22 02:11

Eric Fortis