12

I'm running Awesome WM on the latest Arch Linux with Gnome Terminal and the default bash. I've managed to get this working, but I wonder if any form of improvement is possible with this implementation. The question does NOT relate to opening new tabs, only to spawning new terminal windows with Awesome WM.

I have rewritten the "cd" command to save the current working directory in the ".cd_extend" file:

~/.bashrc

alias cd='source ~/.cd_extend'

~/.cd_extend

#!/bin/bash

command cd $1
echo $(pwd) > ~/.terminal_directory

When I spawn a new terminal, the ".terminal_directory" is read and appended as an argument to gnome terminal's "--working-directory" flag.

~/.dotfiles/open_terminal.sh

#!/bin/bash

DIR=$(cat ~/.terminal_directory)
gnome-terminal --working-directory=$DIR

awesomewm rc.lua

terminal   = "~/.dotfiles/open_terminal.sh"
awful.key({ modkey, }, "Return", function () awful.util.spawn(terminal) end)

I wonder if I have missed any internal bash functionality which could have simplified this and if there is room for improvement.

user1213904
  • 240
  • 1
  • 2
  • 8

5 Answers5

9

On Arch Linux + GNOME (at least), you can add the following line into ~/.bashrc to have new Terminal windows opened in the current directory:

source /etc/profile.d/vte.sh

There is already a related question about new tabs. It turns out that the answer is the same for new windows.

  • 2
    It’s not clear how this answers the question.  What does this do?  Please do not respond in comments; [edit] your answer to make it clearer and more complete (i.e., to explain how this answers the question). – G-Man Says 'Reinstate Monica' Apr 08 '19 at 18:02
  • It precisely answers the asked question. I have nothing to add, except that I just thank you for your downvote. – Thomas Hugel Apr 09 '19 at 06:11
  • It does not answer the question as it is related to tabs, and not new windows spawned by Awesome WM as noted in my question. Therefore you're answer should elaborate more as to why sourcing vte.sh solves retaining directory state between Awesome WM spawns. – user1213904 Apr 23 '19 at 13:35
  • Apparently gnome-terminal changed its implementation so this no longer happens by default. The shell is responsible for handling this. /etc/profile.d/vte.sh sets $PROMPT_COMMAND to do a printf to handle this. If you set $PROMPT_COMMAND, then you must ensure that it calls the function in vte.sh. See this ticket for more – jpyams Mar 17 '20 at 19:26
5

In the menu of Gnome-terminal, use:

File --> Open Terminal 

That will open a new window using the pwd as the directory.

Also, you may set the open tabs:

Edit --> Preferences --> General --> Open new terminals in: --> select tab.

So new terminals will open in the same window with the same pwd.
You will still be able to open new windows if needed:

Alt-F2 --> gnome-terminal

  • Thanks for the answer, however this breaks the flow with awesome, because awesome should be spawning the terminals, otherwise I'm required to bind my keys for a new terminal through gnome-terminal, which is not desired. Also using Alt-F2 is a bit hard as i'm missing the Fn buttons. – user1213904 Dec 02 '16 at 12:38
  • 1
    This option seems to be missing in gnome-terminal 3.22 :/ – hugomg Apr 13 '17 at 04:55
  • On the Gnome-terminal 3.22.2 I have it is still valid, shrug :/. –  Apr 14 '17 at 00:02
  • The simple solution is, just add this line to the ~/.bashrc. source /etc/profile.d/vte.sh – Irshad Jun 30 '21 at 05:04
4

I am using i3 and if I do the normal Meta+Enter then it opens a new terminal in the home directory. But, if I am already in a terminal and I press Shift+Ctrl+N then a new terminal opens in the last terminal's directory.

Shift+Ctrl+N is the shortcut for opening a new window on the terminal (my terminal is gnome-terminal).

I understand this question is regarding AwesomeWM and the terminal other's use may not have this key binding but sharing just in case it may help someone.

AdminBee
  • 22,803
EJC
  • 41
2

After a few days of testing and asking around, this seems to be the easiest implementation. It could be done with a global state which Awesome WM reads, however to keep predictability and ease of implementation I'm keeping this.

~/.bashrc

function cd {
  command cd "$1"
  pwd > /tmp/terminal_pwd
}

Afterwards I can open the terminal, which reads from the /tmp/terminal_pwd file and opens a new gnome shell with the given working directory.

Awesome WM - rc.lua

 -- Terminal
  awful.key({ modkey, }, 'Return',
    function()
      awful.spawn.easy_async_with_shell(
        "if [[ -s /tmp/terminal_pwd ]]; then cat /tmp/terminal_pwd; else echo '~'; fi",
        function(path)
          awful.spawn.easy_async_with_shell('gnome-terminal --working-directory '..path, function() end)
        end)
    end),
user1213904
  • 240
  • 1
  • 2
  • 8
  • (1) Since you’re running the ~/.cd_extend file with the source command, you don’t need a “shebang” (#!/bin/bash) in it. (It is harmless; it will be ignored.)  (2) The gnome-terminal command should say --working-directory="$DIR" (with quotes). – G-Man Says 'Reinstate Monica' Apr 08 '19 at 18:12
  • thanks @G-Man, I have updated my answer accordingly. – user1213904 Apr 23 '19 at 13:43
  • with command cd "$1" with double quotes affect the basic behavior - cd without arguments not move you to the home directory. without quotes works as expected. – Jan Galtowski Oct 29 '22 at 14:10
1

My solution would be to run command from terminal:

gnome-terminal $(pwd)

That works great under i3wm for me, so you can alias this (in ~/.bashrc or ~/.bash_profile) to anything you desire, something like:

alias new='gnome-terminal $(pwd)'

Haven't tested under Awesome WM, but shall do the same thing I believe, very busy working on the project, so can't relly test it now. Will do in future for sure

  • Unfortunately this wouldn't work for this question. Because i'm spawning a new terminal window from an active Awesome WM session, This only works if you run it from the terminal. – user1213904 May 27 '20 at 20:33