GithubHelp home page GithubHelp logo

wt-tabs's Introduction

Open a new tab in the same directory in Windows Terminal

An essential feature for me in terminals is the possibility to quickly open a new tab by pressing ctrl+t (much like in a browser) to make a new search. Differently from the browsers, where you often want to start from the home page, in terminals it is useful to start from the previously used directory. Windows Terminal allows both to open new tabs (ctrl+t) and to split pane vertically (alt+plus) and horizontally (alt+-). However, at the moment it does not offer (as far as I know) a ready-to-use feature to open a new tab (or split pane) in the same directory you were working on. By default, Windows Terminal will open the new tabs/panes in the %USERPROFILE% directory, as specified in the settings, but it is also possible to specify the startingDirectory in the settings.json file. The only problem is that it will be always the same, unless you change it every time.

Inspired by the references, I wrote some functions that allow creating a new tab (or split pane) in the last accessed directory for the following shells:

Bash, zsh

sd() {
  settings='/mnt/c/Users/<USERNAME>/AppData/Local/Packages/Microsoft.WindowsTerminal_8wekyb3d8bbwe/LocalState/settings.json'
  startDir=\"startingDirectory\"
  sed -i -E "s|$startDir: .*\"|$startDir: \"$(wslpath -m "$(pwd)")\"|g" $settings
}

where <USERNAME> has to be replaced by your account username.

Once called, it changes all occurrences of startingDirectory in your settings.json file to your current directory. Since I don't want to call it manually every time I plan to open a new tab, I overrode cd and z commands to make them call sd as follows:

cd() {
    builtin cd "$@"; sd
}
z_store() {
    z "$@"; sd
}
alias z=z_store

You can put these two functions in your .bashrc or .zshrc to use sd automatically every time you access a directory.

This "trick" works both for new tabs and split panes. An example is shown below:

test

However, there is a known problem: if you open a tab in "Home", cd to "Beautiful_directory", then open a new tab (it will open in "Beautiful_directory" as expected), then from the new tab cd back to "Home", then if you switch the first tab (which is still in "Beautiful_directory") and you open a new tab (or split pane) from there, it will open a new tab (or split pane) in "Home", because it is the last accessed directory (using cd). You can avoid this by manually calling sd before opening a new tab (or just using z to get back to your desired directory), as shown below:

bug

Command Prompt

Lastly, I wrote a batch script to achieve the same result with Command Prompt. Unfortunately, naming the batch file cd and updating the PATH accordingly does not override Windows cd function, and I did not find a way to override it, therefore I wrote the following script:

@echo off
cd %1
set settings=/c/Users/<USERNAME>/AppData/Local/Packages/Microsoft.WindowsTerminal_8wekyb3d8bbwe/LocalState/settings.json
set startDir="startingDirectory"
set lastDir="%CD:\=/%"
sed -i -E 's;%startDir%: .*^";%startDir%: %lastDir%;g' %settings%

where <USERNAME> has to be replaced by your account username.

I named the batch script cdd.bat and put its directory to the PATH environment variable. After doing so, it is possible to just call cdd to achieve the desired result. An example is shown below:

cdd

References

wt-tabs's People

Contributors

alessioquercia avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.