Vi syntax-highlighting after executing vi within bash script - bash

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 :)

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

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).

Related

-bash: emacs.profile: command not found

I am trying to resolve this issue and trying to get it work. What are all the steps of the solution? I really don't know what to do on my Mac terminal
-bash: emacs.profile: command not found
What are you trying to accomplish? If you're trying to open a file named .profile with emacs you'll want to add a space between the command emacs and the file .profile:
$ emacs .profile
If you are typing emacs.profile you will get an error because you are mixing together the command emacs and the file you are trying to edit: .profile. Therefore the solution would be to add a space between them like someone suggested: $ emacs .profile.
On the other hand, if you are typing emacs .profile and you are still getting: -bash: emacs: command not found, this means that you haven't installed emacs yet and your system doesn't recognize that instruction.
Emacs is a very popular text editor and is widely used by technical users, but it is not installed by default.
Solution 1:
If you want to edit a file (in this case ~/.profile) you don't need emacs to do so, you could just use a normal text editor or a pre-installed text editor using your terminal like:
$ vi ~/.profile or $ nano ~/.profile
(Guide to use Vi/Vim text editors) (Guide to use Nano).
Solution 2:
If you want to install emacs to edit that file, you can do so using brew:
$ brew update
$ brew install emacs
And after that you can use:
$ emacs ~/.profile
You can find other ways to install emacs here.
Important Note:
One important thing to consider in this example, is that if you are trying to edit the .profile file (i.e. to set an exported environment variable) you should know that the name of the .profile file might vary from one Mac system to another. The name of this profile configuration file depends on the shell of your system, if you are using bash the name of this profile will be .bashrc or .bash_profile.
Before editing your profile file make sure what is the name of the profile file you are trying to edit. To do so, you can use:
$ cd $HOME
to go to the Home folder and then use:
$ ls -al
to see hidden files (These files starting with . are hidden files). You will find the profile file of your system there.

bash script "ignores" .vimrc

