vim-shebang icon indicating copy to clipboard operation
vim-shebang copied to clipboard

set is_sh or is_bash according to #!/bin/sh or #!/bin/bash

Open oryband opened this issue 9 years ago • 12 comments

If possible, i'd prefer #!/bin/sh to set g:is_sh=1 according to original sh syntax instead of bash.

oryband avatar Mar 30 '16 16:03 oryband

Hi @oryband!

Please correct me if I missed something.

You want to detect #!/usr/bin/sh as sh filetype, and #!/usr/bin/bash as bash?

vitalk avatar Mar 30 '16 16:03 vitalk

@vitalk yup, but since vim treats bash and sh as the same filetype sh along with the g:is_bash = 1 or g:is_sh = 1 variables, implementing this might be tricky.

oryband avatar Mar 30 '16 16:03 oryband

Edited original post text.

oryband avatar Mar 30 '16 16:03 oryband

Thanks @vitalk ! Will test this out and report if I have any problems

oryband avatar Apr 03 '16 14:04 oryband

@vitalk well, this doesn't appear to work right. Opening a .sh file with a #!/bin/sh on top, and executing :echo is_bash returns 1, meaning the syntax is bash, not sh. It should return 0.

oryband avatar Apr 03 '16 14:04 oryband

@oryband I don’t set global is_bash/is_sh variables and use buffer-local, e.g. b:is_bash/b:is_sh. The filetype.vim from my vim distribution uses them to change syntax highlighting appropriately.

Note, I don’t set

let b:is_bash = 0

for sh filetype and use

let b:is_sh = 1
if exists('b:is_bash')
  unlet b:is_bash
endif

the same is true for bash filetype.


For example, if I open the following script

#!/bin/sh

if [[ $# -ne 1 ]]; then
    exit;
fi

and change shebang to #!/bin/bash the syntax highlighting will change if I execute :Shebang command.

vitalk avatar Apr 03 '16 19:04 vitalk

@vitalk

  1. doesn't work for me on the following #!/bin/sh file, syntax is bash and doesn't change executing :Shebang: https://github.com/rounds/10M-Docker-Images/blob/d954b776ab1ce5d9a94564e36bd39eeb24636152/sensu/tmp/entrypoint.sh
  2. I didn't know there was :Shebang command. Perhaps you should generate vim help docs?

oryband avatar Apr 04 '16 08:04 oryband

My vim version:

VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Mar 23 2016 11:40:39)
Included patches: 1-1639
Compiled by Arch Linux
Huge version without GUI.  Features included (+) or not (-):
+acl             +farsi           +mouse_netterm   +tag_binary
+arabic          +file_in_path    +mouse_sgr       +tag_old_static
+autocmd         +find_in_path    -mouse_sysmouse  -tag_any_white
-balloon_eval    +float           +mouse_urxvt     +tcl/dyn
-browse          +folding         +mouse_xterm     +terminfo
++builtin_terms  -footer          +multi_byte      +termresponse
+byte_offset     +fork()          +multi_lang      +textobjects
+channel         +gettext         -mzscheme        +timers
+cindent         -hangul_input    +netbeans_intg   +title
-clientserver    +iconv           +packages        -toolbar
-clipboard       +insert_expand   +path_extra      +user_commands
+cmdline_compl   +job             +perl/dyn        +vertsplit
+cmdline_hist    +jumplist        +persistent_undo +virtualedit
+cmdline_info    +keymap          +postscript      +visual
+comments        +langmap         +printer         +visualextra
+conceal         +libcall         +profile         +viminfo
+cryptv          +linebreak       +python/dyn      +vreplace
+cscope          +lispindent      +python3/dyn     +wildignore
+cursorbind      +listcmds        +quickfix        +wildmenu
+cursorshape     +localmap        +reltime         +windows
+dialog_con      +lua/dyn         +rightleft       +writebackup
+diff            +menu            +ruby/dyn        -X11
+digraphs        +mksession       +scrollbind      -xfontset
-dnd             +modify_fname    +signs           -xim
-ebcdic          +mouse           +smartindent     -xsmp
+emacs_tags      -mouseshape      +startuptime     -xterm_clipboard
+eval            +mouse_dec       +statusline      -xterm_save
+ex_extra        +mouse_gpm       -sun_workshop    -xpm
+extra_search    -mouse_jsbterm   +syntax
   system vimrc file: "/etc/vimrc"
     user vimrc file: "$HOME/.vimrc"
 2nd user vimrc file: "~/.vim/vimrc"
      user exrc file: "$HOME/.exrc"
  fall-back for $VIM: "/usr/share/vim"
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H   -D_FORTIFY_SOURCE=2  -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1
Linking: gcc   -L. -Wl,-O1,--sort-common,--as-needed,-z,relro -fstack-protector -rdynamic -Wl,-export-dynamic -Wl,-E -Wl,-rpath,/usr/lib/perl5/core_perl/CORE  -Wl,-O1,--sort-common,--as-needed,-z,relro -L/usr/local/lib -Wl,--as-needed -o vim        -lm -lncurses -lelf -lnsl    -lacl -lattr -lgpm -ldl   -Wl,-E -Wl,-rpath,/usr/lib/perl5/core_perl/CORE -Wl,-O1,--sort-common,--as-needed,-z,relro -fstack-protector-strong -L/usr/local/lib  -L/usr/lib/perl5/core_perl/CORE -lperl -lpthread -lnsl -ldl -lm -lcrypt -lutil -lc   -L/usr/lib -ltclstub8.6 -ldl -lz -lpthread -lieee -lm

oryband avatar Apr 04 '16 08:04 oryband

syntax is bash and doesn't change

The sample your provided works as expected with my Vim distribution (the highlighting for ${tmpl%%.tmpl} part is changed). Just for a note, my distribution is a little bit newest.

VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Mar 31 2016 17:41:42)
MacOS X (unix) version
Included patches: 1-1655
Compiled by Homebrew
Huge version without GUI.  Features included (+) or not (-):

What is the output of :echo b:is_sh for your example? It should be 1.

Perhaps you should generate vim help docs?

Well, I opened issue #2.

vitalk avatar Apr 04 '16 09:04 vitalk

@vitalk I get

E121: Undefined variable: b:is_sh
E15: Invalid expression: b:is_sh

Trying let b:is_sh fails as well.

oryband avatar Apr 04 '16 14:04 oryband

Also, please note you're probably using MacVim while I'm running "standard" Vim under Arch Linux.

oryband avatar Apr 04 '16 14:04 oryband

I see the two possible issues:

  • Pre hook I defined don’t called and both of b:is_sh/b:is_bash variables are still undefined. Please, ensure the hook is called and output looks good

    AddShebangPattern! test  ^#!/bin/test   echo\ "looks\ good"
    

    for test file

    #!/bin/test
    

    output

    looks good
    
  • Your $VIMRUNTIME/filetype.vim doesn’t support b:is_sh/b:is_bash. Try to update vim. I really don’t know how to fix it.

vitalk avatar Apr 04 '16 19:04 vitalk