Opening a new terminal tab based on the current one - visual-studio-code

I've added the following keybinding in vscode to open a new terminal window:
{
"key": "cmd+t",
"command": "workbench.action.terminal.new"
}
When I execute a command like cd Desktop then run cmd+t again while still in it, the new terminal starts from zero. Is it possible for the new terminal to be based on the previous one? meaning in our case it should open a new terminal tab that runs on /Desktop instead of the root directory.
I am using bash.

Add the following to your User Settings (Menu:File > Preferences):
"terminal.integrated.cwd": "Desktop"
This will now open up in Desktop instead of having to cd into it.
I'm not sure how to change the cwd based on other open terminals per your question.Here's the full commented setting in vscode:
// An explicit start path where the terminal will be launched, this is
used as the current working directory (cwd) for the shell process.
This may be particularly useful in workspace settings if the root
directory is not a convenient cwd.
"terminal.integrated.cwd": "",

Related

How do you select the terminal location for every terminal type in a VS Code workspace?

I am working in a VS Code workspace with multiple directories. When I open the default terminal (Powershell) it get a prompt asking which of the directory roots I want to open the terminal in.
However, if I open another terminal from the dropdown, it opens at the root location of whichever file that I am currently displaying.
Is there a setting to show this prompt for every new terminal, not just the default one? Specifically for the WSL terminal.

Change default working directory for VS Code terminal

