1

I created this Stackoverflow account only for asking this question:

Is there a way to tell Vi to do syntax-highlighting when executing it from within a script like this:

#!/bin/bash
vi /path/to/script.sh

I was trying things like this and other stuff like sudo su - in order to reset the environment, but nothing worked..

Any chance a to make it work(preferably without editing the .vimrc)?

Thanks :)

rumpel
  • 33
  • 1
  • 5

2 Answers2

2

You can run a command when you launch vi after the file is loaded:

vi -c "syntax on" /path/to/script.sh

That should turn on syntax highlighting

  • That didn't work :/ – rumpel Sep 15 '22 at 17:08
  • What happens ? Can you provide more information on how you did it ? – Fabio Almeida Sep 16 '22 at 14:04
  • I tried multiple ways e.g.[this](https://stackoverflow.com/questions/2576687/how-to-highlight-bash-scripts-in-vim)): `#!/bin/bash vi -c "syntax on" script.sh` `#!/bin/bash vi -c "syntax on" -R +'setf bash' scripts.sh` `#!/bin/bash echo "vi script.sh" 0> `tty` # to get it forwardet to another terminal (plan was to exit the script and execute it "fresh" after the script exits` As you can see the way is always the same: Start a Script -> run vi script.sh within it. But the result is always without syntax highlighting – rumpel Sep 16 '22 at 16:26
  • This works perfectly for me: `vi +"syntax on" +"set ft=python" run.sh` – Fabio Almeida Sep 16 '22 at 19:24
  • Didn't work, too.. you ran that command from within a script or directly from cli? – rumpel Sep 16 '22 at 20:23
  • From within a script as per your question... – Fabio Almeida Sep 22 '22 at 18:09
  • Nevermind. I was just wondering since it seems to be so easy, regarding your answer. I was trying different terminal-clients too (xfterm and the gnome3-client) with same result .. – rumpel Sep 24 '22 at 07:31
0

On most Unix systems, vi is not vi itself, but a symlink to /etc/alternatives/vi, which itself is a symlink to a vi-alike editor. You can find out which editor that is with the command ls -l /etc/alternatives/vi. Fabio Almeida clearly has vim, which has syntax highlighting, but you might have an editor like nvi, which doesn't. To use it, you would have to install a different editor (like vim).