I'd like to write structured command in multiple lines as

$ for i in *;
→ do
→    file $i;
→ done
bmdt.md: ASCII text
breakfast.md: ASCII text
brmdh.md: ASCII text
test.sh: Bourne-Again shell script text executable, ASCII text

but find the tab not working,

How could I activate the tab key in the bash console?

  • 4
    press Control-V Tab instead; Tab is bound to file/command completion by default. – mosvy Oct 28 '18 at 9:53

When you press Enter after entering an incomplete command, the bash shell will show a continuation prompt (the secondary prompt, $PS2) and allow you to complete the entered command. The command line editing facilities of the shell at that point is the same as at the original prompt (the primary prompt, $PS1), which means that the Tab key would do command and filename completion as usual.

To insert a tab character, use Ctrl+V Tab, or use spaces for indentation. Tab characters (or any other literal character) can always be inserted if prefixed by Ctrl+V.

Alternatively, if in Emacs command line editing mode (which I believe is the default in bash), use Ctrl+X Ctrl+E to bring up an editor to enter your command into. The editor used will be the one specified by the EDITOR shell/environment variable, or emacs if this variable is unset.

In Vi command line editing mode, press Escape v to do the same thing.

The command will be executed after saving and exiting the editor.

  • Could I ask What's vi command line, I am aware the Vim is a editor which should be opened in advance, go inside, and edit, rather than directly type into command and run. – Calculus Oct 28 '18 at 10:12
  • 1
    @avirate Vim is a variant of the original Vi editor. "Vi command line editing mode" is a mode that you enable with set -o vi and that gives you the ability to use Vi/Vim editing commands on the command line, like (Esc) dw to delete to the end of the current word etc. Use set -o emacs to get back to Emacs command line editing mode. – Kusalananda Oct 28 '18 at 10:15
  • amazing,I immediately search and explore it. – Calculus Oct 28 '18 at 10:16
  • 1
    @avirate Note that the editor doesn't need to be opened in advance. If you press Ctrl+X Ctrl+E, the shell will open it for you. – Kusalananda Oct 28 '18 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.