38

I have installed MinGW-w64 and MSYS2. But how do I change the HOME directory in MSYS2? So that when I type cd $home or cd ~ it goes to another directory that I defined.

And how do I write a code so that the starting directory is always where the .bat file is placed on?

In cmd I used this code:

%~d1 cd "%~p1" call cmd

so when I open cmd on my desktop, it starts from the directory on desktop.

How can I do a similar thing with msys2?

2

11 Answers 11

67

If you would like to use your windows home folder as the home folder for MSYS2, you can edit /etc/nsswitch.conf and write:

db_home: windows 
Sign up to request clarification or add additional context in comments.

7 Comments

I couldn't get this to work for me for some reason. Is there something else I need to do?
/etc/nsswitch.conf not /etc/nssswitch.conf
This worked for me. I just had to fix the spelling mistake in the answer. I'll make an edit but need to change at least 6 characters. So there may be 5 other unrelated character changes for me to fix this answer.
didn't work for me, but this did db_home: env windows /C/your-dot-files as suggested here [ conemu.github.io/en/CygwinHome.html]
@JoelSantosRico your link has the closing "]" included in the link. Here is the working link: conemu.github.io/en/CygwinHome.html
|
7

Msys2 will use windows %HOME% as it's $HOME dir. If you set %HOME% in environment variables (to the windows directory you need Msys2 to use) it will work.

1 Comment

the problem is that git use the same environment variable for storing ssh keys as msys2. how to change that %HOME% env variable to something else ? I already started a new thread on here
5

I prefer to just update /etc/fstab @ fstab.

# cat /etc/fstab # For a description of the file format, see the Users Guide # https://cygwin.com/cygwin-ug-net/using.html#mount-table # DO NOT REMOVE NEXT LINE. It remove cygdrive prefix from path none / cygdrive binary,posix=0,noacl,user 0 0 ################################################################## # Canonicalize the two home directories by mounting the windows # # user home with the same path mapping as unix. # ################################################################## c:/Users /home ntfs binary,posix=0,noacl,user 0 0 

3 Comments

I like this idea as it's a more pure Unix way. But the syntax on the last line should actually be C:/Users /home ntfs binary,posix=0,noacl,user 0 0. BTW I prefer to mount the entire C:\Users to /home, as it's easier to comply.
Yup, johann, you're right, I confirmed this now, I edited the response
Updated for multi-user compatibility.
4

Besides the above answers, there is another way with the Windows command mklink to make a directory symbol link, that similar to ln in Linux.

First, make a directory of e:\msys\home, then run cmd as Administrator, and execute:

> cd c:\msys64 > mklink /j home e:\msys\home 

no other change required.

Occasionally, after specific base updates, the directory link becomes invalid, and a new 'home' directory is made under the raw installation directory. That requires to rename or remove the new generated 'home' directory, and run the mklink command again.

Comments

4

Note that Git Bash is MSYS2-based. So, see also my other answer here: Change the location of the ~ directory in a Windows install of Git Bash.

how do I change the HOME directory in MSYS2?

Quick summary

Open C:\msys64\home\my_username\.bash_profile and add this to the top:

# Change your home (`~`) dir to `C:\Users\my_username` HOME="/c/Users/$(whoami)" # OPTIONAL: change your start directory too: # Start in HOME cd "$HOME" # OR cd "$HOME/path/to/some/start/dir" # etc. 

Details

I really prefer to use the seven MSYS2 terminals inside Windows Terminal, since it is a great terminal shell and allows modern features like multiple tabs.

From my MSYS2 setup answer here: Installing & setting up MSYS2 from scratch, including adding all 7 profiles to Windows Terminal:

1. Set the starting directory to your HOME directory

First, here is how to set the starting directory to your HOME directory so that a terminal starts there when you open it up:

Open Windows Terminal.

Then, click the little drop-down arrow at the top-right of the open tab, and select "Settings" --> in the Settings tab that opens up, click "Open JSON file" in the bottom-left, as shown here:

enter image description here

In the JSON settings file that opens up, look for the "profiles" section in the JSON file

Then, add an MSYS2 profile like this, setting the "startingDirectory" value like this. Note that this is just one of seven of the MSYS2 profiles. See my full answer above for all 7 profiles:

 // -ucrt64 (recommended default) { "commandline": "C:/msys64/msys2_shell.cmd -defterm -here -no-start -ucrt64 -shell bash", "guid": "{a718a3d5-9e77-4d0d-b7b6-69ec3d190206}", "hidden": false, "name": "MSYS2: ucrt64 (recommended default)", // "startingDirectory": "C:/msys64/home/%USERNAME%", // alternative "startingDirectory": "%USERPROFILE%", // ie: C:\Users\my_username "icon": "C:/msys64/ucrt64.ico", "font": { "size": 10 } }, 

2. Set your HOME directory to whatever you want

Next, here is how I set my HOME (~) dir to whatever I want in the seven MSYS2 terminals:

Again, from my MSYS2 setup answer here: Installing & setting up MSYS2 from scratch, including adding all 7 profiles to Windows Terminal:

  1. Change your HOME (~) dir from MSYS2's default of C:\msys64\home\my_username to your regular Windows home dir of C:\Users\my_username:

    Open up C:\msys64\home\my_username\.bash_profile in a text editor, such as VSCode. Modify it so that it looks like this. Note: you can just comment everything out with # and add this to the top:

    # Change your home (`~`) dir to `C:\Users\my_username` HOME="/c/Users/$(whoami)" # Source your `C:\Users\my_username\.profile` file, if it exists if [ -f "$HOME/.profile" ]; then . "$HOME/.profile" fi 

    Close and re-open Windows Terminal. Run echo ~ and echo "$HOME" and ensure they now both show /c/Users/my_username. Run ls -a and ensure you see the contents of your normal Windows home directory for your user. pwd should show that you are currently in that directory too...

Comments

3

In one of your shell startup scripts (e.g. ~/.bash_profile where ~ is the default/original home directory) you can change the $HOME environment variable:

export HOME=/something/else 

If you want your shell to open in that directory you might need to run cd (with no arguments) after setting $HOME; I have not tested it.

Comments

2

Create or modify an MSYS2 /etc/passwd file. Two ways of doing this are shown below.

The following command can be run from an MSYS2 shell, and works safely whether or not the file exists and whether or not it already contains the current user:

$ grep "^${USERNAME}:" /etc/passwd >/dev/null 2>&1 || mkpasswd | grep "^${USERNAME}:" >>/etc/passwd 

Next, edit /etc/passwd, and change the relevant user's home directory field (the 6th colon-delimited field).

$ vim /etc/passwd 

BONUS: It is also possible to change the MSYS2 username by editing the first field.

As desired, move current home directory content to the new home directory.

Log off, then log back in.

The /etc/passwd edits can be done without manual editing, but this makes for a more complex command-line to paste into the MSYS2 shell, and, it might not work if the /etc/passwd file already exists and has the username in it already:

__DIR="/path/to/home" mkpasswd | grep "^${USERNAME}:" | \ awk -v DIR="${__DIR}" -v RS=":" -v ORS="/n" \ 'NR == 6 { printf(DIR ":"); next } { printf("%s", $0) } NR < 7 { printf(":") }' - >>/etc/passwd 

1 Comment

this does not work when you login to windows using LDAP
2

Adding HOME='E:/Users/your_directory' in /etc/profile. It looks as follows:

... # Setup some default paths. Note that this order will allow user installed # software to override 'system' software. # Modifying these default path settings can be done in different ways. # To learn more about startup files, refer to your shell's man page. MSYS2_PATH="/usr/local/bin:/usr/bin:/bin" MANPATH='/usr/local/man:/usr/share/man:/usr/man:/share/man' INFOPATH='/usr/local/info:/usr/share/info:/usr/info:/share/info' HOME='e:/Users/HP' case "${MSYS2_PATH_TYPE:-minimal}" in ... 

but then in fresh start, your shell would shows the full path, instead of just ~.

E:/Users/<your_username> $ 

instead of

~ $ 

1 Comment

This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review
0

Also using /etc/fstab but in another way:

# this line is generated by cygwin/msys2. none YOUR_CYGDRIVE_MOUNT_PATH cygdrive binary,posix=0,noacl,user 0 0 # add this line: YOUR_CYGDRIVE_MOUNT_PATH/c/Users/USER_NAME /home/USER_NAME none bind 

YOUR_CYGDRIVE_MOUNT_PATH = The path where your Windows drives are mounted into your cygwin/msys2 environment.

USER_NAME = Your username. Usually in msys2 you have the same username as your Windows username.

Explanation:

Cygwin/msys2 will mount your Windows drives to YOUR_CYGDRIVE_MOUNT_PATH (for msys2, the default value is /).

Instead of creating another mount directly from your NTFS filesystem (from @cornnutz 's answer), we create a bind from a subpath to our Windows user directory YOUR_CYGDRIVE_MOUNT_PATH/c/Users/USER_NAME to /home/USER_NAME.

Try it out without editing /etc/fstab:

You can achieve the same result from command line by executing:

mount -o bind YOUR_CYGDRIVE_MOUNT_PATH/c/Users/USER_NAME/ /home/USER_NAME 

Comments

0

I know this is an old post, however, I just got a new Windows 11 laptop, and had to go over it again. So here is what I've been using to let MSYS2 use my Windows USERPROFILE directory (C:\Users\USERNAME) as their HOME (/home/USERNAME) directory.

db_home: /c/Users/%U 

It is based on the ConEmu post mentioned in the comments to dr ganjoo top rated accepted answer.

I use the following sed oneliner to make the change.

sed -i.bak 's!^\(db_home:\)\(.*\)$!# \1\2\n\1 /c/User/%U!' '/etc/nsswitch.conf' 

Diff:

- db_home: cygwin desc + # db_home: cygwin desc + db_home: /c/Users/%U 

Comments

-1

I've created a batch file which sets the HOME variable:

set HOME=C:\Users\%USERNAME% C:\LocalApp\PortableGit\git-bash.exe 

This allows me to put my .bash_profile in this HOME (rather than on the default network location which performs very slowly).

4 Comments

git-bash and MSYS2 are different tools
@Arkanosis, sort of. Actually, Git Bash is based on the MSYS2 MINGW64 environment. See here: msys2.org/docs/who-is-using-msys2: "Git for Windows is based on MSYS2".
But, it's true, Git Bash does not replace any of the seven MSYS2 environments, and none of those environments replace Git Bash. See here: msys2.org/docs/git and my answer here.
That correct, but OP wrote they installed “MinGW-w64 and MSYS2”; a solution for git-bash won't help, unfortunately. It doesn't mean the solution is not useful for git-bash users that happen to use MSYS2 unknowingly, obviously.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.