3

I am using zsh shell, which has been the default shell for macOS since 2019.

Suppose a command has been entered as below...

filebot -rename "/Users/john/Desktop/BBC Storyville 2022 The Fire Within 1080p HDTV.mp4" --db TheTVDB --filter "id in [82300]"

And I want to select/mark the entire file path to replace or delete. For years I just place the cursor, using the Option key, at the last character and delete one character at a time until I get to the beginning. Is there a faster/better way without changing to another console app?

I know how to delete the entire line - Control-U
I know how to delete from the cursor to the end of the line - Control-K

According to this stack exchange page you can determine mode. Unfortunately the method they describe "describe-mode" isn't available in my shell.

I tried esc - x which brought up a prompt

execute: _

I then just hit return and Y

0-zsh% 
execute: _
zsh: do you wish to see all 401 possibilities (201 lines)?
.accept-and-hold
.accept-and-infer-next-history
.accept-and-menu-complete
.accept-line
.accept-line-and-down-history
.accept-search
.argument-base
.auto-suffix-remove
.auto-suffix-retain
.backward-char
.backward-delete-char
.backward-delete-word
.backward-kill-line
.backward-kill-word
.backward-word
.beep
.beginning-of-buffer-or-history
.beginning-of-history
.beginning-of-line
.beginning-of-line-hist
.bracketed-paste
.capitalize-word
.clear-screen
.complete-word
.copy-prev-shell-word
.copy-prev-word
.copy-region-as-kill
.deactivate-region
.delete-char
.delete-char-or-list
.delete-word
.describe-key-briefly
.digit-argument
.down-case-word
.down-history
.down-line
.down-line-or-history
.down-line-or-search
.emacs-backward-word
.emacs-forward-word
.end-of-buffer-or-history
.end-of-history
.end-of-line
.end-of-line-hist
.end-of-list
.exchange-point-and-mark
.execute-last-named-cmd
.execute-named-cmd
.expand-cmd-path
.expand-history
.expand-or-complete
.expand-or-complete-prefix
.expand-word
.forward-char
.forward-word
.get-line
.gosmacs-transpose-chars
.history-beginning-search-backward
.history-beginning-search-forward
.history-incremental-pattern-search-backward
.history-incremental-pattern-search-forward
.history-incremental-search-backward
.history-incremental-search-forward
.history-search-backward
.history-search-forward
.infer-next-history
.insert-last-word
.kill-buffer
.kill-line
.kill-region
.kill-whole-line
.kill-word
.list-choices
.list-expand
.magic-space
.menu-complete
.menu-expand-or-complete
.neg-argument
.overwrite-mode
.pound-insert
.push-input
.push-line
.push-line-or-edit
.put-replace-selection
.quote-line
.quote-region
.quoted-insert
.read-command
.recursive-edit
.redisplay
.redo
.reset-prompt
.reverse-menu-complete
.run-help
.select-a-blank-word
.select-a-shell-word
.select-a-word
.select-in-blank-word
.select-in-shell-word
.select-in-word
.self-insert
.self-insert-unmeta
.send-break
.set-local-history
.set-mark-command
.spell-word
.split-undo
.transpose-chars
.transpose-words
.undefined-key
.undo
.universal-argument
.up-case-word
.up-history
.up-line
.up-line-or-history
.up-line-or-search
.vi-add-eol
.vi-add-next
.vi-backward-blank-word
.vi-backward-blank-word-end
.vi-backward-char
.vi-backward-delete-char
.vi-backward-kill-word
.vi-backward-word
.vi-backward-word-end
.vi-beginning-of-line
.vi-caps-lock-panic
.vi-change
.vi-change-eol
.vi-change-whole-line
.vi-cmd-mode
.vi-delete
.vi-delete-char
.vi-digit-or-beginning-of-line
.vi-down-case
.vi-down-line-or-history
.vi-end-of-line
.vi-fetch-history
.vi-find-next-char
.vi-find-next-char-skip
.vi-find-prev-char
.vi-find-prev-char-skip
.vi-first-non-blank
.vi-forward-blank-word
.vi-forward-blank-word-end
.vi-forward-char
.vi-forward-word
.vi-forward-word-end
.vi-goto-column
.vi-goto-mark
.vi-goto-mark-line
.vi-history-search-backward
.vi-history-search-forward
.vi-indent
.vi-insert
.vi-insert-bol
.vi-join
.vi-kill-eol
.vi-kill-line
.vi-match-bracket
.vi-open-line-above
.vi-open-line-below
.vi-oper-swap-case
.vi-pound-insert
.vi-put-after
.vi-put-before
.vi-quoted-insert
.vi-repeat-change
.vi-repeat-find
.vi-repeat-search
.vi-replace
.vi-replace-chars
.vi-rev-repeat-find
.vi-rev-repeat-search
.vi-set-buffer
.vi-set-mark
.vi-substitute
.vi-swap-case
.vi-undo-change
.vi-unindent
.vi-up-case
.vi-up-line-or-history
.vi-yank
.vi-yank-eol
.vi-yank-whole-line
.visual-line-mode
.visual-mode
.what-cursor-position
.where-is
.which-command
.yank
.yank-pop
_bash_complete-word
_bash_list-choices
_complete_debug
_complete_help
_complete_tag
_correct_filename
_correct_word
_expand_alias
_expand_word
_history-complete-newer
_history-complete-older
_list_expansions
_most_recent_file
_next_tags
_read_comp
accept-and-hold
accept-and-infer-next-history
accept-and-menu-complete
accept-line
accept-line-and-down-history
accept-search
argument-base
auto-suffix-remove
auto-suffix-retain
backward-char
backward-delete-char
backward-delete-word
backward-kill-line
backward-kill-word
backward-word
beep
beginning-of-buffer-or-history
beginning-of-history
beginning-of-line
beginning-of-line-hist
bracketed-paste
capitalize-word
clear-screen
complete-word
copy-prev-shell-word
copy-prev-word
copy-region-as-kill
deactivate-region
delete-char
delete-char-or-list
delete-word
describe-key-briefly
digit-argument
down-case-word
down-history
down-line
down-line-or-history
down-line-or-search
emacs-backward-word
emacs-forward-word
end-of-buffer-or-history
end-of-history
end-of-line
end-of-line-hist
end-of-list
exchange-point-and-mark
execute-last-named-cmd
execute-named-cmd
expand-cmd-path
expand-history
expand-or-complete
expand-or-complete-prefix
expand-word
forward-char
forward-word
get-line
gosmacs-transpose-chars
history-beginning-search-backward
history-beginning-search-forward
history-incremental-pattern-search-backward
history-incremental-pattern-search-forward
history-incremental-search-backward
history-incremental-search-forward
history-search-backward
history-search-forward
infer-next-history
insert-last-word
kill-buffer
kill-line
kill-region
kill-whole-line
kill-word
list-choices
list-expand
magic-space
menu-complete
menu-expand-or-complete
neg-argument
overwrite-mode
pound-insert
push-input
push-line
push-line-or-edit
put-replace-selection
quote-line
quote-region
quoted-insert
read-command
recursive-edit
redisplay
redo
reset-prompt
reverse-menu-complete
run-help
select-a-blank-word
select-a-shell-word
select-a-word
select-in-blank-word
select-in-shell-word
select-in-word
self-insert
self-insert-unmeta
send-break
set-local-history
set-mark-command
spell-word
split-undo
transpose-chars
transpose-words
undefined-key
undo
universal-argument
up-case-word
up-history
up-line
up-line-or-history
up-line-or-search
vi-add-eol
vi-add-next
vi-backward-blank-word
vi-backward-blank-word-end
vi-backward-char
vi-backward-delete-char
vi-backward-kill-word
vi-backward-word
vi-backward-word-end
vi-beginning-of-line
vi-caps-lock-panic
vi-change
vi-change-eol
vi-change-whole-line
vi-cmd-mode
vi-delete
vi-delete-char
vi-digit-or-beginning-of-line
vi-down-case
vi-down-line-or-history
vi-end-of-line
vi-fetch-history
vi-find-next-char
vi-find-next-char-skip
vi-find-prev-char
vi-find-prev-char-skip
vi-first-non-blank
vi-forward-blank-word
vi-forward-blank-word-end
vi-forward-char
vi-forward-word
vi-forward-word-end
vi-goto-column
vi-goto-mark
vi-goto-mark-line
vi-history-search-backward
vi-history-search-forward
vi-indent
vi-insert
vi-insert-bol
vi-join
vi-kill-eol
vi-kill-line
vi-match-bracket
vi-open-line-above
vi-open-line-below
vi-oper-swap-case
vi-pound-insert
vi-put-after
vi-put-before
vi-quoted-insert
vi-repeat-change
vi-repeat-find
vi-repeat-search
vi-replace
vi-replace-chars
vi-rev-repeat-find
vi-rev-repeat-search
vi-set-buffer
vi-set-mark
vi-substitute
vi-swap-case
vi-undo-change
vi-unindent
vi-up-case
vi-up-line-or-history
vi-yank
vi-yank-eol
vi-yank-whole-line
visual-line-mode
visual-mode
what-cursor-position
where-is
which-command
yank
yank-pop

