Tab in command line linux

How to enter a tab char on command line?

In an interactive bash terminal how do I enter a tab character? For example, if I wanted to use sed to replace «_» with tabs I’d like to use:

Where TAB means the tab key. This works in a shell script not interactively where when I hit the tab key I get no character and a clank noise sounds. I’ve also tried \t but it only places t’s in the string and not tabs. Note this is mac osx.

2 Answers 2

Precede it with Control + V , followed by Tab to suppress the usual expansion behavior.

I’m getting strange results. Entering Control + V one time does nothing. Doing it a 2nd times gives me a ^V where I wanted a tab and replaces the «_» chars with nothing.

Since this question is tagged «bash». using the «ANSI-C quoting» feature of the Bash shell is probably preferable. It expands ANSI C-style escape sequences such as \t and \n and returns the result as a single-quoted string.

This does not rely on your terminal understanding the Control + V (insert next character literally) key binding—some may not. Also, because all the «invisible» characters can be represented literally, your solution can be copy-pasted without loss of information, and will be much more obvious/durable if you’re using including this sed invocation in a script that other people might end up reading.

Also note that macOS’s version of sed is the BSD version. GNU sed will interpret character escapes like \t in the replacement pattern just fine, and you wouldn’t even need above workaround.

You can install GNU sed with MacPorts, and it should be made available as gsed , but Homebrew might supersede your system’s sed depending on how you have your $PATH variable arranged.

Источник

Use tab to autocomplete commands in the command line

autocomplete dos tip

Computer Hope

When at the MS-DOS, Windows command line or a Linux or Unix shell, typing in long file names or directories can become a burden. Use Tab to autocomplete the names of directories and files while in the command line. Below are a few examples of how this can be done.

Читайте также:  Linux lite based on

MS-DOS and Windows command line users

While at the prompt, typing in one or more letters a file or directory name contains will autocomplete the name in alphabetical order. For example, when at the C:\> prompt, type the command below.

After the command above is typed, instead of pressing Enter , press Tab . When you press Tab , the first directory that begins with «p» will be automatically typed into the command. Continuing to press Tab cycles through all available directories, including the «Program Files» directory. This shortcut can be used any time you need to type a file name or directory in a command.

If you type one or more beginning letters for a file name or directory that does not exist in the current directory, the Tab key will not autocomplete.

Most Linux and Unix users

Depending on your variant of Linux or Unix and if you’re remotely connecting to a Linux box changes how the Tab key autocompletes. However, below is a general run down on how this is commonly used in Linux. At the prompt in your home directory, type the command below.

Assuming your home directory has «public_ftp,» «public_html,» or another file or directory that begins with «p,» you are shown all available files and directories. If you continue to type the next letter available, for example, «cd pu,» completes the remaining of the file name or directory up to the point there’s no conflict. If you had both directories mentioned earlier in the current directory, it would autocomplete up to «public_» because there are two directories beginning with «public_». Pressing Tab again shows you the remaining available files beginning with «public_». If you wanted to type «public_html,» add a «h» and press Tab to complete the directory name.

Источник

How to enable tab-completion of command line switches in bash?

With bash , I can complete a command with TAB. Normally, it should also complete the command line switches: e.g. when I typed:

it should show me the possibilities. It does not. How can I enable this preview? See also Surprise! the shell suggests command line switches

How do I add functionality to my command line scripts, so they too can offer auto completion for commands? (I have googled, but only get stuff about how to use it, and completion via history file)

Читайте также:  Linux пакеты для ноутбука

5 Answers 5

You need to have bash_completion installed and then just add . /etc/bash_completion to your .bashrc .

Don’t know if the name changed but I needed to install bash-completion (with the dash), see also askubuntu.com/questions/86375/…

In the answers to that question there were several links to documentation. You might find what you look for there.

Depending on what Linux flavor you’re using, you may want to add a package. For Fedora and related distributions, you need to add the separate package bash-completion to get this to work. I wouldn’t be surprised if other distributions had this packaged as an optional 2nd package that you need to add in addition to the bash package.

If you want to create your own custom completions you can look at this post: https://stackoverflow.com/a/21476506/2649637

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.17.43537

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

What is the proper way to insert tab in sed?

What is the proper way to insert tab in sed? I’m inserting a header line into a stream using sed. I could probably do a replacement of some character afterward to put in tab using regular expression, but is there a better way to do it? For example, let’s say I have:

some_command | sed '1itextTABtext' 

I have tried substituting TAB in the command above with «\t», «\x09″, » » (tab itself). I have tried it with and without double quotes and I can’t get sed to insert tab in between the text. I am trying to do this in SLES 9.

Try using column (linux.about.com/library/cmd/blcmdl1_column.htm) for this — it is a tool specialized toward columnizing data.

8 Answers 8

Assuming bash (and maybe other shells will work too):

some_command | sed $'1itext\ttext' 

Bash will process escapes, such as \t , inside $’ ‘ before passing it as an arg to sed.

I like this solution, as it can be applied to many other instances where tab might be needed in a parameter.

You can simply use the sed i command correctly:

some_command | sed '1i\ text text2' 

where, as I hope it is obvious, there is a tab between ‘text’ and ‘text2’. On MacOS X (10.7.2), and therefore probably on other BSD-based platforms, I was able to use:

some_command | sed '1i\ text\ttext2' 

and sed translated the \t into a tab.

If sed won’t interpret \t and inserting tabs at the command line is a problem, create a shell script with an editor and run that script.

Читайте также:  Install application linux terminal

Your solution works along with @AaronMcDaid ‘s, so I’m curious why it also somewhat works when «\» is omitted? Is newline required after «\»?

It depends on the version of sed . Classically (meaning anything not using GNU sed ) you have to use the backslash newline after the i command to specify the lines to be inserted. The last line without a backslash at the end marks the end of the inserted text. GNU sed does support, as an extension, the ability to specify the first line of inserted text immediately after the i ; MacOS X sed does not. It depends on your portability criteria: if working with only GNU sed is OK, then the one line variant is OK; if you need to work on HP-UX, Solaris, AIX, MacOS X as well then be careful!

So is «\» optional in some variants or is it still an incorrect command even though it works for inserting text without tab character in it?

The ‘sed -e ‘/^/itext\ttext2’ notation seems to work with GNU sed and embeds a tab. So did the version with an actual tab in place of the \t escape sequence.

Another option is to prepare your scripts with printf. For example . scr=»`printf ‘1i\\\ntext\ttext2\n’`» . so that you can run some_command | sed «$scr»

Источник

How to be fast in the Linux command line

The Linux command-line should not be tedious. Thankfully, Linux ships with many GNU utilities to make the command-line easy. These tips will help you be fast on the Linux command line. Mastering the command line will make you fast, efficient, and love Linux even more.

Latest Posts

Scan Your Docker Images and Containers with VirusTotal: A Step-by-Step Guide

Bitbucket for Newbies: Mastering Basic Commands and Collaborating on Code

Accelerate Your Performance Testing on Ubuntu with k6 and Postman-to-k6

Solve the “Cannot read properties of undefined (reading ‘type’)” error with these simple fixes

December 19, 2022 May 1, 2023

Solving the ‘tail: inotify resources exhausted’ Error on Ubuntu

December 18, 2022 May 1, 2023

Copyright 2018-2022 Anto Online.

All rights reserved. Please consider the information, scripts and instructions carefully before using it yourself. Make sure you have ample backups! The information, scripts and instructions are provided without warranty. Consult a professional if you are unsure. Anto does not speak on behalf of any company and our opinions are our own.

The feature images have been provided by pexels.com and unsplash.com.

Источник

Оцените статью
Adblock
detector