I have a problem as follows:
I have a script which copies a log file from remote machine, does some modification on it and then opens it in vim, problem is vim doesn't auto recognize the file type (which outside of the script id does) – I need this for coloring the log.
Script as follows:
/usr/bin/rcp 14.1.61.10$node:/output/LocalLog_IPNode$node.log /export/home/fpd/tmp/tmp_local_log
chmod 777 /export/home/fpd/tmp/tmp_local_log/*
sed -i 's/[A-Z]\{4,8\}.*[oigus][kbdct][sel]\//---/g' /export/home/fpd/tmp/tmp_local_log/LocalLog_IPNode$node.log
vi /export/home/fpd/tmp/tmp_local_log/LocalLog_IPNode$node.log
My .vimrc:
au BufNewFile,BufReadPost LocalLog* set filetype=local_log
Note that the files opens in vim (if it helps the manual command ":set syntax=local_log" doesn't work either).
After exiting the script and manually opening the log everything works fine =(
Your problem is that the autocommand option is only availaible in vim and not vi.
So if this is available on your system, you should replace the last command line by :
vim /export/home/fpd/tmp/tmp_local_log/LocalLog_IPNode$node.log
Vim stands for "Vi Improved" and many options are only available in the latter.
To be sure you can do:
:help autocommand
It is always mentioned if the feature is vi or vim compatible.

~/.profile, ~/.bashrc, and ~/.bash_profile not running on new terminal start up

I am trying to create a permanent alias for my terminal. I put the alias in my ~/.profile, ~/.bashrc, and ~/.bash_profile files, previously empty. When I start a new terminal, bash does not recognize the alias, but if I source any of them, it does. Why are these not getting run when I open a terminal? I am on OSX.
Newer MacOS versions use zsh as the default shell for both Terminal and iTerm2. Run echo $SHELL to confirm if this is the case for you.
Zsh looks for a .zshrc file upon shell startup, so to continue using zsh while sourcing the contents of your bash profile, you can run the following:
echo "source ~/.bash_profile" >> ~/.zshrc
Open a new terminal window for the changes to take effect.
Two things need to happen here when using iTerm to get the loading of dotfiles to work.
First you should add the following to your .bash_profile
[[ -s ~/.bashrc ]] && source ~/.bashrc
Secondly you will need to ensure that in iTerm preferences your terminal is set to launch a login shell.
Hope this helps!
Using the default mac terminal, what worked for me was to add a command to run on start up to source my .bash_profile.
Preferences > Profile > Startup > Add command 'source ~/.bash_profile'
Mac terminal preferences window screenshot
Might be considered to be a bit hacky, but it does the trick.
Adding source ~/.profile to my .bash_profile worked for me.
As of High Sierra, both Terminal and iTerm want to load ~/.profile first. So I suggest you put one line in your .profile to make your Mac work like other Unixes:
source ~/.bash_profile
By editing this one file, you won't have to search through the menus of multiple apps to override Apple's bizarre behavior in each.
As of Catalina the default shell is now zsh. You can change it back to bash with chsh -s /bin/bash and that should load your .profile or .bash_profile
Why are your shell's initialization files not loading?
As with most things, It Depends ™
I recently experienced the same phenomenon and went through the following exercise to resolve it:
I use iTerm. iTerm runs a login shell by default. Verify in iTerm Preferences > General > Command > (*) Login Shell
Therefore, I know that ~/.bash_profile will always be called.
Knowing that, I put the following in my ~/.bash_profile file:
for file in ~/.{bashrc,bash_exports,bash_aliases,bash_functions}; do
[ -r "$file" ] && source "$file"
done
unset file
Notice that I use separate files for .bashrc, .bash_exports, etc. It keeps things separate and simple.
Note also that /etc/profile is loaded first, but since I have never used that system wide init file, I knew that that was not my problem. For details check out $ man bash
So, I started with my ~/.bash_profile file.
I found that when I installed Canopy Express that it's installer replaced the contents of my ~/.bash_profile file with the following content:
# Added by Canopy installer on 2017-04-19
# VIRTUAL_ENV_DISABLE_PROMPT can be set to '' to make the bash prompt show that Canopy is active, otherwise 1
alias activate_canopy="source '/Users/lex/dev/python/User/bin/activate'"
# VIRTUAL_ENV_DISABLE_PROMPT=1 source '/Users/lex/dev/python/User/bin/activate'
p.s. Canopy is an excellent, free python IDE, that I highly recommend.
Fortunately, I backup my ~/.bash* files so restoring that was easy and quickly fixed my issue.
My advice would be to understand the order of calls to your initialization files and start with the first one and work your way through them until you find the problem.
Oh, and you may want to verify which shell you are using (I use bash):
~ $ echo $SHELL
/usr/local/bin/bash
I am guessing you may use another shell, such as bash, tcsh, sh, zsh etc.
Put source .bash_profile into your appropriate 'bashrc' file will make the auto loading recovered, i.e.
.login for tcsh, .bash_profile for bash, .zshrc for zsh
My issue was solved by unchecking Preferences > General > tmux >
Use "tmux" profile rather than profile of the connecting session
Most likely, you need to create the files yourself as they appear not to exist by default. You should give them execute permission to make them run.
~ % sudo chmod 700 ~/.bash_profile
Also, you should check the ownership of the files. They should belong to current user rather than root. Otherwise, you will get permission denied error.
~ % ls -a -l
~ % sudo chown <user_name> ~/.bash_profile
Finally, please note that bash looks in your home directory for .bash_profile, .bash_login, and .profile in order. Bash will stop looking if the first is found.
This means if you have both .bash_profile and .profile files, the .profile will not run.
For more information
Hope this would help you.
Little late to the party but it seems that the file .zprofile is the equivalent to that of .bash_profile when loading zsh. I used this instead to execute a few commands on startup. Of course this only valid for a specific iTerm setup with zsh.
https://zsh.sourceforge.io/Intro/intro_3.html

macvim shell (:sh) only displays character codes / escape sequences

Just compiled MacVim with homebrew. Here's what it looks like when I :sh and then type ls:
http://cloud.jtmkrueger.com/image/2N0S0T3k3l1J
As you can see, it's just character codes.
UPDATE
I run oh-my-zsh
Tried installing the plugin named here:
http://vim.1045645.n5.nabble.com/ANSI-colors-td1219411.html
Didn't seem to help
ANOTHER UPDATE
Upon removing my zsh syntax highlighting plugin It seems to work ok. Is there a way to turn off zsh plugins when using oh-my-zsh only when it's a vim 'dumb terminal'?
When you do :sh in GVim or MacVim, you don't get a real terminal emulator.
It's "dumb" and there's no way to make it understand those escape sequences. You better get used to it or ask (with convincing arguments and a ready-made patch) on the vim-dev mailing list.
You might want to try the ConqueTerm plugin which does its best to interpret ANSI sequences, even inside MacVim.
Just for the reference, :h guioptions now support the following flag:
'!' External commands are executed in a terminal window. Without
this flag the MS-Windows GUI will open a console window to
execute the command. The Unix GUI will simulate a dumb
terminal to list the command output.
The terminal window will be positioned at the bottom, and grow
upwards as needed.
Set :set go+=!, run :sh, and be surprised :).
What you see is actually not just character codes, but your usual shell prompt which contains color codes. You can probably disable it by redefining PS1 or remove your modified definition in ~/.bashrc.
If you would like to use a color prompt on the command line, but not in MacVim you can fix this in ~/.bashrc by setting PS1 differently when inside vim (from here)
if [ $VIM ]; then
export PS1='\h:\w\$ '
fi
You could try the following, instead of ls, type command ls; it shouldn't show the escapes codes.
If it works you can simple create a new file in a folder in your path, say vls, with the following contents:
#!/bin/sh
command ls $#
after that chmod +x vls and again, if it is in your path, you should be able to use that from vim.

How to set my GVim to be feel like IDE?

Currently, I'm using GEdit as my text editor for editing Ruby and Javascript source codes. I would like to give GVim a try to be my editor choice. I have tried to follow https://github.com/akitaonrails/vimfiles and few others instructions, but I don't get any luck, when I source ~/.vimrc, then I always get:
bash: /home/samnang/.vimrc: line 5: syntax error near unexpected token ('
bash: /home/samnang/.vimrc: line 5:call pathogen#runtime_append_all_bundles()'
Could you point me somewhere to get the instruction or configuration?
Environment: Ubuntu 10.10
Edit: If I don't source it, when I type vim or gvim, then I got:
Vim: Caught deadly signal SEGV
Vim: Finished.
Segmentation fault
You need to run source ~/.vimrc as an Ex mode command. That is, inside Vim itself, hit : and enter the command:
:source ~/.vimrc
Right now, you're running Bash's source command, which is entirely not what you want to do.
~/.vimrc is the configuration file for vim, and will automatically be read by vim when it launches in order to know how you want it set up. You can add your non-graphical vim commands, such as key mappings, abbreviations to ~/.vimrc/. In ~/.gvimrc you can add commands to set your colorscheme, the default number of columns and rows displayed at startup, etc.
If you aren't familiar with vim itself type vimtutor at the command-line and go through the tutorial.
To start gvim, type gvim at the command-line. To edit a file you can either open gvim, then use :e file/to/load in comman-mode, or do gvim file/to/load at the command-line. gvim supports multiple windows and tabs as does vim, so study those things to make the most use of them.
You can also try integrating Vim with eclipse if you want to bring IDE functionality to Vim (like projects, error highlighting, code completion, etc).
If you're interested check out the eclim project. It supports most of the modern languages, including Ruby, and I highly recommend it.
Try adding a .vim or vimfiles in your $HOME directory. If it still fails, add a file to the .vim directory. I did a
cd
mkdir .vim
cd .vim
touch .netrwhist
chmod g+w .netrwhist
I discovered this while learning about building your own syntax files at vim wikia creating your own syntax files

Resources