Archives For command skill

The ‘Tab’ key bash auto-completion does not work for you? Here’s how to re-enable this feature in Ubuntu.

When typing in terminal/command console, user can press ‘Tab’ key to auto-complete command, options, file path & name. This feature is quite useful and available out-of-the-box for every user with home directory.

If you broke something done and the feature does no longer work, then this tutorial could help.

Step 1: Install bash-completion package

For Debian, Ubuntu, and most other Linux, this feature is implemented by bash-completion package files. So the first step is to make sure that package is installed on your system.

To do so, open terminal/command console and run command:

sudo apt install bash-completion

Step 2: Enable Bash Completion

For each user, there’s a .bashrc file in user home directory. The file runs automatically on every login, and it contains the rule to enable bash completion.

1. First, run command to edit the file:

nano ~/.bashrc

2. When the file opens, add the following lines or make sure the lines below exist:

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

To save file, press Ctrl+X, type y and hit Enter.

You can alternatively edit the system wide config file:

sudo nano /etc/bash.bashrc

Then enable the corresponding lines (remove # at the beginning for each line), so it applies to all users at login.

After saving the change, it should work immediately in new terminal window or command console.

In addition

The default configuration files for bash completion is located in “/usr/share/bash-completion”. However, some Linux Distro or may be 3rd party program may add rules by creating files under “/etc/bash_completion.d/”.

So, if the feature does not work for a specific command, go check the config file under “/etc/bash_completion.d/”.

Familiar with Linux commands? You may already know many tips and tricks that will save you a lot of time.

Here are some that I have been using for a long period of time, including Tab auto-completion, when you type something in Linux terminal and hit Tab, it auto-completes the command or file path, or outputs all possible options.

And Ctrl+Shift+V will do paste text to terminal instead of Ctrl+V. Without copying to the clipboard, you can simply select / highlight the text and use mouse middle-click to paste into terminal.

Today I found a new terminal tips that I didn’t know before. Thanks to the reddit, I’m going to share it with you by following steps.

1. Drag and drop file or folder to paste the path into terminal.

Just drag and drop a file or folder into terminal, and you’ll see the full path pasted with single quotes. It’s useful if there’s special character in file path or you just don’t want to type …

2. Drag and drop a selection of text into terminal.

You can also drag and drop a selection of text into terminal, so you don’t need to do copy and paste with context menu clicks or keyboard shortcuts.

This is a simple command line skill for beginners how to use history commands quickly and easily.

For frequently used command with a long code, you don’t need to type all the code everytime you run this command. Just type in !n, where n is a number from your history list.

For example, following is a section of my history commands:

1262 sudo apt-key adv –keyserver pgp.mit.edu –recv-keys 5044912E
1263 sudo sh -c ‘echo “deb http://linux.dropbox.com/ubuntu/ raring main” >> /etc/apt/sources.list.d/dropbox.list’
1264 sudo apt-get update
1265 sudo apt-get install dropbox

Before cleaning the history, I can run “sudo apt-get update” command by type in:

!1264

For more:

  • !! : run the previous command.
  • !n:p : only preview command number n (will not execute the command).
  • !string : Run most recent command starting with characters in string.