new empty line in cmd - cmd

When I open cmd, the default value is::
C:>Users\otariki>
When I press ENTER on keyboard, one new line is written
C:>Users\otariki>
In new line, I do not need the C:>Users\otariki> string. I need an empty line. Please tell me how to do this?

This is the "PROMPT" variable, you cant set it to nothing when its nothing it defaults to the one you had.. nearest you can have is a space.. but.. prompts are useful, indicates the command finished and isnt just waiting, as well as normally tells you where you are.

You can use PROMPT $H to get the appearance of what you want.
Each line will be empty, starting in the 1st position.
But in reality each line will start with <backspace><space><backspace>

You can type
echo off
into the CMD window, so it will not prompt you at all but you can still type in commands.

Related

Jump to string in current line while using Bash in VI mode

While navigating in BASH using VI mode, I can jump back to a specific character (e.g. '-') of the current command line via the following command:
F-
How can I jump back to a specific string (e.g. '--path') in the current command line of BASH? I know navigating in VI but I did not understand how to perform regex search in current command line of BASH.
According to here, what you want doesn't seem possible. The ?word and /word bindings search in command history rather than in the current command line.
But in vi mode, you can press ESC v to open the current command line in an editor. Then you can edit & save the command and it will be executed (source).
Of course, as pointed out in nur-sh's answer, you can simply keep pressing B to get to the word.
You could use the find command which searches backwards from where you are
?word
or you could keep pressing B to get to the word
(this command goes back one Word at a time).

How can I add a vertical space in 'Terminal' after each command?

