How do I avoid this while printing to screen? - printk

When I do a printk with "bad" characters, sometimes I scrambles my screen,
Is this unavoidable when printing them?

Type reset and Enter on your command line to get it fixed again. If you don't see it anymore, you might have to type it blindly, but it works often.
Depending on how you set up your environment, there are characters that can't be printed without messing up your command line.
You can try to print hex values %x. Like this it is usually much easier to find a logic behind the data.

Related

How to get console cursor back to the begining of line in R?

Sometimes I perform long lasting calculations in the loop. To get control of the progress I print current value of the loop variable. However it makes my console flooded with numbers.
I would like to move back cursor to the begining of console line after each printing. This would end with the last value of the variable only. That would be similar to command without on line printer.
Any idea on how to achieve that in R console?
as b2f correctly mentions in the comment use \r as in the example below
cat('hello world\rMinor')
Minor world
One thing to note though is that every command is assigned a new line in an interactive setting, so in interactive code this only works as part of a single command.

Paste bracketing causes slow pastes

When I paste in large blocks of text, or even just long commands or urls, pasting is done character by character and takes a long time. I can make it speed up by pressing right arrow but that sometimes autosuggest will then come in and paste other things.
I know this is caused in part by paste bracketing. I tried turning it off which makes it about the speed it goes when I press right arrow during a slow paste. But then indents aren't preserved and it executes line by line. This doesn't work for me since I regularly paste in multiline commands like with Docker for example. Also, syntax highlighting doesn't work anymore when using the right arrow technique.
Is there a way to make pasting instant but preserve the bracketing behavior? In vim it's instant but I guess indents are just newline characters or something I guess? Is bracketing even causing this problem or is there some other explanation/fix?
I'm using iTerm2 with ohmyzsh and Powerlevel10k on MacOS Catalina. Searched high and low but only found posts about the ~0 and ~1 characters being inserted with bracketing or something. I never had that happen.
A gif for illustration purposes:
https://i.imgur.com/vFL0gmL.mp4
Just disable magic functions in .zsh.
Uncoment DISABLE_MAGIC_FUNCTIONS="true"

How to replace a string pattern with different strings quickly?

For example, I have many HTML tabs to style, they use different classes, and will have different backgrounds. Background images files have names corresponding to class names.
The way I found to do it is yank:
.tab.home {
background: ...home.jpg...
}
then paste, then :s/home/about.
This is to be repeated for a few times. I found that & can be used to repeat last substitute, but only for the same target string. What is the quickest way to repeat a substitute with different target string?
Alternatively, probably there are more efficient ways to do such a thing?
I had a quick play with some vim macro magic and came up with the following idea... I apologise for the length. I thought it best to explain the steps..
First, place the text block you want to repeat into a register (I picked register z), so with the cursor at the beginning of the .tab line I pressed "z3Y (select reg z and yank 3 lines).
Then I entered the series of VIM commands I wanted into the buffer as )"zp:.,%s/home/. (Just press i and type the commands)
This translate to;
) go the end of the current '{}' block,
"zp paste a copy of the text in register z,
.,%s/home/ which has two tricks.
The .,% ensures the substitution applies to everything from the start of the .tab to the end of the closing }, and,
The command is incomplete (ie, does not have a at the end), so vim will prompt me to complete the command.
Note that while %s/// will perform a substitution across every line of the file, it is important to realise that % is an alias for range 1,$. Using 1,% as a range, causes the % to be used as the 'jump to matching parenthesis' operator, resulting in a range from the current line to the end of the % match. (which in this example, is the closing brace in the block)
Then, after placing the cursor on the ) at the beginning of the line, I typed "qy$ which means yank all characters to the end of the line into register q.
This is important, because simply yanking the line with Y will include a carriage return in the register, and will cause the macro to fail.
I then executed the content of register q with #q and I was prompted to complete the s/home/ on the command line.
After typing the replacement text and pressing enter, the pasted block (from register z) appeared in the buffer with the substitutions already applied.
At this point you can repeat the last #qby simple typing ##. You don't even need to move the cursor down to the end of the block because the ) at the start of the macro does that for you.
This effectively reduces the process of yanking the original text, inserting it, and executing two manual replace commands into a simple ##.
You can safely delete the macro string from your edit buffer when done.
This is incredibly vim-ish, and might waste a bit of time getting it right, but it could save you even more when you do.
Vim macro's might be the trick you are looking for.
From the manual, I found :s//new-replacement. Seemed to be too much typing.
Looking for a better answer.

