How to delete from current position to the beginning of the line? - terminal

How do I delete everything from my cursor current position to the beginning of the line?
For example, say I'm running npm run start and my cursor is positioned just before start? How do I remove npm run with a keyboard shortcut leaving start?
~ npm run â–ˆstart
I'm expecting something like how ctrl+w deletes the last word. Similarly, is there a shortcut to delete from current cursor position to the beginning of the line?
I'm using oh-my-zsh shell on Mac 10.12.
EDIT:
Some shortcuts here -
move to the start of line - ctrl + a
move to the end of line - ctrl + e
clear the word previous to the cursor - ctrl + w
clear text from cursor to the start of line - ctrl + r, by adding "bindkey \^R backward-kill-line" to your zshrc file
clear text from cursor to the end of line - ctrl + k
clear full line - ctrl + u

Use Ctrl+U
unix-line-discard (C-u)
Kill backward from point to the beginning of the line. The killed text is saved on the kill-ring.
It is also useful to see man bash.
In case you use zsh, you should add this line: bindkey \^U backward-kill-line to your .zshrc. See this.

Related

Shortcut to jump to the previous white space in a bash command line

To jump to the previous word in a command line, I use Alt + b.
However, the names of my files are pretty long and look like this:
2018_09_03_abcdef_ghijkl_mnopqr_stuvwx_yz.txt
When I want to change the name of these files, I use the command mv and a shortcut** that permits me to paste the first word/argument (the current name of the file). This gives me the following command:
$ mv 2018_09_03_abcdef_ghijkl_mnopqr_stuvwx_yz.txt 2018_09_03_abcdef_ghijkl_mnopqr_stuvwx_yz.txt
Then I either want to change the date of the day and/or the first letters of the file name to get for example the final command line:
$ mv 2018_09_03_abcdef_ghijkl_mnopqr_stuvwx_yz.txt 2018_09_04_ABcd1234_ghijkl_mnopqr_stuvwx_yz.txt
To make the change at the beginning of the file name, I have to type the shortcut Alt + b several times since this shortcut considers every letters separated by an underscore as a word.
I would like to be able to jump directly to the beginning of the name (not the beginning of the line) to modify it. A shortcut targeting the white space would be ideal.
I have not been able to find such a shortcut that would skip the underscores and go directly to the previous white space.
Did anyone already create a shortcut in bash that allows you to do this?
Would the only possible way to accomplish this be to switch from emacs mode to vi mode (set -o vi) and use the vi shortcut:
F + space
?
Thank you very much in advance!
** The shortcut I found to paste the previous word in the current command line and paste it in the same current command line uses Alt + j and has to be added in the '~/.inputrc' file (followed by bind -f ~/.inputrc):
"\ej":"!#:$\e^"
There are several shortcuts to navigate bash command line. Here's another list.
Assuming cursor is at the end of the line, it could jump to 03 (day part) by typing Meta+7 Meta+b (2 keystrokes without releasing Meta key).
Now, for the file name changes, brace expansion could be used to get a command like this that provides the second argument from expansion.
mv 2018_09_{03_abc,04_ABC}def_ghijkl_mnopqr_stuvwx_yz.txt

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 to make the specified command run in vim?

I know how to make shell command run in vim,in command mode,type
:!shell_command
Now, I have a command in line 16 of a file open in Vim.
cp /home/debian/Downloads/rar/rar /usr/local/bin
How can I run this command in vim, without typing all of it?
When I hit :! to open up the ex command line and then use <C-r>", I get this:
ls /tmp^m
I have to erase ^m with the backspace key, and press the enter key, is that right?
can i not to let ^M to be shown ?
To run the command on the line your cursor is on you can yank the line into you clipboard. Type :! to open up the ex command line and then use <C-r>" to paste the command onto the command line.
So with the cursor on the line type and hit enter.
yy:!<C-r>"
Take a look at :h <c-r>.
CTRL-R {0-9a-z"%#:-=.} *c_CTRL-R* *c_<C-R>*
Insert the contents of a numbered or named register. Between
typing CTRL-R and the second character '"' will be displayed
to indicate that you are expected to enter the name of a
register.
The text is inserted as if you typed it, but mappings and
abbreviations are not used. Command-line completion through
'wildchar' is not triggered though. And characters that end
the command line are inserted literally (<Esc>, <CR>, <NL>,
<C-C>). A <BS> or CTRL-W could still end the command line
though, and remaining characters will then be interpreted in
another mode, which might not be what you intended.
Special registers
Simple answer:
:exec '!'.getline('.')

On bash command-line, how to delete all letters before cursor?

On bash command-line, how to delete all letters before cursor? I know Ctrl-k deletes all afterward the cursor.
Ctrl-u - Cut everything before the cursor
Other Bash shortcuts,
Ctrl-a Move cursor to beginning of line
Ctrl-e Move cursor to end of line
Ctrl-b Move cursor back one word
Ctrl-f Move cursor forward one word
Ctrl-w Cut the last word
Ctrl-k Cut everything after the cursor
Ctrl-y Paste the last thing to be cut
Ctrl-_ Undo
And discover more via man page for bash shell: man bash
Additional bash command-line shortcut cheat sheet: http://www.bigsmoke.us/readline/shortcuts
See the documentation here: http://www.gnu.org/software/bash/manual/bashref.html#Commands-For-Killing
Obligatory: Learn more about Bash, Linux, and Tech through Julia's comics: https://twitter.com/b0rk/media
In zsh, Alt+w clears all characters before the cursor.
In contrast to bash this does NOT cut them; it just deletes them.
This applies to zsh's Emacs mode (which is the default), NOT to Vi mode.
The hotkey Ctrl+U should do this for you.
Ctrl+x backspace clear all entry before cursor current position

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

Resources