Vi: how to automatically insert spaces - keyboard-shortcuts

I'm trying to write a nice feature for crazy people like me who like there lines to be perfectly aligned.
I often write some file in which the format is "key = value".
Since the key may contain an indeterminate number of character, one have to manually align the "=" symbols which is not cool.
Is there a way to tell vi "when someone type the equal character, then insert as spaces as necessary to go to the column 25, then write an the equal symbol"?
The second step will be to define a shortcut to apply this format to an entire file.
Any help would be appreciated.
Ben.

Map the behavior of = in Insert Mode.
Next code will add spaces until column 24 from current cursor position and will add an equal sign after it. If there were characters after cursor position (suppose in a middle of a word), those characters will be moved after column 25. Add it to your vimrc file and try.
"" If length of the line is more or equal to 24, add an equal sign at the end.
"" Otherwise insert spaces from current position of cursor until column 24
"" and an equal sign, moving characters after it.
function My_align()
let line_len = strlen( getline('.') )
if line_len >= 24
s/$/=/
return
endif
let col_pos = col('.')
exe 's/\%#\(.\|$\)/\=submatch(1) . printf( "%' . (24 - col_pos) . 's%s", " ", "=" )/'
endfunction
inoremap = <Esc>:call My_align()<CR>A
For second step, use the multiple repeats command, check for an equal sign and insert spaces until column 25 just before it. Won't work if equal sign is after column 25 before executing it, but you get the idea.
:g/=/exe 's/=/\=printf( "%' . ( 24 - stridx( getline('.'), "=" ) ) . 's", " " ) . submatch(0)/'

Related

Is there an equivalent of an f-string in Google Sheets?

I am making a portfolio tracker in Google Sheets and wanted to know if there is a way to link the "TICKER" column with the code in the "PRICE" column that is used to pull JSON data from Coin Gecko. I was wondering if there was an f-string like there is in Python where you can insert a variable into the string itself. Ergo, every time the Ticker column is updated the coin id will be updated within the API request string. Essentially, string interpolation
For example:
TICKER PRICE
BTC =importJSON("https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids={BTC}","0.current_price")
You could use CONCATENATE for this:
https://support.google.com/docs/answer/3094123?hl=en
CONCATENATE function
Appends strings to one another.
Sample Usage
CONCATENATE("Welcome", " ", "to", " ", "Sheets!")
CONCATENATE(A1,A2,A3)
CONCATENATE(A2:B7)
Syntax
CONCATENATE(string1, [string2, ...])
string1 - The initial string.
string2 ... - [ OPTIONAL ] - Additional strings to append in sequence.
Notes
When a range with both width and height greater than 1 is specified, cell values are appended across rows rather than down columns. That is, CONCATENATE(A2:B7) is equivalent to CONCATENATE(A2,B2,A3,B3, ... , A7,B7).
See Also
SPLIT: Divides text around a specified character or string, and puts each fragment into a separate cell in the row.
JOIN: Concatenates the elements of one or more one-dimensional arrays using a specified delimiter.

How do I need to change the datatype in a textfile in order to read a String and not a Integer (C++/CLI, OleDb)?

My goal is it to read from a text file. This text file contains different columns and rows for each value. I can read the file as long as I don't change the datatype that windows set on its own. But I do not want the "plz" and "nr" column to be numbers (integers) but a text (String) value because a plz could contain values like "01979" and the nr could contain something like "4a". As a number the starting zero would be lost and this way something like a postcard would never reach its intended destination.
This way I need to change the datatype in a "schema.ini" file. But it doesn't work. I think I make some mistakes and do not follow the tutorial the way I need to do: "Schema.ini File"
Everytime I tried to read a String I got an Exception because it still want to read an Int32-values that I would need to convert into a string.
I did name the file "kunde.txt"
knr|nachname|vorname|plz|ort|strasse|nr
1|Müller|Johan|12345|Muster|Musterstr|1
2|Kummer|Freude|23456|Feeling|Gefühlswelt|4a
Col 0 = knr, 1 = nachname, 2 = vorname, 3 = plz, 4 = ort, 5 = strasse, 6 = nr
con->ConnectionString =
"Provider=Microsoft.JET.OLEDB.4.0;" +
"Data Source=D:/C++/Quellen;" +
"Extended Properties=text";
// ....
meineKunden->CommandText =
"SELECT knr, nachname, vorname, plz, ort, strasse, nr " +
"FROM kunde.txt ";
// ....
String ^ str;
while(reader->Read()){
str += Convert::ToString(reader->GetInt32(0));
str += " ";
str += reader->GetString(3);
str += " ";
str += reader->GetString(6);
str += "\r\n";
}
this->txb_Insert->Text = str;
My schema.ini
[kunde.txt]
ColNameHeader=True
Format=Delimited(|)
3=plz Char Width 5
6=nr Char Width 10
I did try with "Col3" instead of "3". I did use "Text" rather tan "Char", and I did even attempted it without Width. But everytime I got the same failure message. Even if i use 4 or 7 ... since I am not sure how it will be counted in the ini / txt file.
Exception:
System.InvalidCastException: Die angegebene Umwandlung ist ungültig.
bei System.Data.OleDb.ColumnBinding.ValueString()
bei System.Data.OleDb.OleDbDataReader.GetString(Int32 ordinal)
The exception is calls already by str += reader->GetString(3)
If I am correct column 3 contains plz, this way.
Could someone please say what I do understand wrong with the schema.ini file?
Since I could read the file without mistakes as long as I doesn't try to specific change the datatype in some columns the problems need to be with the ini file. At least I think so.
EDIT: I did change my ini-file to:
[kunde.txt]
ColNameHeader=True
Format=Delimited(|)
Col1="knr" Integer
Col2="nachname" Text
Col3="vorname" Text
Col4="plz" Text
Col5="ort" Text
Col6="strasse" Text
Col7="nr" Text
Now it works for "plz" but the exception is called in the last row, when I call the "nr". WTF?
You have to maintain that the way you read data from the file suits the way it's written in .
So if the file in not written by you and you have to use it,try to know if it has a specific structure (for ex: fixed length record delimited fields , fixed length record fixed length fields.. etc) and use a way that suits this structure to read it.
Also try to know how fields of records are written in details because the way you read is the same as you write .

removing blank line when using IF in microsoft word using mail merge

I am actually using conga composer with word template to generate word doc fill with data from my database. There is a section where I have to display from a loop a single line that meet a requirement. This also works fine, but when looping, for each line that do not match the requirement words is leaving a blank line. I don't want blank lines to appear :
Here is my IF condition :
A : {{TableStart:TiersPrestataires}}
{ IF "<<variable>>" = "S1" "<<variable1>> <<variable2>>" ""}
{{TableEnd:TiersPrestataires}}
If in my TiersPrestataires variables I have 5 lines , with the 3rd line that respect the condition, this will print :
A :
<blank_line>
<blank_line>
some text represented by variable 1 some text represented by variable2
<blank_line>
<blank_line>
I want to print :
A:
some text represented by variable 1 some text represented by variable2
and that's all.How can I prevent word to replace the unmet condition with a blank line ? Is there something I can specify in my else condition?
It will be the way you position your brackets.
So I am assuming is another conditional, therefore you would do it like:
A:
{ IF (CONDITION 1) "true
"}{ IF (CONDITION 2) "true
"}{ IF "<<variable>>" = "S1" "<<variable1>> <<variable2>>
"}{ IF (CONDITION 4) "true
"}{ IF (CONDITION 5) "true
"}
If you only want to show content when condition is met then you only need to do one set of " " after the condition as false will be blank anyone.
Doing it this way ensures there are no blank lines left from the IF.
Hope this helps.

Extra blank space between words

Please help me with 2 questions on how to do the GREL expression for:
If there are double spaces between 2 words in a column, how can I eliminate 1 space Example: Robert--Smith to Robert-Smith The minus character equals a blank for illustration
How can I look for an exact word in a text filter.
Thanks!
1°) try transform---> value.replace(" "," ")
Or, simply common transforms ----> collapse consecutive white spaces
2°) Column ---> text filters and enter you word
Or, do column---> Facet---> Customs facet and type : value.contains(" you_word ")
or value.contains(/(yourexactword)/)
This will return a True or False facet
H.
#hpiedcoq is the right answer if you need to have them in GREL. if not you can just use the point and click interface:
for the first question: Select your column and select Edit cells > Common transforms > Collapse consecutive white space
for the second question: select your column > text filter > enter the work you are looking for. You can select case sensitive if you want to take into account upper and lower case in your search.
1.1 transform -- > value.replace(" "," ")
Deletes all double whitespace.
1.2 transform -- > value.trim()
Deletes all double whitespace and deletes whitespaces before and after the string.
1.3 transform -- > value.replace(/\b \b/," ")
Replace with regular expression, deletes only double whitespace between two words.
Text filter > turn on regular expression and use \b.
Text filter with regular expression: \bWord\b = exact word, before and after the word may or may not be a only whitespace.

How to load 2D array from a text(csv) file into Octave?

Consider the following text(csv) file:
1, Some text
2, More text
3, Text with comma, more text
How to load the data into a 2D array in Octave? The number can go into the first column, and all text to the right of the first comma (including other commas) goes into the second text column.
If necessary, I can replace the first comma with a different delimiter character.
AFAIK you cannot put stings of different size into an array. You need to create a so called cell array.
A possible way to read the data from your question stored in a file Test.txt into a cell array is
t1 = textread("Test.txt", "%s", "delimiter", "\n");
for i = 1:length(t1)
j = findstr(t1{i}, ",")(1);
T{i,1} = t1{i}(1:j - 1);
T{i,2} = strtrim(t1{i}(j + 1:end));
end
Now
T{3,1} gives you 3 and
T{3,2} gives you Text with comma, more text.
After many long hours of searching and debugging, here's how I got it to work on Octave 3.2.4. Using | as the delimiter (instead of comma).
The data file now looks like:
1|Some text
2|More text
3|Text with comma, more text
Here's how to call it: data = load_data('data/data_file.csv', NUMBER_OF_LINES);
Limitation: You need to know how many lines you want to get. If you want to get all, then you will need to write a function to count the number of lines in the file in order to initialize the cell_array. It's all very clunky and primitive. So much for "high level languages like Octave".
Note: After the unpleasant exercise of getting this to work, it seems that Octave is not very useful unless you enjoy wasting your time writing code to do the simplest things. Better choices seems to be R, Python, or C#/Java with a Machine Learning or Matrix library.
function all_messages = load_data(filename, NUMBER_OF_LINES)
fid = fopen(filename, "r");
all_messages = cell (NUMBER_OF_LINES, 2 );
counter = 1;
line = fgetl(fid);
while line != -1
separator_index = index(line, '|');
all_messages {counter, 1} = substr(line, 1, separator_index - 1); % Up to the separator
all_messages {counter, 2} = substr(line, separator_index + 1, length(line) - separator_index); % After the separator
counter++;
line = fgetl(fid);
endwhile
fprintf("Processed %i lines.\n", counter -1);
fclose(fid);
end