Multiline prompt formatting incorrectly due to date command in zsh

I have the following in my .zshrc:
setopt PROMPT_SUBST
precmd(){
echo""
LEFT="$fg[cyan]$USERNAME#$HOST $fg[green]$PWD"
RIGHT="$fg[yellow]$(date +%I:%M\ %P)"
RIGHTWIDTH=$(($COLUMNS-${#LEFT}))
echo $LEFT${(l:$RIGHTWIDTH:)RIGHT}
}
PROMPT="$ "
This gives me the following screenshot
The time part on the right is always not going all the way to the edge of the terminal, even when resized. I think this is due to the $(date +%I:%M\ %P) Anyone know how to fix this?
EDIT: Zoomed in screenshot
While your idea is commendable, the problem you suffer from is that your LEFT and RIGHT contains ANSI escape sequences (for colors), which should be zero-width characters, but are nevertheless counted toward the length of a string if you naively use $#name, or ${(l:expr:)name}.
Also, as a matter of style, you're better off using Zsh's builtin prompt expansion, which wraps a lot of common things people may want to see in their prompts in short percent escape sequences. In particular, there are builtin sequences for colors, so you don't need to rely on nonstandard $fg[blah].
Below is an approximate of your prompt written in my preferred coding style... Not exactly, I made everything super verbose so as to be understandable (hopefully). The lengths of left and right preprompts are calculated after stripping the escape sequences for colors and performing prompt expansion, which gives the correct display length (I can't possibly whip that up in minutes; I ripped the expression off pure).
precmd(){
local preprompt_left="%F{cyan}%n#%m %F{green}%~"
local preprompt_right="%F{yellow}%D{%I:%M %p}%f"
local preprompt_left_length=${#${(S%%)preprompt_left//(\%([KF1]|)\{*\}|\%[Bbkf])}}
local preprompt_right_length=${#${(S%%)preprompt_right//(\%([KF1]|)\{*\}|\%[Bbkf])}}
local num_filler_spaces=$((COLUMNS - preprompt_left_length - preprompt_right_length))
print -Pr $'\n'"$preprompt_left${(l:$num_filler_spaces:)}$preprompt_right"
}
PROMPT="$ "
Edit: In some terminal emulators, printing exactly $COLUMN characters might wrap the line. In that case, replace the appropriate line with
local num_filler_spaces=$((COLUMNS - preprompt_left_length - preprompt_right_length - 1))
End of edit.
This is very customizable, because you can put almost anything in preprompt_left and preprompt_right and still get the correct lengths — just remember to use prompt escape sequence for zero width sequences, e.g., %F{}%f for colors, %B%b for bold, etc. Again, read the docs on prompt expansion: http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html.
Note: You might notice that %D{%I:%M %p} expands to things like 11:35 PM. That's because I would like to use %P to get pm, but not every implementation of strftime supports %P. Worst case scenario: if you really want lowercase but %P is not supported, use your original command subsitution $(date +'%I:%M %P').
Also, I'm using %~ instead of %/, so you'll get ~/Desktop instead of /c/Users/johndoe/Desktop. Some like it, some don't. However, as I said, this is easily customizable.

Why is a % symbol appearing in my standard output?

Sometimes I echo a string to the standard output (I see it at the Kubuntu console) and a trailing % symbol gets appended with it's colors inverted (black text on white background).
I can't find any accidental additional character (or half-baked-UTF8 or anything) in the string I'm printing. The character seems to get added when the program finishes.
I'm using Go (golang) right now, but I've already seen this in the past, and I think I was using PHP back then.
What could be causing this?
what shell are you using? sounds like what happens in zsh when there is no carriage return at the end of your output

Resources