I am writing a bash script which uses here strings (<<<
), please see example below. The script works fine and gives expected output, but the problem is that the (vim) editor syntax color is all messed up after the line where here string was used. Any clues why and also how I can fix it ?
As text:
# get all running screens
scrcmd=$(ps auxw|grep -i screen|grep -v grep|awk '{print $15}')
allscr=()
while read -r line; do
allscr+=("$line")
done <<< $scrcmd
echo "got screens, now do something else"
A here string can be considered as a stripped-down form of here document. It consists of nothing more than COMMAND <<<$WORD, where $WORD is expanded and fed to the stdin of COMMAND. Example 17-13. Prepending a line to a file. #!/bin/bash # prepend.sh: Add text at beginning of file.
Bash Line Editor―a full-featured line editor written in pure Bash! Syntax highlighting, auto suggestions, vim modes, etc.
bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.
The bash (really sh
) highlight mode in Vim is multipurpose; it tries to cover POSIX sh, bash, and ksh. You have to tell it you specifically want bash.
:let b:is_bash=1
:set ft=sh
It should highlight properly after that.
If you only ever care about bash, you could just make this your default in .vimrc:
let g:is_bash=1
EDIT: As pointed out by Charles Duffy in comments, if you use a #!
line, e.g.
#!/bin/bash
#!/usr/bin/env bash
Then vim should do the right thing on its own. That is probably easier, unless you have some reason you prefer not use #!
lines.
(Even though that's likely easier, I am leaving my answer here because in this case you do not have a #!
line, and that is not uncommon, especially in library code or files expected to be sourced, not executed.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With