I don't know how to delete the middle section of an entered line. nano has a way to create two marks and delete text in between.

Does Terminal.app or zsh have this capability using emacs-style keys or vi-style keys or another option?

2

2 Answers 2

2

Which shell? It has very little to do with Terminal.app and everything to do with whatever editor your shell provides.

From your examples, it seems like whatever shell you are using, you have it in Emacs mode. I will discuss ksh, as that's what I use.

ESC b Move one word backward
ESC f Move one word forward
ESC DEL, ESC h, ESC CTRL-H Delete one word backward
ESC d Delete one word forward
    
CTRL-@ Set mark at point
CTRL-W Delete ("wipe") all characters between point and mark.

(from docstore.mik.ua/orelly/unix3/korn/ch02_03.htm)

5
  • I am using the default shell which is zsh. Terminal.app Help lists commands as if they work for whichever shell the user is running in Terminal.app. It's not mentioned that it's dependent on the shell. It could be that the default "mode" is Emacs for Terminal... I am not clear on how to determine what mode it's in.
    – John
    Commented Oct 21, 2022 at 15:30
  • I don't believe I am in emacs mode because the C-h key strokes don't work. Using the Escape key as a modifier key does work.
    – John
    Commented Oct 21, 2022 at 16:17
  • 2
    For zsh, bindkey -l will list available modes. -e selects Emacs, -v selects Vi. You'll find it documented in man zshzle Commented Oct 21, 2022 at 16:19
  • If ^K and ^U work, your shell is in emacs mode. Terminal.app doesn't have a mode. It exposes the Cocoa edit controls in its Edit menu but most of those aren't going to be passed to the shell. Terminal.app isn't an application ithat provides a command line, it is a framework inside of which an application such as the shell runs. Commented Oct 21, 2022 at 17:01
  • 1
    I would also point out that if you are planning on using the shell more than trivially, you should invest some effort in configuring it the way you want it to work. Such as, explicitly setting the editor in the shell's rc file(s). Commented Oct 21, 2022 at 17:09
0

I'm from the school of vi and old shells so I would do this in a vanilla zsh shell:

set -o vi

Then use the w/W or b/B command to get to the beginning word needing deletion and then count the words to delete (if there were two words)

d2w

There are shell wizards far more skilled than I so I hope to learn from their better or more powerful solutions. You probably get more mileage from bindkey -v instead of set -o vi but I've not needed more than the old school commands from my learnings before zsh came about.

1
  • 1
    I don't use zsh, but did some experimenting and it's odd that by default both emacs and vi are off, but the shell is in emacs mode. That's probably documented somewhere. ksh (well, ksh93 u+m) defaults to emacs mode if you don't tell it otherwise but it also turns on set -o emacs when it does it. zsh doesn't seem to. Commented Oct 21, 2022 at 17:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .