zsh-users
 help / color / mirror / code / Atom feed
* Convert control characters to bindkey/quoted-insert -style escape sequences
@ 2024-01-24 13:35 Marlon Richert
  2024-01-24 13:54 ` Roman Perepelitsa
  2024-01-24 23:21 ` Bart Schaefer
  0 siblings, 2 replies; 4+ messages in thread
From: Marlon Richert @ 2024-01-24 13:35 UTC (permalink / raw)
  To: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 347 bytes --]

I know I can convert control characters to $'...' -style escape sequences
using ${(qqqq)...} or ${(q+)...} expansion.

However, how can I convert control characters to bindkey/quoted-insert
-style escape sequences?

For example, I want to convert a newline character to ^M and not $'\n'

Basically, I want the inverse of a ${(g:c:)...} expansion.

[-- Attachment #2: Type: text/html, Size: 955 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Convert control characters to bindkey/quoted-insert -style escape sequences
  2024-01-24 13:35 Convert control characters to bindkey/quoted-insert -style escape sequences Marlon Richert
@ 2024-01-24 13:54 ` Roman Perepelitsa
  2024-01-25  8:34   ` Marlon Richert
  2024-01-24 23:21 ` Bart Schaefer
  1 sibling, 1 reply; 4+ messages in thread
From: Roman Perepelitsa @ 2024-01-24 13:54 UTC (permalink / raw)
  To: Marlon Richert; +Cc: Zsh Users

On Wed, Jan 24, 2024 at 2:36 PM Marlon Richert <marlon.richert@gmail.com> wrote:
>
> I know I can convert control characters to $'...' -style escape sequences using ${(qqqq)...} or ${(q+)...} expansion.
>
> However, how can I convert control characters to bindkey/quoted-insert -style escape sequences?
>
> For example, I want to convert a newline character to ^M and not $'\n'
>
> Basically, I want the inverse of a ${(g:c:)...} expansion.

That thing with psvar that you described in another thread is
${(V)name}. It'll give you ^M (rather than \r) as you seem to want.
You are still going to get \n instead of ^J and \t instead of ^I. It's
pretty good in practice for what it is meant to do: make a string
presentable to humans. It's not particularly consistent with the
choice of C-escapes vs carets though.

I don't know if there is a way to encode special characters
exclusively through caret notation. This encoding is fairly simple, so
you could also do it manually if you need to.

Roman.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Convert control characters to bindkey/quoted-insert -style escape sequences
  2024-01-24 13:35 Convert control characters to bindkey/quoted-insert -style escape sequences Marlon Richert
  2024-01-24 13:54 ` Roman Perepelitsa
@ 2024-01-24 23:21 ` Bart Schaefer
  1 sibling, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 2024-01-24 23:21 UTC (permalink / raw)
  To: Zsh Users

On Wed, Jan 24, 2024 at 5:36 AM Marlon Richert <marlon.richert@gmail.com> wrote:
>
> For example, I want to convert a newline character to ^M and not $'\n'

Ugly, but:

showctl() {
  local -a ctls=(
   # Subshell for throwaway keymap
   $(bindkey -N ctl &&
     bindkey -M ctl -- "$1" '' &&
     bindkey -M ctl)
  )
  print -r -- "${ctls[1]}"
}


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Convert control characters to bindkey/quoted-insert -style escape sequences
  2024-01-24 13:54 ` Roman Perepelitsa
@ 2024-01-25  8:34   ` Marlon Richert
  0 siblings, 0 replies; 4+ messages in thread
From: Marlon Richert @ 2024-01-25  8:34 UTC (permalink / raw)
  To: Roman Perepelitsa; +Cc: Zsh Users

${(V)…} is exactly what I was looking for. Thanks!

Could the documentation of that flag be updated, though?

It nows says: “Make any special characters in the resulting words visible.”

To make it easier to find, it could mention that it “quotes” or “escapes” Those are the kind of words I searched for and which are used to describe this kind of thing elsewhere in the manual. Here’s some examples:

* ${(g)…} “Process escape sequences”
* ${(q)…}: “Quote characters that are special to the shell”
* ${(q+)…}: “This quoting is similar to that used by the output of values by the typeset family of commands.”
* ${(Q)…}: “Remove one level of quotes.”
* ${(p)…} “Recognize the same escape sequences as the print builtin”
* bindkey: “the following escape sequences are recognised”
* print -b: “Recognize all the escape sequences defined for the bindkey command.”
* quoted-insert

It could also mention what format it uses to escape the characters.

Finally, parameter flags dealing with quoting/escaping could be grouped together in one section. That would help a lot. 

> On 24. Jan 2024, at 15.54, Roman Perepelitsa <roman.perepelitsa@gmail.com> wrote:
> 
> That thing with psvar that you described in another thread is
> ${(V)name}. It'll give you ^M (rather than \r) as you seem to want.
> You are still going to get \n instead of ^J and \t instead of ^I. It's
> pretty good in practice for what it is meant to do: make a string
> presentable to humans. It's not particularly consistent with the
> choice of C-escapes vs carets though.
> 
> I don't know if there is a way to encode special characters
> exclusively through caret notation. This encoding is fairly simple, so
> you could also do it manually if you need to.
> 
> Roman.


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-01-25  8:35 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-24 13:35 Convert control characters to bindkey/quoted-insert -style escape sequences Marlon Richert
2024-01-24 13:54 ` Roman Perepelitsa
2024-01-25  8:34   ` Marlon Richert
2024-01-24 23:21 ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).