vim - run :!commands in my .bashrc [duplicate] - bash

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Commands executed from vim are not recognizing bash command aliases
Why doesn't my vim know my alias?
say I set
alias kapow='grep'
in my .bashrc, which I source after.
I open vim, type
:!kapow "dude"
but vim tries to run /bin/bash kapow, when I actually wanted it to run my alias.
How does one run commands from a bashrc inside of vim (without leaving to the :shell)?

The vim manual says this about :!
On Unix the command normally runs in a non-interactive shell. If you want an interactive shell to be used (to use aliases) set 'shellcmdflag' to "-ic".

Related

How to make .bashrc aliases available within vim ':!some_command' shell command? [duplicate]

This question already has answers here:
How to make the vim call function in .bashrc?
(5 answers)
Closed 4 years ago.
When I try to call an aliased shell command from vim using :! it says
bash: l: command not found
shell returned 127
l is
alias l=ls
(from my .bashrc)
Use the vim command :set shellcmdflag=-ic which tells it to use the -i argument when starting a shell when executing a :! command. The -i argument means interactive (the shell you are commonly using in terminal), and the way it solves the problem is to use the -i argument, which tells bash to read the .bashrc with your aliases (and probably .bash_alises if you have one). But that may not be what you want because this solution leads to weird behaviour, like vim going into the foreground (use 'fg' to recall it).
A better solution is add this line to your .bashrc (or .bash_aiases):
shopt -s expand_aliases
Then all aliases, even in non-interactive shells, will be expanded correctly.
Also, add this to your .vimrc so the aliases file is actually read each time you run a shell command from within vim:
let $BASH_ENV = "~/.bash_aliases"
Original answer: https://stackoverflow.com/a/19819036/6152931
Bash manual (explains the difference between interactive and non-interactive shells).

Fastest way to find current file for alias [duplicate]

This question already has answers here:
How to find out where alias (in the bash sense) is defined when running Terminal in Mac OS X
(12 answers)
Closed 8 years ago.
Normaly e.g. alias ll="ls -la"
is stored in ~/.bashrc (or ~/.bash_profile).
But what ist the most effective way to find the location where is an alias stored?
I come across this question, because alias la runs perfect, but it's not stored in
the usual suspected locations.(~/.bashrc)
How can I find out where is an alias located?
echo $SHELL
/bin/zsh
And yes, I know for zsh is .zshrc the usual suspect.
The alias you create on the command prompt will be valid only till the current session. Once you exit the session and re-login, it will not be preserved. So it is upto you where to store it depending on its usage. e.g. for login specific, you can store it in /etc/profile. If you want it sourced during non-login session, the use .bashrc, if you want it to be sourced always, then use bash_profile. I am assuming bash shell. Reference - http://www.linuxforums.org/forum/newbie/147054-where-alias-stored.html
So I guess the answer is that it is really upto you to manually store it. Just because you define it on the command line, will not get it stored automatically.

Bash error after opening Terminal and running shell scripts (CentOS) [duplicate]

This question already has answers here:
Error message on Terminal launch [duplicate]
(2 answers)
Closed 6 years ago.
Every time I open Terminal in CentOS 6.4, I get the error:
bash: usr/local/bin: No such file or directory
I've checked .bashrc and .bash_profile to see if there are any lines that reference usr/local/bin, but haven't found anything. The same error also appears when I switch to root, or run a shell script.
Is it as simple as adding a backslash in front of usr? Like so--
/usr/local/bin
Still don't know where the error is happening though. Any help is much appreciated. Thanks!
This is strange as the normal bash directory on a centos 6.4 system is /bin/bash, however I would advise you to check the following:
echo $SHELL
It should pull your SHELL environment variable to show you where what shell you are using, normally it looks like this:
SHELL=/bin/bash
If it's different say for example:
SHELL=usr/local/bin/bash
then I would check your passwd file to make sure your users default shell is pointing to the right place.
username:x:601:601::/home/username:/bin/bash
Also I would suggest check where you shell actually lives
which bash
/bin/bash
And make sure everything is pointing to the correct location.

"Git Bash here" is not preserving bash history between sessions [duplicate]

This question already has answers here:
.bash_history does not update in Git for Windows (git bash)
(11 answers)
Closed 2 years ago.
I am running Git-1.8.0-preview20121022 on Windows 7 and the install was with "Git Bash Only" (least intrusive to Windows cmd).
When I open the Git Bash from the start menu shortcut, everything is fine with the history.
But when the Git Bash here context menu (either the git-cheetah shell extension one or the simpler registry one) is what launched a session, the commands from that session are not saved to the .bash_history.
How could figure out why this is happening? Or better yet, does someone know how to fix this?
You should be able to fix this by adding this line to your ~/.bash_profile
PROMPT_COMMAND='history -a'
As mentioned here: https://stackoverflow.com/a/60718848/6680510
Create the following files
~/.bash_profile
~/.bashrc
And put the following line in both of them
PROMPT_COMMAND='history -a'
To do this from the console (git bash) itself use the following
commands
echo "PROMPT_COMMAND='history -a'" >> ~/.bash_profile
echo "PROMPT_COMMAND='history -a'" >> ~/.bashrc
What history -a means
From history --help command
-a append history lines from this session to the history file
What is PROMPT_COMMAND ?
Bash provides an environment variable called PROMPT_COMMAND. The contents of this variable are executed as a regular Bash command just
before Bash displays a prompt.
Difference between .bash_profile AND .bashrc
.bash_profile is executed for login shells, while .bashrc is
executed for interactive non-login shells.
When you login (type username and password) via console, either
sitting at the machine, or remotely via ssh: .bash_profile is executed
to configure your shell before the initial command prompt.
But, if you’ve already logged into your machine and open a new
terminal window (xterm) then .bashrc is executed before the window
command prompt. .bashrc is also run when you start a new bash instance
by typing /bin/bash in a terminal.
On OS X, Terminal by default runs a login shell every time, so this is
a little different to most other systems, but you can configure that
in the preferences.
References
https://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x264.html
https://apple.stackexchange.com/questions/51036/what-is-the-difference-between-bash-profile-and-bashrc
Putting
PROMPT_COMMAND='history -a ~/.bash_history'
into the .bash_profile did it for me.

Running a command behind BASH prompt in bashrc [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Start background process from .bashrc
I'm executing a script in .bashrc and the user prompt will not appear until all commands have executed. Is there a way, on launching a terminal, that the prompt appears while the commands execute in the background?
Thanks for any assistance!
append & to your command to have it run in the background.

Resources