Is it possible to change the working directory for new integrated terminal windows in Visual Studio Code?
I have opened the folder /path/to/project in Code, so when I open a new terminal windows, it starts in that folder. I would like the terminal to open in /path/to/project/app so that I don't have to cd to that folder every time. Is it possible to configure this in settings.json?
It turns out this was fairly easy to find in the documentation at https://code.visualstudio.com/docs/getstarted/settings.
The setting that controls this is
// An explicit start path where the terminal will be launched, this is used
as the current working directory (cwd) for the shell process. This may be
particularly useful in workspace settings if the root directory is not a convenient cwd.
"terminal.integrated.cwd": "",
This can be added to settings.json in the .vscode folder. Absolute and relative paths are supported, so
"terminal.integrated.cwd": "app"
and
"terminal.integrated.cwd": "/path/to/project/app"
will both work.
if you are changing your directory in your .bashrc or .zshrc file to your home directory (cd ~/ or cd $HOME/) then your vscode console will use the directory that you have specified.
Bellow is a little code you can use in your .bashrc or .zshrc to address this:
if [ "$NAME" != "Code ]; then
cd ~
else
cd $OLDPWD
fi

How to open directory from terminal in vs code? but inside the same window

I have vs code open and I'm using bash terminal within it, when I'm inside the directory I want to open I type code . and that opens everything I want but it opens a new vs code window. Is there a way it can open inside of the current vs code window I am in?
Try code . -r to force opening in the same window. See https://code.visualstudio.com/docs/editor/command-line#_core-cli-options for more options in the command line.
For a more "permanent" solution there is this setting:
Window: Open Files In New Window
Controls whether files should open in a new window. Note that there
can still be cases where this setting is ignored (e.g. when using the
--new-window or --reuse-window command line option).
Set it to off and then ``code .` is enough.

How to open file using vscode terminal?

I want to open a new tab and open a file using vscode terminal.
like open somefile.js command, so I can use ls and open file quickly.
Use code -r <file> to open the file in the last active code window
use
code -r <filename>
just remeber to install the code command to PATH. in VScode, open the command palette and type "code", you should see a Shell Command: Install code to PATH option.
I really haven't actually seen any difference between using the command without the -r flag.
If you are already in VSCode terminal (not an external OS terminal), check out the latest VSCode 1.64 (Jan. 2022) Terminal shell integration:
The terminal now features experimental opt-in shell integration which allows VS Code to gain insights on what is going on within the terminal as it was previously a black box.
When enabled using "terminal.integrated.enableShellIntegration": true, arguments to run a shell integration script will be injected into your terminal profile if possible.
The script itself mostly just injects invisible sequences into your prompt, providing us with information like where the prompt, command and command output is, what the current working directory (cwd) is for each command and the exit code of each command.
That means:
Link support relative to the cwd
Since we know the cwd for each line in the terminal buffer, we can support opening links in the terminal relative to the cwd at the location where it was activated.
Before, when a link was clicked, a quick pick would open with results from any folders containing a match for that name.
Now, the exact file match will be opened.
In a terminal with a cwd of VSCode, package.json is echoed.
Clicking on the file name will result in vscode/package.json opening.
The directory is changed to be the template-string-converter and then package.json is echoed.
Clicking on the file name will open template-string-converter/package.json.

Set the default directory in mac terminal

I only use terminal (mac) for git, and I only use git for one directory. Is it possible to set the default directory (the directory when terminal is opened) to the directory where I use git, and if so how?
As of Mac OS X Lion 10.7, Terminal supports Resume and by default will automatically restore terminals you had open when you quit, restoring their working directories. So, you can just open a new terminal and cd to your git directory, then leave the window open when you Quit. Each time you reopen Terminal, the terminal will be there, in the same directory. (This works for bash by default. If you're using some other shell, you'll need to adapt the code in /etc/bashrc to your shell. I've posted code for zsh in my answer to Resume Zsh-Terminal (OS X Lion) on SuperUser.)
You can also arrange for Terminal to start a shell in a particular directory. You can customize or create a "Settings Profile" to issue a "cd" command when it starts:
Terminal > Preferences > Settings > [profile] > Shell > Startup > Run command
Enable "Run command" and "Run inside shell", then set the command to cd your_git_directory. When you open a new terminal with that profile, it will go to your git directory.
I recommend you Duplicate the current default profile (if you've never changed it, the default is "Basic") using the Action ("gear") menu at the bottom of the profiles list, then customize that profile.
Finally, to have it automatically open a terminal with this profile when you open Terminal, set
Terminal > Preferences > Startup > On Startup, open
to your custom profile. (On Lion, Resume will restore windows that were open when you quit, rather than perform the startup action. As I mentioned, you can just leave this terminal open when you Quit and it will be restored when you open Terminal again. Or, you can press the Option modifier key when quitting; the Quit menu item will change to "Quit and Discard Windows" and the next time you open Terminal it will perform the startup action.)
I like to have 'New windows open with: Same Working Directory'. All answers I've found for this question (many SO's) will break that setting by always going to the new home directory. Below is what I use at the top of my .profile (or .bashrc, etc).
export START="/Users/michael/my/starting/directory"
if [[ $PWD == $HOME ]]; then
cd $START
fi
This will see if you are in your HOME directory only on launch, and if so change to your new START directory. That way new windows won't automatically run this command.
The only caveat is if you're in your actual HOME directory and open a new window, it will take you to START. Which is expected.
Try echo "cd $directory" >> ~/.bash_profile
If you are using for example ZSH,
just add to your config file .zshrc this string:
# Working directory
cd ~/Desktop
or for instance:
# Working directory
cd $HOME/Desktop
Change path with your preference.
It just will return command to ZSH, you won't see it in terminal and it will start with your chosen path.
Use a Window Group. Arrange your shell window(s) as you'd want them to be on startup - cd to the directory you want in each, set colors, Shell--Edit Title, etc. Then go to Window--"Save Windows as Group...". Give it a name, check "Use window group when Terminal starts". Next time you start, this arrangement will be your starting point.
I modified #Michael Ozeryansky's answer to solve the caveat he mentioned:
The only caveat is if you're in your actual HOME directory and open a new window, it will take you to START. Which is expected.
To make your second terminal window tab starts from the Home or Any other dir:
export START="/Users/michael/my/starting/directory"
export DIR = "path/to/directory"
if [[ $PWD == $HOME ]]; then
cd $START
else
cd $HOME // or any other dir: cd $DIR
fi
If you modify the .zshrc file, the “New Terminal in Folder” Service shortcut will not work on Mac. I do not recommend.
On the iTerm2 terminal, I do the following:
Profiles --> Open Profiles --> Click the Edit Profiles button
Go to the Profiles tab --> General
Update the Working Directory