I've just started using Terminal (the CLI for Mac OS X).
When I run a command, get some information back, run another command, get more info etc., it is hard (on the eyes) to find a certain point on the screen (e.g. the output for the command before last).
Is there a way of adding a vertical empty space to the end of each output/ after each command is run that has no output?
Each new command that you enter is preceded by a "prompt", and these can be customized (though the exact way to customize depends on the shell). Since you mention Mac OS X I'm assuming you are using the default bash shell, in which case the absolute simplest way to add a blank line is like this: PROMPT_COMMAND=echo. You can run that command to try it out, or add it to a startup file (like .profile in your home folder) to have it done automatically each time.
If you use Bash 4.4 and you want a blank line after your prompt, you could set the PS0 prompt to a newline:
PS0="\n"
Now, this will be inserted every time you run a command:
$ echo "Hello"
Hello
Wondering this too, I've looked at the menu options in Terminal & most of the control characters one can type in and nothing does this on a keystroke. You can however enter an echo command, it alone to leave a single blank line below it before the next prompt. echo \n will add an extra blank line to that, echo \n\n to do 2 extra, ie. 3 blank lines, etc. (you can also do echo;echo;echo getting the same effect)
You can create a shell alias like alias b='echo;echo' (i couldn't seem to get the \n notation to work in a alias), then entering b on a prompt will leave a double-blank line, not bad. Then you gotta figure out how to save aliases in your .profile script.
I tried making an alias for the command ' ' ie. space character, which I though you could type like \ (hmm, stack overflow not formatting this well, that's backslash followed by a space, then return to execute it), but the bash shell doesn't seem to allow an alias with that name. It probably wouldn't allow a function named that either (similar to alias), though I didn't check.
I often use the fish shell, and I found that it does allow a function with that name! Created with function ' '; echo \n; end and indeed it works; at the shell prompt, typing the command \ (again backslash space) leaves a double blank line.
Cool, but.. I tried saving this function using funcsave ' ' (how you save functions in fish, no messing with startup scripts!) and afterwards the function no longer works :^( This is probably a bug in the fish shell. It's in active development right now though, I think I'll report this as a bug since I would kind of like this to work myself.
One could also send Apple a feature request through their bug reporter for an Insert Blank Line menu/keyboard command in Terminal. If someone pays attention to your request it might be implemented in a year maybe.
I wanted to solve exactly the same, and for anyone interested in doing the same, I used what tripleee said in his comment here - I created a .bash_profile (see details here) with the line export PS1="\n\n$ ".
Hopefully that helps someone else too!

In bash, how do I bind a function key to a command?

Example: I want to bind the F12 key to the command echo "foobar" such that every time I hit F12 the message "foobar" will be printed to screen. Ideally it could be any arbitrary shell command, not just builtins. How does one go about this?
You can determine the character sequence emitted by a key by pressing Ctrl-v at the command line, then pressing the key you're interested in. On my system for F12, I get ^[[24~. The ^[ represents Esc. Different types of terminals or terminal emulators can emit different codes for the same key.
At a Bash prompt you can enter a command like this to enable the key macro so you can try it out.
bind '"\e[24~":"foobar"'
Now, when you press F12, you'll get "foobar" on the command line ready for further editing. If you wanted a keystroke to enter a command immediately, you can add a newline:
bind '"\e[24~":"pwd\n"'
Now when you press F12, you'll get the current directory displayed without having to press Enter. What if you've already typed something on the line and you use this which automatically executes? It could get messy. However, you could clear the line as part of your macro:
bind '"\e[24~":"\C-k \C-upwd\n"'
The space makes sure that the Ctrl-u has something to delete to keep the bell from ringing.
Once you've gotten the macro working the way you want, you can make it persistent by adding it to your ~/.inputrc file. There's no need for the bind command or the outer set of single quotes:
"\e[24~":"\C-k \C-upwd\n"
Edit:
You can also create a key binding that will execute something without disturbing the current command line.
bind -x '"\eW":"who"'
Then while you're typing a command that requires a username, for example, and you need to know the names of user who are logged in, you can press Alt-Shift-W and the output of who will be displayed and the prompt will be re-issued with your partial command intact and the cursor in the same position in the line.
Unfortunately, this doesn't work properly for keys such as F12 which output more than two characters. In some cases this can be worked around.
The command (who in this case) could be any executable - a program, script or function.
You can define bash key bindings in ~/.inputrc (configuration file for the GNU Readline library). The syntax is
<keysym or key name>: macro
for example:
Control-o: "> output"
will create a macro which inserts "> output" when you press ControlO
"\e[11~": "echo foobar"
will create a macro which inserts "echo foobar" when you press F1... I don't know what the keysym for F11 is off hand.
Edit:
.inputrc understands the \n escape sequence for linefeed, so you can use
"\e[11~": "echo foobar\n"
Which will effectively 'press enter' after the command is issued.
This solution is specific to X11 environments and has nothing to do with bash, but adding the following to your .Xmodmaps
% loadkeys
keycode 88 = F12
string F12 = "foobar"
%
will send the string "foobar" to the terminal upon hitting F12.
I wanted to bind Ctrl+B to a command. Inspired by an answer above, I tried to use bind but could not figure out what series of cryptic squiggles (\e[24~ ?) translate to Ctrl+B.
On a Mac, go to Settings of the Terminal app, Profiles -> Keyboard -> + then press the keyboard shortcut you're after and it comes out. For me Ctrl+B resulted in \002 which i successfully bound to command
bind '"\002":"echo command"'
Also, if you want the command to be executed right-away (not just inserted in to the prompt), you can add the Enter to the end of your command, like so:
bind '"\002":"echo command\015"'

In bash, how does one clear the current input?

Suppose in bash you start writing a command like:
$ rm -rf /foo/bar/really/long/path/here
and then realize you don't want to execute this after all. Is there a way to clear the input with one or two keystrokes?
What I have been doing lately is prepending echo and enclosing the input in quotes (Ctrl+A, echo ", Ctrl+E, ") then hitting enter. Is there a faster way?
Press Ctrl-U to delete everything before the cursor. The deleted command will be stored into a buffer. Press Ctrl-Y to paste the deleted command.
(Optional: Press End or Ctrl-E to jump to the end of the input first.)
Alternatively, press Ctrl-C to abort what you're typing.
Try Ctrl+U. That clears the input line.
Found a short reference at http://www.ice2o.com/bash_quick_ref.html while searching.
ctrl + e (if not at the end of the line) plus ctrl + u will do it.
Ctrl-U, Ctrl-K does the trick as well.
Ctrl-U deletes everything from the beginning of the line up to the cursor, Ctrl-K deletes everything from the cursor to the end of the line. (It is sometimes useful to use only one of them.)
There are two options to do this
ctrl+c - this clears the whole line, no matter where the cursor is.
ctrl+u - this clear the line from the position of the cursor until the beginning.
A nice shortcut is pressing Esc#. It will prepend a # character (thus making the line a comment) and then press enter. If you then decide that you still the need the command, you still have it in your history :)
Pressing Esc plus Backspace in bash will clear everything up to the cursor's position.
(In Cygwin, this will clear the input up to the next word. Words are separated by spaces, underscores, ...)
This is an expansion of knittl's answer that stores the line in the console history by prefixing with a hash. Overcoming drawbacks of the clipboard, such as accidental overwriting or being unable to view the cut line for reference.
Comment Line & Return New Prompt
Use either key shortcut:
Esc,#
Alt+#
A hash character # will be prepended to the line, thus turning the whole line into a comment. It will also return a new prompt, as if enter was pressed by the user. e.g.
$ #rm -rf /foo/bar/really/long/path/here
$
Retrieve Commented Line
To recover the old line from console history use one of the following shortcuts:
Up
Ctrl+p
Repeat key shortcut until the desired line appears.
Quick Hash Prefix Removal
To remove the line's hash # prefix there are a few different options available:
Remove first character and immediately execute command:
Esc,1,Esc,#
Alt+-, Alt+#
Move cursor to start and remove first character, without executing the command:
Home, Delete
Ctrl+a, Ctrl+d
Consider that using Ctrl-U (or Ctrl-E and then Ctrl-U) will store what you clear in a buffer so that you can then paste it later using Ctrl-Y.
If you are using Bash in vi mode (set it with set -o vi), then press Esc to switch to the normal mode of vi, and type dd to delete the current line!
To delete the current line, try:
Ctrl-X, Ctrl-U
As an alternative you may use:
Esc-D
which requires in ~/.inputrc:
"\ed": kill-whole-line
see: http://codesnippets.joyent.com/posts/show/1690

Going backwards in a bash prompt

I'd like to have a blank line after my bash prompt and before the output on my Mac. It should look like this would:
echo; ls
Can I add a newline to my bash prompt and then go back up one line to wait for user input? Is there something obvious I'm missing?
I know this is old but for someone like me who came across this while googling for it. This is how you do this...
It's actually pretty simple!
Check out this link --> Cursor Movement
Basically to move up N number of lines:
echo -e "\033[<N>A HELLO WORLD\n"
Just change the "< N >" to however many lines you want to go back...
For instance, to move up 5 lines it would be "/033[5A"
To my knowledge this is not possible unless you delve into more low-level stuff like full-screen emulators like curses.
This is a bit of a stab in the dark, but you may be able to use VT102 terminal codes to control the cursor without having to use Curses. The relevant VT102 commands that you'd be interested in all consist of sending ESC, then [, then the specific command parameters.
For instance, to move the cursor up one line, one needs to output:
ESC [ 1 A
0x1B 0x5B 0x31 0x41
Be warned that the VT102 documentation generally uses octal, so keep an ascii table handy if you're using hex.
All of this advice is given without having tested it -- I don't know if VT102 commands can be embedded into your bash prompt, but I thought it might be worth a shot.
Edit: Yeah -- looks like a lot of people use VT102 formatting codes in their bash prompts. To translate my above example into something Bash would recognize, putting:
\e[1A
into your prompt should move the cursor up one line.
This is very possible. If your bash has C-v set as the readline quoted-insert command, you can simply add the following to your ~/.inputrc:
RETURN: "\C-e\C-v\n\C-v\n\n"
This wil make bash (readline, actually) insert two verbatim newlines before a regular interpreted newline. By default, only one is inserted, which is what causes output to start on the line after the prompt.
You can test if C-v is set to quoted-insert by typing it in bash (that's Ctrl+V) followed by e.g. an up arrow. This should print ^[[A or something similar. If it doesn't, you can bind it in ~/.inputrc too:
C-v: quoted-insert
RETURN: "\C-e\C-v\n\C-v\n\n"
~/.inputrc can be created if it doesn't exist. The changes will not take effect in running bashes unless you issue a readline re-read-init-file command (by default on C-x C-r). Be careful though. If you do something wrong, enter will no longer issue commands, and fixing your mistake could prove to be difficult. If you should do something wrong, C-o will by default also accept the line.
Adding a newline followed by moving the cursor back to the regular prompt (like you described) is possible, but will not have the effect you intend. The newline you inserted would simply be overwritten by the application output, since you moved the cursor back in front of it.
This works:
trap echo DEBUG
It doesn't add an extra newline if you hit return at an empty prompt.
The command above will cause a newline to be output for every member of a pipeline or multi-command line such as:
$ echo foo; echo bar
\n
foo
\n
bar
To prevent that so that only one extra newline is output before all command output:
PROMPT_COMMAND='_nl=true'; trap -- '$_nl && [[ $BASH_COMMAND != $PROMPT_COMMAND ]] && echo; _nl=false' DEBUG
The DEBUG trap is performed before each command so before the first command it checks to see if the flag is true and, if so, outputs a newline. Then it sets the flag to false so each command afterwards on the line doesn't trigger an extra newline.
The contents of $PROMPT_COMMAND are executed before the prompt is output so the flag is set to true - ready for the next cycle.
Because pressing enter on an empty command line still triggers the execution of the contents of $PROMPT_COMMAND the test in the trap also checks for those contents as the current command and doesn't perform the echo if they match.
I believe (but haven't tried) if you put '\n\b' in the prompt string it would do that.
In general, if you want to find out the codes to do anything a terminal can do, read the terminfo man page.
In this case, the cursor up one line code can be determined by:
tput cuu1
If you redirect the tput output to a file, you can see what control characters are used.
Bash also supports the PROMPT_COMMAND variable, allowing you to run arbitrary commands before each prompt is issued.

Resources