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

With bash, I can complete a command with TAB. Normally, it should also complete the command line switches: e.g. when I typed:
java -
it should show me the possibilities. It does not. How can I enable this preview?
See also Surprise! the shell suggests command line switches

Take a look at Extended Bash Completion

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

Related: Surprise! the shell suggests command line switches
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

Related

Linux command to DOS

I have a file include some linux command and I want to run in on windows (DOS command).
The command is:
cat tmp/$id/index.html | sed -e 's/ID/$id/g' > a;mv a tmp/$id/index.html
What is the similar command in MS-DOS?
Thank you!
The problem is that natively there is no equivalent command to sed. You have two options from my point of view. Either create a vb script that does what you want (It will not take 1 line though - more like 10-15 I guess), or use something like GnuWin32 that gives you the option to run unix commands in windows terminal.
You could consider using powershell to do approximately the same thing. It supports cat and mv and you can get a sed like equivalent by using %{_ -replace "expression", "replace"}. Details here http://blogs.msdn.com/b/zainnab/archive/2007/07/09/grep-and-sed-with-powershell.aspx
Or consider using a linux like command prompt like bash which should be available through cygwin
I think this is impossible to do in "bare" command line (as you called DOS command), because cat and sed are separate utilities. If you want to port this script from Linux command shell to windows command line, I would advise you to download and install CygWin
DOS itself does not have support for that. You could try with a port of SED for DOS available here. If you can get Powershell, that's an option. Here's an example of using grep/sed with Powershell.
There are many options.
You can try to install cygwin or download and install Git and use Git-bash or add the bin directory to your PATH so you can run this command on your CMD prompt.
There is no such command(s) for MS-DOS.

vim -u skips some initialization steps

vim -u vimrc_file lets me specify a particular vimrc file to use, however from what I read from :help initialization some initialization steps are skipped.
Is there a different option I can use to run vim using a particular vimrc file without skipping those initialization steps?
or Maybe how can I include those initialization steps that are skipped into the vimrc file?
thanks in advance
Is there a different option I can use to run vim using a particular vimrc file without skipping those initialization steps?
Documentation of my VIM 7.1 shows that those initialization steps are only to read from other standard rc files. What IMO is the whole point of the -u option: only the initialization commands from the user supplied script are read, thus they are guaranteed not to be overridden by standard initialization scripts.
Try the -S {file} option to see if it fits your needs better.

How to get Command history by cursor key in Linux tclsh

Can get the command history by using cursor key (like up arrow key) in TCL shell (tclsh).
I am running tclsh on fedora with linux version 2.6.21.
You want access to the readline library, you can do that with rlwrap:
$ rlwrap tclsh
Useful options are -c for file name completion, and -f to add words from a file to the completion list:
$ rlwrap -cf my_complete_file tclsh
Since you almost always want to use rlwrap, adding a shell alias is useful:
alias tclsh='rlwrap tclsh'
I usually use tkcon which comes with ActiveTcl, or as a separate installation. tkcon has many features, but the one I use the most is the command-line editing aspect.
Another good pure-terminal option is tclsh-wrapper
Link to tclsh-wrapper on github
It provides rich command line editing, history, aliasing, and keyword completion but does not require X11. Documentation for the key mapping is also available.

How does the 'ls' command work in Linux/Unix?

I would like to know exactly how the "Is" command works in Linux and Unix.
As far as I know, ls forks & exec to the Linux/Unix shell and then gets the output (of the current file tree. eg./home/ankit/). I need a more detailed explanation, as I am not sure about what happens after calling fork.
Could anyone please explain the functionality of the 'ls' command in detail?
ls doesn't fork. The shell forks and execs in order to run any command that isn't built in, and one of the commands it can run is ls.
ls uses opendir() and readdir() to step through all the files in the directory. If it needs more information about one of them it calls stat().
To add to the answer, in The C Programming Language book (K&RC) they have given a small example on how to go about implementing ls. They have explained the datastructures and functions used very well.
To understand what ls does, you could take a gander at the OpenSolaris source: https://hg.java.net/hg/solaris~on-src/file/tip/usr/src/cmd/ls/ls.c.
If that´s overwhelming, on Solaris you start by using truss to look at the system calls that ls makes to understand what it does. Using truss, try:
truss -afl -o ls.out /bin/ls
then look at the output in ls.out
I believe that trace is the equivalent to truss in Linux.
If you really want to understand the detailed innards of ls, look at the source code. You can follow tpgould's link to the Solaris source, or it's easy to find the source online from any Linux or BSD distribution.
I'll particularly recommend the 4.4BSD source.
As I recall, ls starts by parsing its many options, then starts with the files or directories listed on the command line (default is "."). Subdirectories are handled by recursion into the directory list routine. There's no fork() or exec() that I recall.
This is a old thread , but still I am commenting because I believe the answer which was upvoted and accepted is partially incorrect. #Mark says that ls is built into shell so shell doesn't exec and fork. When I studied the tldp document on bash(I have attached the link)
"ls" is not listed as a build in command.
http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_01_03.html
Bash built-in commands:
alias, bind, builtin, command, declare, echo, enable, help, let, local, logout, printf, read, shopt, type, typeset, ulimit and unalias.

Surprise! the shell suggests command line switches

I noticed that the bash shell can suggest command line switches for your command.
Just type your command, a dash (-) and type tab. The shell will show you available switches.
For example, try:
andy#andyx:~$ java -
and press tab - Surprise!
The question is:
How do I interface with this feature. I.e., if I write a program that is to be run from the console, what's the API to tell the shell what switches are available?
You have discovered Bash's programmable completion feature.
See:
$ man bash
In particular, the section entitled "READLINE" and subsection "Programmable Completion"
FYI: In Ubuntu and/or Debian the scripts are at /etc/bash_completion.d/

Resources