Close current tab firefox using terminal

17,536

Solution 1

To keep Firefox minimized after closing its current tab (according to OP, see the comment) use the following commands in a terminal,

wmctrl -a firefox; xdotool key Ctrl+w; wmctrl -r firefox -b add,shaded

How it works

wmctrl -a firefox

Go to the window with a name containing 'firefox' in it

xdotool key Ctrl+w

Send the keystroke Ctrl+W to firefox which is the shortcut to close current tab in firefox.

wmctrl -r firefox -b add,shaded

Shade a window with a title that contains the word 'firefox' in it.

To know more about these see man xdotool and man wmctrl

Alias for above command

You can make an alias for the above commands, say the alias name be cfct ( Close Firefox Current Tab) in ~/.bashrc or better in ~/.bash_aliases from a terminal as,

echo -e "\nalias cfct='wmctrl -a firefox; xdotool key Ctrl+w; wmctrl -r firefox -b add,shaded'" >> ~/.bash_aliases
. ~/.bash_aliases

If you don't have ~/.bash_aliases replace ~/.bash_aliases in the above commands with ~/.bashrc

Usage

open a terminal and enter

cfct

And it will close Firefox’s current tab instantaneously.

Note: You need to have wmctrl and xdotool installed in your system. To install them use,

sudo apt-get install wmctrl xdotool

Solution 2

You can install this Add-on: https://github.com/bard/mozrepl/wiki and enable it with Tools->MozRepl->Start. After that you can script Firefox from outside. Create following test.js file:

window.getBrowser().removeCurrentTab()
repl.quit()

And run it following way:

nc localhost 4242 < test.js
Share:
17,536

Related videos on Youtube

shivshnkr
Author by

shivshnkr

Updated on September 18, 2022

Comments

  • shivshnkr
    shivshnkr almost 2 years

    I have firefox already running on my system, and now in minimised state. What I want is to close the current tab running in firefox. How can I do this by simply using commands in terminal?

  • shivshnkr
    shivshnkr about 11 years
    firefox is minimised, i have already stated.
  • André Stannek
    André Stannek about 11 years
    Also: xkill would kill the whole Firefox and not only one tab.
  • shivshnkr
    shivshnkr over 10 years
    the browser window gets opened, which is not supposed to. how to avoid this?
  • kiri
    kiri over 10 years
    Quite unreliable in that it sends Ctrl+W to the last opened window, regardless if it is Firefox or not.
  • sourav c.
    sourav c. over 10 years
    @minerz029 You are correct. check my edits.
  • m3nda
    m3nda about 9 years
    Can i choose another tab than the current?
  • mchid
    mchid over 4 years
    @m3nda You can switch tabs before you close the tab. You can switch to the first tab using Alt+1 or use any other number to switch to a different 𝑛th tab. To close the 2nd tab for example: wmctrl -a firefox; xdotool key Alt+2; xdotool key Ctrl+w; wmctrl -r firefox -b add,shaded