Skip to content

Instantly share code, notes, and snippets.

@DeveloperChris
Last active March 13, 2024 01:06
Show Gist options
  • Save DeveloperChris/83580a0e29d3dd2016a3764e9a13d5c1 to your computer and use it in GitHub Desktop.
Save DeveloperChris/83580a0e29d3dd2016a3764e9a13d5c1 to your computer and use it in GitHub Desktop.
Creating per project/Folder history files in Visual Studio Code - VSCODE

Because I work on multiple projects in different languages I am constantly flicking back and forth between vscode sessions and using terminals to run commands

This can be annoying after restarts because you either lose your history and cant find what you want or they get all mushed together so you get php and perl and whatever else you are working on in your bash? history

for example I am bulding a laravel app and am constantly calling artisan ... something command and often scroll up the history to get the exact command I previously used. but after restarts I can lose that and have a whole bunch of perl commands from another terminal session.

To improve this situation I have added a configuration to the settings json to run on the remote session each time the terminal is started up This comes courtesy of this stackoverflow question

https://stackoverflow.com/questions/45635168/vscode-how-to-run-a-command-after-each-terminal-open

Create a file called .terminal_startup.sh in your users root folder (e.g. ~/.terminal_startup.sh). This is the folder terminal starts in (unless you have configured otherwise)

in that file write the following

CWD=$(pwd)

#run your standard bashrc to get any commands in that
if [ -f ~/.bashrc ]; then
    source ~/.bashrc
fi

#touch the history file to make sure it exists
touch $CWD/.vscode_history
#change the history file to be a local file
export HISTFILE=$CWD/.vscode_history

#force the history to be written immediately a command is run
export PROMPT_COMMAND="history -a; $PROMPT_COMMAND"

Add other commands you may want to run each time a bash terminal is created (use the same template if you prefer other shells)

open your user settings.json file and add the following

"terminal.integrated.profiles.linux": {
        "BashWithStartup": {
          "path": "bash",
          "args": [
            "--init-file",
            "~/.terminal_startup.sh"
          ]
        }
      },
    "terminal.integrated.defaultProfile.linux": "BashWithStartup",
    "terminal.integrated.automationProfile.windows": {},
    "files.exclude": {
        ".vscode_history": true
    }

save all files

when a new terminal is started ~/.terminal_startup.sh is run in the context of your working folder, it creates a history file and switches to it.

It also forces immediate writes to the history file enabling other terminals in the same folder to get access to the same commands

I added the files.exclude to ensure I wasnt cluttering my folder

Now when you open a new terminal it will change to the local history file ".vscode_history" and keep commands in that, thus not cluttering other sessions with these commands

Your normal history file will continue to be used if you ssh into your machine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment