How do I run a shell command from vim in the existing terminal SCM session? - vim

Here's the problem statement:
I open a GNOME terminal and run a startup script setup_myproject_view1 which sets up an SCM session and sets some enviroment variables.
I open terminal vim and start browsing through some files often in multiple split panes (:sp, :vsp)
I realize that I need to edit and save one or more of these files. But before I can do that I need to check out the file from the SCM server/depot.
Now I have multiple options:
Close all my vim panes and run the checkout command in the shell and then open one or all of the files I had open before. Very tedious.
Open a new terminal window or tab and run the startup script and checkout the file. Then return to the original terminal, run :e to load the file again with write permission enabled and proceed to edit it. Still tedious because I have to run the startup script (so I have to remember which project view I'm working in) and switch through windows/tabs.
Run the shell check out command from vim using :shell or :!. This would be ideal because I'm quickly able to return to editing without breaking my train of thought. But the shell inside vim again does not have the startup session setup_myproject1_view1. Nor do I want to put setup_myproject_view1 to run automatically in my shell's .cshrc file because I have different projects and views: setup_myproject3_view2, etc.
So how can I run a shell command conveniently from vim without having to setup the SCM project view?

you can pause vim using ctrl+z, do your checkout and then resume it with fg if you invoked vim from a bash shell. For more information see bash job control
An alternative, you should modify your setup script to export variables to underlying shells. For example if your script is like this:
# script.sh
MEH=4
you will get this result when you use it:
source script.sh
vim
:shell
echo $MEH
exit
but if you change it like this:
# script.sh
export MEH=4
you will get this result when you use it:
source script.sh
vim
:shell
echo $MEH
4
exit

Related

Why must I run . ~/.bashrc each time I want to access my shortcuts there?

To speed up some of my Linux commands, I followed the recommendations of this Lifehacker article by writing aliases to quickly cd into folders I like. Fran's answer to "source" the .bashrc file by executing this line enabled me to use my shortcuts:
. ~/.bashrc
Like the OP in this post, it is unclear to me how to always access my shortcuts without having to rerun that command.
This is an issue lots of people run into when new to Bash. It comes from not understanding which files are read when.
Your .profile or .bash_profile is read when you log on. This is a login shell.
Your .bashrc is run whenever you start another interactive shell.
Typically you will want to include a source ~/.bashrc as the last thing that happens in your profile to ensure that your login shell has these same aliases.
Per the bash man page:
When an interactive shell that is not a login shell is started, bash
reads and executes commands from ~/.bashrc, if that file exists.
Therefore, every time you open a new interactive bash shell (such as when you open a new terminal), ~/.bashrc will automatically be read.

Vim Ex mode loads when opening terminal

I know little about Vim in terminal(Mac) and the other day I was working copy and pasting text and i think I accidently did it when in terminal. Now whenever I open terminal it instantly loads on Vim Ex mode. I know how to quit Ex mode once in terminal but is there any way i can get rid of Vim loading when I open Terminal?
Thanks
Edit: To explain further to what i mean when I open terminal.app from Utilities I get the following
and the only way I get back to the command prompt is by typing quit every time I open terminal and i cant understand why the Vim process is running in the first place.
I was just outside the terminal in a document copy and pasting text then accidentally did a command v to paste within terminal which resulted in this happening.
It appears that you've accidentally updated one of your shell startup scripts so it launches vim.
If your default shell is csh or tcsh, take a look at .cshrc, .tcshrc, and .login in your home directory, and look for a command like vi -e or vim -e.
If your default shell is bash, check .bashrc and .bash_profile.
It may be easier to figure out which file you messed up by checking which file in your home directory was modified most recently:
% ls -altr $HOME | tail
-a lists all files, including files whose names start with ..
-l gives you a long listing, showing timestamps.
-t sorts by modification time.
-r reverses the order, so newer files are shown last

How to add in bash for auto completion of arcanist commands

I am new to linux.
I am trying to set up arcanist.
I am done with git clone and adding the path in environment variable, however I am confused on how to set up the tab completion for arcanist commands.
In the arcanist user guide it says that you need to add source /path/to/arcanist/resources/shell/bash-completion to your .bashrc, .profile files.
What are these files and how can I edit them to work with arcanist with the tab completion.
The .bashrc, found in your home directory, is the configuration file for that user for bash.
There is a global bashrc usually located in /etc/bashrc.
The difference to .profile is that the .bashrc is executed every time you start a terminal (bash) while .profile only once when you use a login shell.
The command source loads everything that is inside the file you use with that command and treats it as if you wrote those commands in the .bashrc yourself. I guess in /path/to/arcanist/resources/shell/bash-completion are aliases/functions/etc. which enable tab-completion with arcanist.
Edit: for bash, the profile file is usually called .bash_profile.
After lot of googling and asking people, I finally did it.
First of all I had to export the path where my arcanist code from the github has been cloned in the ~/.bashsrc file(in bold below)
export PATH="$PATH:$HOME/.rvm/bin:$HOME/arcanist/bin/"
After this command, I copied the function which was present in the /arcanist/resources/shell/bash-completion into the bashrc file
And then I closed and open the terminal and bingo I was able to get arc and it's command as auto completion on striking tab.
Thanks ap0 for the comments.

what is .bashrc - How to find the startup file - putty

I was going over this article and it states in step 3
Add the following to your .bashrc (or the appropriate startup file for your shell) To use it immediately, be sure to type “source .bashrc”
Any idea on how I could know what my startup file is ? I am using putty ?
Once you use putty to SSH into your server, you can run "ls -al .bashrc" and it should show you the file, edit this with an editor you know, if none, then use vi like this "vi .bashrc".
Go to where you need to edit the file and type in "i" to put vi in Insert mode. Next type in your text. Once you are done press the escape button and ":wq", no quotes for the i or :wq.
Next you can source it by typing "source .bashrc" and the setting you added should be part of your BASH shell environment now.
The .bashrc is a file which is called by bash before on each start of a new interactive shell. The file can be used to setup the environment, export variables, create aliases and functions and more...
There are usually multiple instances of that file. One per system and one per user to allow system wide configuration but also customization by users ( users bashrc will be sourced after the system wide bashrc and can overwrite things). I suggest to add the lines to your user's bashrc first. The file is located in your home folder. Type:
vi $HOME/.bashrc
in order to edit the file. If you aren't familiar with the vi editor you can choose an editor of your choice like nano, mcedit or even a GUI text editor, but mind that a GUI editor's file dialog may hide the file because it's name starts with a .
Once you managed to edit the file, start a new connection or simply type
source $HOME/.bashrc
in order to parse the file
A path which will work with any bash shell regardless of operating system (macOS/Linux/BSD etc.) is:
~/.bashrc
check your home directory ...because it exists in user's home directory.
check /home/username/ on your terminal if you are using RHEL or CentOS.
.bashrc and .bash_profile are bash config files (bash shell script) that bash runs(execute) whenever it is started interactively. It initializes an interactive (non-login) shell session and the config is read from these files $HOME/.bashrc
.bashrc is a standard hidden file located in your home directory.It determines the behaviour of interactive shells.
.bashrc runs on every interactive shell launch.If you say: $bash
For login shells, the config is read from these files:
/etc/profile (always sourced)
$HOME/.bash_profile (the rest of these files are checked in order until one is found,then no other are read)
$HOME/.bash_login
$HOME/.profile
For example: I added an echo to my .bashrc and .bash_profile files and whenever I called bash or bash -l command in terminal it showed me the echo.

Terminal window closes after double clicking executable .sh file in Fedora Linux

I have to work on a project in Fedora Linux and I have to type the following very often:
player map1.cfg &
I figured out that I can create an executable .sh file and it contains this:
#!/bin/bash
player *.cfg &
However, when double click on the runmap.sh file it shows me 'Run in Terminal', 'Display', 'Cancel', and 'Run' and when I click 'Run in Terminal' a terminal window opens and closes immediately. If I just hit 'Run' then the .cfg opens but I need the Terminal window to run additional (Java) files.
How can I fix this problem?
Other information:
I use *.cfg because I want to copy and paste the .sh files into other folders that also contain .cfg files such as map2.cfg, map3.cfg, etc.
It's for a Player/Stage project.
When you run a script from the file manager, the shell that is started isn't interactive. The shell can only read the script file.
To open an interactive shell in addition to the files, you can exec the new shell at the end of the script, and use "Run in Terminal":
#!/bin/bash
player *.cfg &
exec /bin/bash
Well let's look at it this way.
When you run the command in a terminal, the command starts as a child process and is then sent to the background. Once the command finishes it terminates. During the time it is running in the background you can still issue commands because your parent process is the terminal window itself.
When you write a script that issues a command to run in the background it is started, spawns the command as a child to it and then closes because the script has finished.
These are a behavior of the OS and something that really shouldn't change. Essentially what you are therefore asking for is a way for it to run the command quickly for yourself yet still leave a command terminal for you to work with?
1) Why is typing the command such a hassle? Bash and other terminals have a history function for this very reason.
2) Why don't you just call the mini script you wrote from a terminal window whenever you need to call the commands. If you put the script in a folder on your $PATH variable it will be available to you in the terminal at any location.

Resources