How to resize terminal window permanently using CLI?

25,364

Solution 1

First option

You can always run resize -s 50 50 at terminal launch using ~/.bashrc file. And to hide the command output just redirect it to the /dev/null device.

In other words, to resize the terminal using command line use:

echo "resize -s 50 50 >/dev/null" >> ~/.bashrc

Second option

After messing around with dconf-editor I believe I found exactly what you're searching for. gnome-terminal store its setting in dconf instead of config files. First you have to select the currently active terminal profile running this command:

currentprofile=$(gsettings get org.gnome.Terminal.ProfilesList default)

Then to set 50 rows as default run:

gsettings set org.gnome.Terminal.Legacy.Profile:/org/gnome/terminal/legacy/profiles:/:${currentprofile:1:-1}/ default-size-rows 50

And similarly for the columns:

gsettings set org.gnome.Terminal.Legacy.Profile:/org/gnome/terminal/legacy/profiles:/:${currentprofile:1:-1}/ default-size-columns 50

Done. Hope this helps

Solution 2

gnome-terminal

If I understand correctly, you are using standard Ubuntu and gnome-terminal, so you can use the following command line in your script,

gnome-terminal --geometry <columns>x<rows>

for example

gnome-terminal --geometry 135x45

You find the available options in man gnome-terminal.

xterm

The corresponding command line for good old xterm is

xterm -geometry 135x45

where you can add details to set the absolute position on the desktop (in pixels). Test these command lines,

xterm -geometry 90x25+0+0  # top left
xterm -geometry 90x25+0-0  # bottom left
xterm -geometry 90x25-0+0  # top right
xterm -geometry 90x25-0-0  # bottom right

and you can select other values than 0 for an arbitrary location.

You find the available options in man xterm (there are a lot of options).

Solution 3

It works for me when I unselected use custom default terminal size

enter image description here

enter image description here

Add add

resize -s 50 50 > /dev/null  

to ~/.bashrc

After pressing CtrlAltT

enter image description here

Share:
25,364

Related videos on Youtube

alhelal
Author by

alhelal

Updated on September 18, 2022

Comments

  • alhelal
    alhelal almost 2 years

    Inside the terminal when I type resize -s 50 50 terminal output is changed as rows 50 and columns 50.

    enter image description here

    But after closing the window and reopen terminal doesn't reflect the changes.

    enter image description here

    I want to open the terminal all time as rows 50 and columns 50.

    I know this can be done using terminal Edit->Profile Preference->General setting with changing the rows and columns number, but I want to do this in CLI, because I have to do this inside a big script.

    My .bashrc

    # ~/.bashrc: executed by bash(1) for non-login shells.
    # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
    # for examples
    
    # If not running interactively, don't do anything
    case $- in
        *i*) ;;
          *) return;;
    esac
    
    # don't put duplicate lines or lines starting with space in the history.
    # See bash(1) for more options
    HISTCONTROL=ignoreboth
    
    # append to the history file, don't overwrite it
    shopt -s histappend
    
    # for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
    HISTSIZE=1000
    HISTFILESIZE=2000
    
    # check the window size after each command and, if necessary,
    # update the values of LINES and COLUMNS.
    shopt -s checkwinsize
    
    # If set, the pattern "**" used in a pathname expansion context will
    # match all files and zero or more directories and subdirectories.
    #shopt -s globstar
    
    # make less more friendly for non-text input files, see lesspipe(1)
    [ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
    
    # set variable identifying the chroot you work in (used in the prompt below)
    if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
        debian_chroot=$(cat /etc/debian_chroot)
    fi
    
    # set a fancy prompt (non-color, unless we know we "want" color)
    case "$TERM" in
        xterm-color) color_prompt=yes;;
    esac
    
    # uncomment for a colored prompt, if the terminal has the capability; turned
    # off by default to not distract the user: the focus in a terminal window
    # should be on the output of commands, not on the prompt
    #force_color_prompt=yes
    
    if [ -n "$force_color_prompt" ]; then
        if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
        # We have color support; assume it's compliant with Ecma-48
        # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
        # a case would tend to support setf rather than setaf.)
        color_prompt=yes
        else
        color_prompt=
        fi
    fi
    
    if [ "$color_prompt" = yes ]; then
        PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
    else
        PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
    fi
    unset color_prompt force_color_prompt
    
    # If this is an xterm set the title to user@host:dir
    case "$TERM" in
    xterm*|rxvt*)
        PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
        ;;
    *)
        ;;
    esac
    
    # enable color support of ls and also add handy aliases
    if [ -x /usr/bin/dircolors ]; then
        test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
        alias ls='ls --color=auto'
        #alias dir='dir --color=auto'
        #alias vdir='vdir --color=auto'
    
        alias grep='grep --color=auto'
        alias fgrep='fgrep --color=auto'
        alias egrep='egrep --color=auto'
    fi
    
    # some more ls aliases
    alias ll='ls -alF'
    alias la='ls -A'
    alias l='ls -CF'
    
    # Add an "alert" alias for long running commands.  Use like so:
    #   sleep 10; alert
    alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
    
    # Alias definitions.
    # You may want to put all your additions into a separate file like
    # ~/.bash_aliases, instead of adding them here directly.
    # See /usr/share/doc/bash-doc/examples in the bash-doc package.
    
    if [ -f ~/.bash_aliases ]; then
        . ~/.bash_aliases
    fi
    
    # enable programmable completion features (you don't need to enable
    # this, if it's already enabled in /etc/bash.bashrc and /etc/profile
    # sources /etc/bash.bashrc).
    if ! shopt -oq posix; then
      if [ -f /usr/share/bash-completion/bash_completion ]; then
        . /usr/share/bash-completion/bash_completion
      elif [ -f /etc/bash_completion ]; then
        . /etc/bash_completion
      fi
    fi
    ##########################################################################################################
    #alhelal customization
    resize -s 50 50
    #resize -s 50 50 >/dev/null
    ##########################################################################################################
    

    Result after pressing CtrlAltT

    enter image description here

    The default terminal size of Ubuntu is smaller than my monitor. When I install new OS I have to change many setting(customization), installation many software. So, I am making a script that do the customization and install all stuffs without my interruption. e.g.,

    • change terminal window size
    • make shortkey
    • install firefox addons
    • install software
    • clone git repository.
  • alhelal
    alhelal about 6 years
    I put resize -s 50 50 >/dev/null in top of .bashrc but doesn't affect terminal size.
  • alhelal
    alhelal about 6 years
    it doesn't work also in last line.
  • alhelal
    alhelal about 6 years
    yes, you understand. But I want to type this command each time. When I open terminal automatically should execute this. Keep in mind that I have to done all things in CLI because I have to do this inside a bigger script.
  • sudodus
    sudodus about 6 years
    I thought that you wanted the script to open the terminal window and decide its size, and in that case you need not type all this. Simply put the whole command line in the script. -- On the other hand, if you want to do it manually, you can use an alias (interactively) or use a variable in the script to avoid typing more than necessary.
  • PerlDuck
    PerlDuck about 6 years
    I think the OP is asking for the CLI equivalent of Edit->Profile Preference->General settings. Maybe some gsettings call or something.
  • sudodus
    sudodus about 6 years
    Maybe you are right. Let us wait for the OP to tell us what they want.
  • Eskander Bejaoui
    Eskander Bejaoui about 6 years
    @alhelal I updated my answer, try the second option
  • alhelal
    alhelal about 6 years
    $ currentprofile=$(gsettings get org.gnome.Terminal.ProfilesList default) No such schema 'org.gnome.Terminal.ProfilesList'
  • alhelal
    alhelal about 6 years
    @PerlDuck Yes. I have already wrote this in my edited question.
  • alhelal
    alhelal about 6 years
    @sudodus Yes. I have already wrote this in my edited question
  • sudodus
    sudodus about 6 years
    @alhelal, I see. Well I have not used gsettings, so I suggest that someone who knows how to use it can write an answer about that. Otherwise this AskUbuntu link may help
  • sudodus
    sudodus about 6 years
    I still don't understand how you intend to use this. 1. Why are you modifying the settings of gnome-terminal via a script, if you intend to start gnome-terminal manually? 2. Is this intended for other people too, or only for yourself? 3. Would an alias solve the problem for you? I mean that the script could store the alias in ~/.bashrc. 4. Would a desktop file solve the problem for you? I mean that you can have a desktop file, that opens the size of gnome-terminal, that you want.
  • alhelal
    alhelal about 6 years
    @sudodus The default terminal size small than my monitor. When I install new OS I have to change many setting(customization), installation many software. So, I am making a script that do the customization and install all stuffs without my interruption.
  • sudodus
    sudodus about 6 years
    +1. Thanks for finding how to use gsettings and showing a solution :-)
  • sudodus
    sudodus about 6 years
    By the way, the original and simple resize -s 50 50 works for me in Ubuntu 18.04 LTS. I can't understand why it does not work for you, @alhelal. Please add it to ~/.bashrc and open a new gnome-terminal. What happens? This setting should survive shutdown and reboot too.
  • alhelal
    alhelal about 6 years
    @sudodus see ibb.co/k3PGWT
  • alhelal
    alhelal about 6 years
    @sudodus my .bashrc paste.ubuntu.com/p/YCkrY3Qdmp
  • Eskander Bejaoui
    Eskander Bejaoui about 6 years
    @alhelal did you think about using a separate /home partition? i it should keep all your customizations across OS reinstalls
  • alhelal
    alhelal about 6 years
    @sudodus It works for me also when I unselected use custom default terminal size ibb.co/bAGMWT Thank you.
  • alhelal
    alhelal about 6 years
    @SkanderBejaoui It works for me also when I unselected use custom default terminal size ibb.co/bAGMWT
  • alhelal
    alhelal about 6 years
    @sudodus Should I delete this question or add solution?
  • alhelal
    alhelal about 6 years
    @SkanderBejaoui Should I delete this question or add solution?
  • sudodus
    sudodus about 6 years
    @alhelal, You are welcome to write an answer to your own question and 'accept' it (click on the tick icon). Please describe your solution with as many details as you think are helpful for other people who want to do the same thing. -- If you find @SkanderBejaoui's answer helpful, please upvote it by clicking on the ^icon.