Emacs, Evil-Mode: Replace only in visual selection/visual block? - emacs

By default, :s/[search-term]/[replace-term] works on whole lines rather than on visual selections. For example, if you select between c and e, as such:
a b |c d e| f g
and do :s/ //g, the result is:
abcdefg
rather than
a b cde f g
Similarly, in a visual block selection:
a b |c d e| f g
0 1 |2 3 4| 5 6
:s/ //g yields
abcdefg
0123456
rather than
a b cde f g
0 1 234 5 6
Does anyone have a way to make evil-mode's :s/ work only on the selection (preferably by default, or alternatively with a keyword like vim's \%V)?
(:s/\%V //g does not seem to work in this case; it leads to 0 matches.)
Thanks beforehand.

You can do the replacement in a visual selection by specifying the range. '<,'> works on the first line to the last line of the selection, and `<,`> works on the first character to the last character. So in your first example of
a b |c d e| f g`,
using :`<,`>s/ //g will give you
a b cde f g
Unfortunately, Evil doesn't seem to currently support replacement in a Visual Block, so there's no easy way to do that replacement.

There is now a package evil-visual-replace to accomplish replacement in blocks:
https://github.com/troyp/evil-visual-replace
It would be nice if the underlying logic were integrated with evil, so that pressing : with a visual block just worked, but it's better than nothing.

Related

How can one drop/delete columns from a KDB table in place?

Following the documentation, I tried to do the following:
t:([]a:1 2 3;b:4 5 6;c:`d`e`f) // some input table
`a`b _ t // works: delete NOT in place
(enlist `a) _ t // works: delete NOT in place
t _:`a`b // drop columns in place does not work; how to make it to work?
// 'type
// [0] t _:`a`b
Thank you very much for your help!
You should be able to use
delete a,b from `t
to delete in place (The backtick implies in place).
Alternatively, for more flexibility you could use the functional form;
![`t;();0b;`a`b]
The simplest way to achieve column deletion in place is using qSQL:
t:([]a:1 2 3;b:4 5 6;c:`d`e`f)
delete a,b from `t -- here, the backtick before t makes the change in place.
q)t
c
-
d
e
f
Michael & Kyle have covered the q-SQL options; for completeness, here are a couple of other options using _:
Using _ as in your question, you can re-assign this back to t e.g.
t:`a`b _ t
You can also use . amend with an empty list of indexes i.e. "amend entire", which can be done in-place by passing `t or not in-place by passing just t e.g.
q).[t;();`a`b _] / not in-place
c
-
d
e
f
q).[`t;();`a`b _] / in-place
`t
q)t
c
-
d
e
f

How to add decimal point a automatic rounded division PostgreSQL

I have a code who likes like this:
SELECT
a,
b,
c,
a/b/c AS d
FROM data
with results like this:
a | b | c | d
----|----|-----|----
248 4 1 62
----|----|-----|----
99 2 1 49
----|----|-----|----
2077 1 675 3
instead of give 49.50 & 3.07 the division automatically rounds my results and i need numbers with 2 decimal points
I've tried it with the round() argument
ROUND(a/b/c,2) as d
but that doesn't work
I've trying with NUMERIC & DECIMAL but the console gives syntax errors
(f.e syntax error at or near "AS" )
How do I solve this problem?
You need to cast either of the values a,b or c.
try the following
SELECT
a,
b,
c,
a::float/b/c AS d
FROM data

if-statement: how to rewrite in matlab

I'm newby in Matlab. I have took the work code with complex if-statement condition and need to rewrite it. This code should prepare some initial data to solve an optimization task. This if-statement condition looks like:
x=[784.8 959.2 468 572 279 341 139.5 170.5 76.5 93.5 45 55];
a=nchoosek(x,6); % all possible combinations from 6 elements of x
n=length(a);
q=[];
for i=1:n
if( ((a(i,1)==x(1)) & (a(i,2)==x(2))) |
((a(i,1)==x(3)) & (a(i,2)==x(4))) |
((a(i,1)==x(5)) & (a(i,2)==x(6))) |
((a(i,1)==x(7)) & (a(i,2)==x(8))) |
((a(i,2)==x(3)) & (a(i,3)==x(4))) |
((a(i,2)==x(5)) & (a(i,3)==x(6))) |
((a(i,2)==x(7)) & (a(i,3)==x(8))) |
((a(i,3)==x(3)) & (a(i,4)==x(4))) |
((a(i,3)==x(5)) & (a(i,4)==x(6))) |
((a(i,3)==x(7)) & (a(i,4)==x(8))) |
((a(i,3)==x(9)) & (a(i,4)==x(10)))|
((a(i,4)==x(5)) & (a(i,5)==x(6))) |
((a(i,4)==x(7)) & (a(i,5)==x(8))) |
((a(i,4)==x(9)) & (a(i,5)==x(10)))|
((a(i,5)==x(5)) & (a(i,6)==x(6))) |
((a(i,5)==x(7)) & (a(i,6)==x(8))) |
((a(i,5)==x(9)) & (a(i,6)==x(10)) |
((a(i,5)==x(11)) & (a(i,6)==x(12)))))
q(i,:)=a(i,:);
end;
end;
q;
R1=a-q;
R1(~any(R1,2),:) = [];
R1(:, ~any(R1)) = [];
Question: Could anyone give an idea how to rewrite if-statement to improve readability of code?
If I understood you correctly, what the convoluted if statement basically says
If "x(1) x(2)" or "x(3) x(4)" or ... "x(11) x(12)" appears anywhere consecutively in row i
Think about it:
((a(i,1)==x(1)) & (a(i,2)==x(2))) | ((a(i,1)==x(3)) & (a(i,2)==x(4))) |
((a(i,1)==x(5)) & (a(i,2)==x(6))) | ((a(i,1)==x(7)) & (a(i,2)==x(8)))
is no different from:
((a(i,1)==x(1)) & (a(i,2)==x(2))) | ((a(i,1)==x(3)) & (a(i,2)==x(4))) |
((a(i,1)==x(5)) & (a(i,2)==x(6))) | ((a(i,1)==x(7)) & (a(i,2)==x(8))) |
((a(i,1)==x(9)) & (a(i,2)==x(10))) | ((a(i,1)==x(11)) & (a(i,2)==x(12)))
since [x(9) x(10)] and [x(11) x(12)] will never appear at a(i, 1:2), so the line I added is always false and does not change the result of the chain of OR's. But if makes the logic much easier to understand. Same logic applies to a(i,2:3), a(i,3:4)..., complete those cases too and then you will get the first statement I made in this answer.
Then, instead of generating a directly from x, you should generate a from the INDEX of x, i.e. [1:12], as such:
a = nchoosek(1:length(x), 6);
Why? You said x consists of real numbers, and using == on real numbers does not guarantee success, and is a very bad practice in general.
Then, your target becomes:
find if sequence `[1 2]` or `[3 4]` or `[5 6]` ... exists in each line of `a`
which is equivalent to:
find if there is any odd number n followed by n+1
This logic can be represented as:
success = any (mod(a(:,1:end-1), 2) & diff(a,1,2)==1, 2)
Now success(i) will be true/false for the every a(i) that your statement evaluates to the same value. This method is better than your statement because it is very concise, automatically adapts to different sizes of x and does not need to run in a loop.
And if you want to get the actual combination of x values, just do
x(a(i)); % Get the ith permutation of x
x(a); % Get all permutation of x
x(a(success,:)); % Get all permutation of x that satisfy the requirement.
EDIT:
q = a; % q is basically a copy of a
q(~success,:) = 0; % except the `non-success` rows are zero
x(q) - x(a) % suppose q and a store index, this will give you the substraction.

Emacs auctex inserts line breaks after inline math in an ugly way

I got problems with emacs/auctex fill paragraph which insert linebreaks after each $blabla$ block, resulting in a poorly readable code. For exemple, hitting M-q on the following long line:
a $1$ b $2$ c $3$ d $4$ e $5$ f $6$ g $7$ h $8$ i $9$ j $10$ k $11$ l $12$ m $13$ n $14$ o $15$
Gives:
a $1$
b $2$
c $3$
d $4$
e $5$
f $6$ g $7$ h $8$ i $9$ j $10$ k $11$ l $12$ m $13$ n $14$ o $15$
I would like to have something like:
a $1$ b $2$ c $3$ d $4$ e $5$ f $6$ g $7$ h $8$ i $9$ j $10$ k
$11$ l $12$ m $13$ n $14$ o $15
Note: I have the impression that fill-paragraph didn't have this ugly behavior when I didn't use auctex but the built-in latex mode...
Any ideas?
Many thanks!
This can be fixed by changing the AUCTeX user option LaTeX-fill-break-at-separators (see AUCTeX Manual [Filling]).
Hit M-x and type customize-group in the minibuffer, then type LaTeX.
Now you should be able to see the option LaTeX Fill Break At Separators in the newly created buffer. Click on it (expanding it) and uncheck the box next to Closing Inline Math Switches. Don't forget to save.

Simplify Boolean expression with De Morgan's laws

I need to simplify this Boolean expression with De Morgan's laws.
¬c xor (¬b ∨ c)
Could someone help me?
(accidentally made two accounts, so just responding with this one)
Ive found the best way to visualize a logic formula you do not understand is to make a table for it.
In the case of XOR, it represents One variable or another, but not both. So, lets make a table for A XOR B
A | B | Result
T | T | F *1
T | F | T *2
F | T | T *3
F | F | F *4
To generate the smallest possible result from the above table we can first take the most complex result that takes into account each option. Converting each line into a logical statement is fairly easy.
First, throw out anything that results in a False, Then take those that result in true, and convert them into a logical statement separated by 'OR's. In this case, 1 and 4 are false, and 2 and 3 are true. This means we only need to create logical statements for 2 and 3. I think how to do so would be best explained by example
Lets say X, Y, and Z are our variables, and the table gave us the following rows as true:
T | T | F - X & Y & ¬Z
F | T | F - ¬X & Y & ¬Z
F | F | F - ¬X & ¬Y & ¬Z
then to complete, we simply 'OR' them together
(X & Y & ¬Z) V (¬X & Y & ¬Z) V (¬X & ¬Y & ¬Z)
as you can see, where the variable is true, you put the variable directly in, and where it is false, you put a '¬' before the variable. The statement above basically says...
(True when X=T,Y=T,Z=F: False otherwise) OR (True when X=F,Y=T,Z=F: False otherwise) OR (True when X=F,Y=F,Z=F: False otherwise)
So finally bringing it back to our XOR the table rows are...
*2 A & ¬B
*3 ¬A & B
and are combined to be...
(A & ¬B) V (¬A & B)
So, now that you have an explanation of what to do with xor, you can apply this example to your problem, and come up with a logical statement you can use De Morgan's laws on to simplify.
first you have to split up xor into its basic form.
XOR represents A or B where A != B. If you can do that you should have more luck using demorgans on the whole equation