^A and ^B separated values - linux

I have a file that has contents that are separated by ^B and ^A values. This file has an extension .tsv but the values are separated by ^A and ^B. I want to add more lines to this file with the same delimited values but I'm not sure what the values of ^A and ^B are.
I read around and I think ^A can also be represented as \001
I'm not sure how to recreate the balues ^A and ^B. I tried entering contrl + A and control + B but I dont get the same outputs. Also tried \001 but I still cant seem to recreate it.

^A and ^B are caret notation for the ASCII control characters 1 and 2 respectively.
There is no universal way of typing these. Here are some options:
Bash:
echo $'foo\001bar\002baz' >> file
This appends "foo^Abar^Bbaz" to the file.
Vim: Ctrl+V Ctrl+A
In insert mode, this inserts a ^A (and similarly for B)
Emacs: Ctrl+Q Ctrl+A
This inserts a ^A (and similarly for B)
You can also simply copy-paste these characters in any graphical editor. However, if you use a terminal based text editor, you have to use the editor's keyboard controls instead of the mouse to copy/paste them.

You can use the "tr" command to make a temporary file, edit it and then return it to the original. Requires you to have two characters not in the original data (~ and % in this example).
First translate to get rid of the offending characters:
tr "\001\002" "~%" < original-File > temp-file
Edit "temp-file" as you need to.
Translate back to the original:
tr "~%" "\001\002" < temp-file > modified-file
You can check the original and check your work with:
hexdump -C file

Related

vim - surround text with function call

I want to wrap some code :
myObj.text;
with a function call where the code is passed as an argument.
console.log(myObj.text);
I've thought about using surround.vim to do that but didn't manage to do it.
Any idea if it's possible ? I
With Surround in normal mode:
ysiwfconsole.log<CR>
With Surround in visual mode:
Sfconsole.log<CR>
Without Surround in normal mode:
ciwconsole.log(<C-r>")<Esc>
Without Surround in visual mode:
cconsole.log(<C-r>")<Esc>
But that's not very scalable. A mapping would certainly be more useful since you will almost certainly need to do it often:
xnoremap <key> cconsole.log(<C-r>")<Esc>
nnoremap <key> ciwconsole.log(<C-r>")<Esc>
which brings us back to Surround, which already does that—and more—very elegantly.
I know and use two different ways to accomplish this:
Variant 1:
Select the text you want to wrap in visual mode (hit v followed by whatever movements are appropriate).
Replace that text by hitting c, then type your function call console.log(). (The old text is not gone, it's just moved into a register, from where it will be promptly retrieved in step 3.) Hit <esc> while you are behind the closing parenthese, that should leave you on the ) character.
Paste the replaced text into the parentheses by hitting P (this inserts before the character you are currently on, so right between the ( and the )).
The entire sequence is v<movement>c<functionName>()<esc>P.
Variant 2:
Alternatively to leaving insert mode and pasting from normal mode, you can just as well paste directly from insertion mode by hitting <ctrl>R followed by ".
The entire sequence is v<movement>c<functionName>(<ctrl>R")<esc>.
You can use substitution instruction combined with visual mode
To change bar to foo(bar):
press v and select text you want (plus one more character) to surround with function call (^v$ will select whole text on current line including the newline character at the end)
type :s/\%V.*\%V/foo\(&\)/<CR>
Explanation:
s/a/b/g means 'substitute first match of a with b on current line'
\%V.*\%V matches visual selection without last character
& means 'matched text' (bar in this case)
foo\(&\) gives 'matched text surrounded with foo(...) '
<CR> means 'press enter'
Notes
For this to work you have to visually select also next character after bar (^v$ selects also the newline character at the end, so it's fine)
might be some problems with multiline selections, haven't checked it yet
when I press : in visual mode, it puts '<,'> in command line, but that doesn't interfere with rest of the command (it even prevents substitution, when selected text appears also somewhere earlier on current line) - :'<,'>s/... still works

Remove/Add Line Breaks after Specific String using Sublime Text

Using Sublime Text 2 - Is it possible to insert a line break/text return after a specific String in a text file e.g. by using the Find ‣ Replace tool?
(Bonus question: Is it possible to remove all line breaks after a specific String)
Here's how you'd do it on a Mac:
Command+F > type string > Control+Command+G > ESC > Right Arrow > line break
and Windows/Linux (untested):
Control+F > type string > Alt+F3 > ESC > Right Arrow > line break
The important part being Control+Command+G to select all matches.
Once you've selected the text you're looking for, you can use the provided multiple cursors to do whatever text manipulation you want.
Protip: you can manually instantiate multiple cursors by using Command+click (or Control+click) to achieve similar results.
Using the Find - Replace tool, this can be accomplished in two different ways:
Click in the Replace field and press Ctrl + Enter to insert a newline (the field should resize but it doesn't, so it is hard to see the newline inserted).
Inside the Find - Replace tool, activate the S&R regex mode (first icon on the left .*, keyboard shortcut is Alt + Ctrl/Cmd + R to activate/deactivate it).
Type \n in the Replace field wherever you want to insert a newline.
Both solutions also work if you want to find newlines, just do it in the Find field.
Edit->Lines->Join Line (Ctrl+J)
You should probably use multiple cursors. See the unofficial documentation, or this nice tutorial. Here's some brief instructions to set you on your way:
Put the cursor on the string of interest.
Type Command+D (Mac) or Control+D (Windows/Linux) to select the current instance of the string.
Type Command+D (Mac) or Control+D (Windows/Linux) to select successive instances of the string.
Alternately, type Control+Command+G (Mac) or Control+Command+G to select all instances of your string.
Now you have multiple cursors, so insert or remove your newline as you please.
(type esc to exit multiple cursor mode.)
Have fun!

How to add text at the end of each line in Vim?

In Vim, I have the following text:
key => value1
key => value2
key => value1111
key => value12
key => value1122222
I would like to add "," at the end of each line. The previous text will become the following:
key => value1,
key => value2,
key => value1111,
key => value12,
key => value1122222,
Does anyone know how to do this? Is it possible to use visual block mode to accomplish this?
This will do it to every line in the file:
:%s/$/,/
If you want to do a subset of lines instead of the whole file, you can specify them in place of the %.
One way is to do a visual select and then type the :. It will fill in :'<,'> for you, then you type the rest of it (Notice you only need to add s/$/,/)
:'<,'>s/$/,/
There is in fact a way to do this using Visual block mode. Simply pressing $A in Visual block mode appends to the end of all lines in the selection. The appended text will appear on all lines as soon as you press Esc.
So this is a possible solution:
vip<C-V>$A,<Esc>
That is, in Normal mode, Visual select a paragraph vip, switch to Visual block mode CTRLV, append to all lines $A a comma ,, then press Esc to confirm.
The documentation is at :h v_b_A. There is even an illustration of how it works in the examples section: :h v_b_A_example.
Another solution, using another great feature:
:'<,'>norm A,
See :help :normal.
ex mode is easiest:
:%s/$/,
: - enter command mode
% - for every line
s/ - substitute
$ - the end of the line
/ - and change it to
, - a comma
The substitute command can be applied to a visual selection. Make a visual block over the lines that you want to change, and type :, and notice that the command-line is initialized like this: :'<,'>. This means that the substitute command will operate on the visual selection, like so:
:'<,'>s/$/,/
And this is a substitution that should work for your example, assuming that you really want the comma at the end of each line as you've mentioned. If there are trailing spaces, then you may need to adjust the command accordingly:
:'<,'>s/\s*$/,/
This will replace any amount of whitespace preceding the end of the line with a comma, effectively removing trailing whitespace.
The same commands can operate on a range of lines, e.g. for the next 5 lines: :,+5s/$/,/, or for the entire buffer: :%s/$/,/.
:%s/$/,/g
$ matches end of line
If you want to add ',' at end of the lines starting with 'key', use:
:%s/key.*$/&,
I have <M-DOWN>(alt down arrow) mapped to <DOWN>. so that I can repeat the last command on a series of lines very quickly. with this mapping I can:
A,<ESC>
And then hold alt while pressing down repeatedly to append the comma to the end of each line.
This works well for me because it allows very good control over what lines do and do not get the change.
(I also have the other arrows mapped similarly to allow for easy repeating of .)
Here's the mapping line to paste into your vimrc:
map <M-DOWN> <DOWN>.
Following Macro can also be used to accomplish your task.
qqA,^[0jq4#q

How do I use vim registers?

I only know of one instance using registers is via CtrlR* whereby I paste text from a clipboard.
What are other uses of registers? How to use them?
Everything you know about VI registers (let's focus on vi 7.2) -- share with us.
Registers in Vim let you run actions or commands on text stored within them. To access a register, you type "a before a command, where a is the name of a register. If you want to copy the current line into register k, you can type
"kyy
Or you can append to a register by using a capital letter
"Kyy
You can then move through the document and paste it elsewhere using
"kp
To paste from system clipboard on Linux
"+p
To paste from system clipboard on Windows (or from "mouse highlight" clipboard on Linux)
"*p
To access all currently defined registers type
:reg
I was pleased when I discovered the 0 register. If you yank text without assigning it to a particular register, then it will be assigned to the 0 register, as well as being saved in the default " register. The difference between the 0 and " registers is that 0 is only populated with yanked text, whereas the default register is also populated with text deleted using d/D/x/X/c/C/s/S commands.
I find this useful when I want to copy some text, delete something and replace it with the copied text. The following steps illustrate an example:
Yank the text you want to copy with y[motion] - this text is saved in " and 0 registers
Delete the text you want to replace with d[motion] - this text is saved in " register
Paste the yanked text with "0p
where " is the command to use a register for the next command.
On the final step, if you were to paste from the default register (with p), it would use the text that you had just deleted (probably not what you intended).
Note that p or P pastes from the default register. The longhand equivalent would be ""p (or ""P) and "0 holds the last yank, "1holds the last delete or change.
For more info see :help registers.
One of my favorite parts about registers is using them as macros!
Let's say you are dealing with a tab-delimited value file as such:
ID Df %Dev Lambda
1 0 0.000000 0.313682
2 1 0.023113 0.304332
3 1 0.044869 0.295261
4 1 0.065347 0.286460
5 1 0.084623 0.277922
6 1 0.102767 0.269638
7 1 0.119845 0.261601
Now you decide that you need to add a percentage sign at the end of the %Dev field (starting from 2nd line). We'll make a simple macro in the (arbitrarily selected) m register as follows:
Press: qm: To start recording macro under m register.
EE: Go to the end of the 3rd column.
a: Insert mode to append to the end of this column.
%: Type the percent sign we want to add.
<ESC>: Get back into command mode.
j0: Go to beginning of next line.
q: Stop recording macro
We can now just type #m to run this macro on the current line. Furthermore, we can type ## to repeat, or 100#m to do this 100 times! Life's looking pretty good.
At this point you should be saying, "But what does this have to do with registers?"
Excellent point. Let's investigate what is in the contents of the m register by typing "mp. We then get the following:
EEa%<ESC>j0
At first this looks like you accidentally opened a binary file in notepad, but upon second glance, it's the exact sequence of characters in our macro!
You are a curious person, so let's do something interesting and edit this line of text to insert a ! instead of boring old %.
EEa!<ESC>j0
Then let's yank this into the n register by typing B"nyE. Then, just for kicks, let's run the n macro on a line of our data using #n....
It added a !.
Essentially, running a macro is like pressing the exact sequence of keys in that macro's register. If that isn't a cool register trick, I'll eat my hat.
Other useful registers:
"* or "+ - the contents of the system clipboard
"/ - last search command
": - last command-line command.
Note with vim macros, you can edit them, since they are just a list of the keystrokes used when recording the macro. So you can write to a text file the macro (using "ap to write macro a) and edit them, and load them into a register with "ay$. Nice way of storing useful macros.
The black hole register _ is the /dev/null of registers.
I use it in my vimrc to allow deleting single characters without updating the default register:
noremap x "_x
and to paste in visual mode without updating the default register:
vnoremap p "_dP
If you ever want to paste the contents of the register in an ex-mode command, hit <C-r><registerletter>.
Why would you use this? I wanted to do a search and replace for a longish string, so I selected it in visual mode, started typing out the search/replace expression :%s/[PASTE YANKED PHRASE]//g and went on my day.
If you only want to paste a single word in ex mode, can make sure the cursor is on it before entering ex mode, and then hit <C-r><C-w> when in ex mode to paste the word.
To make it more convenient :
cnoremap <c-g> <c-r>"
cnoremap <C-Right> <c-r><c-w>
A cool trick is to use "1p to paste the last delete/change (, and then use . to repeatedly to paste the subsequent deletes. In other words, "1p... is basically equivalent to "1p"2p"3p"4p.
You can use this to reverse-order a handful of lines:
dddddddddd"1p....
I think the secret guru register is the expression = register. It can be used for creative mappings.
:inoremap \d The current date <c-r>=system("date")<cr>
You can use it in conjunction with your system as above or get responses from custom VimL functions etc.
or just ad hoc stuff like
<c-r>=35+7<cr>
q5 records edits into register 5 (next q stops recording)
:reg show all registers and any contents in them
#5 execute register 5 macro (recorded edits)
From vim's help page:
CTRL-R {0-9a-z"%#:-=.} *c_CTRL-R* *c_<C-R>*
Insert the contents of a numbered or named register. Between
typing CTRL-R and the second character '"' will be displayed
<...snip...>
Special registers:
'"' the unnamed register, containing the text of
the last delete or yank
'%' the current file name
'#' the alternate file name
'*' the clipboard contents (X11: primary selection)
'+' the clipboard contents
'/' the last search pattern
':' the last command-line
'-' the last small (less than a line) delete
'.' the last inserted text
*c_CTRL-R_=*
'=' the expression register: you are prompted to
enter an expression (see |expression|)
(doesn't work at the expression prompt; some
things such as changing the buffer or current
window are not allowed to avoid side effects)
When the result is a |List| the items are used
as lines. They can have line breaks inside
too.
When the result is a Float it's automatically
converted to a String.
See |registers| about registers. {not in Vi}
<...snip...>
I use the default register to grep for text in my vim window without having to reach for the mouse.
yank text
:!grep "<CTRL-R>0"<CR>
Use registers in commands with #. E.g.:
echo #a
echo #0
echo #+
Set them in command:
let #a = 'abc'
Now "ap will paste abc.
One overlooked register is the '.' dot register which contains the last inserted text no matter how it was inserted eg ct] (change till ]). Then you realise you need to insert it elsewhere but can't use the dot repeat method.
:reg .
:%s/fred/<C-R>./
A big source of confusion is the default register ". It is important to know the way it works. It is much better if the default register is avoided most of the times. The explanation from the Vim documentation:
Vim fills this register with text deleted with the "d", "c", "s", "x" commands
or copied with the yank "y" command, regardless of whether or not a specific
register was used (e.g. "xdd). This is like the unnamed register is pointing
to the last used register.
So the default register is actually a pointer to the last used register. When you delete, or yank something this register is going to point to other registers. You can test that by checking the registers. There is always another register that is exactly the same as the default register: the yank register ("0) , the first delete register("1) , small delete register("-) or any other register that was used to delete or yank.
The only exception is the black hole register. Vim doc says:
An exception is the '_' register: "_dd does not store the deleted text in any
register.
Usually you are much better off by using directly: "0, "- and "1-"9 default registers or named registers.
My favorite register is the ':' register. Running #: in Normal mode allows me to repeat the previously ran ex command.
So we can verify some commands in with :MY_commandXXXXX then put MY_commandXXXXX in vimrc
My friend Brian wrote a comprehensive article on this. I think it is a great intro to how to use topics. https://www.brianstorti.com/vim-registers/
My favorite feature is the ability to append into registers by using capital letters. For example, say you want to move a subset of imports from buffer X to buffer Y.
Go to line x1 in buffer X.
Type "ayy to replace register a with the content of line x1.
Go to line x5.
Type "Ayy (capital A) to append line x5 at the end of register a.
Go to buffer Y and type "ap to paste
<content of line x1>
<content of line x5>

How to add line numbers to range of lines in Vim?

How can I add line numbers to a range of lines in a file opened in Vim? Not as in :set nu—this just displays line numbers—but actually have them be prepended to each line in the file?
With
:%s/^/\=line('.')/
EDIT: to sum up the comments.
This command can be tweaked as much as you want.
Let's say you want to add numbers in front of lines from a visual selection (V + move), and you want the numbering to start at 42.
:'<,'>s/^/\=(line('.')-line("'<")+42)/
If you want to add a string between the number and the old text from the line, just concatenate (with . in VimL) it to the number-expression:
:'<,'>s/^/\=(line('.')-line("'<")+42).' --> '/
If you need this to sort as text, you may want to zero pad the results, which can be done using printf for 0001, 0002 ... instead of 1, 2... eg:
:%s/^/\=printf('%04d', line('.'))/
Anyway, if you want more information, just open vim help: :h :s and follow the links (|subreplace-special|, ..., |submatch()|)
cat -n adds line numbers to its input. You can pipe the current file to cat -n and replace the current buffer with what it prints to stdout. Fortunately this convoluted solution is less than 10 characters in vim:
:%!cat -n
Or, if you want just a subselection, visually select the area, and type this:
:!cat -n
That will automatically put the visual selection markers in, and will look like this after you've typed it:
:'<,'>!cat -n
In order to erase the line numbers, I recommend using control-v, which will allow you to visually select a rectangle, you can then delete that rectangle with x.
On a GNU system: with the external nl binary:
:%!nl
With Unix-like environment, you can use cat or awk to generate a line number easily, because vim has a friendly interface with shell, so everything work in vim as well as it does in shell.
From Vim Tip28:
:%!cat -n
or
:%!awk '{print NR,$0}'
But, if you use vim in MS-DOS, of win9x, win2000, you loss these toolkit.
here is a very simple way to archive this only by vim:
fu! LineIt()
exe ":s/^/".line(".")."/"
endf
Or, a sequence composed with alphabet is as easy as above:
exe "s/^/".nr2char(line("."))."/"
You can also use a subst:
:g/^/exe ":s/^/".line(".")."^I/"
You can also only want to print the lines without adding them to the file:
"Sometimes it could be useful especially be editing large source files to print the line numbers out on paper.
To do so you can use the option :set printoptions=number:y to activate and :set printoptions=number:n to deactivate this feature.
If the line number should be printed always, place the line set printoptions=number:y in the vimrc."
First, you can remove the existing line numbers if you need to:
:%s/^[0-9]*//
Then, you can add line numbers. NR refers to the current line number starting at one, so you can do some math on it to get the numbering you want. The following command gives you four digit line numbers:
:%!awk '{print 1000+NR*10,$0}'
The "VisIncr" plugin is good for inserting columns of incrementing numbers in general (or letters, dates, roman numerals etc.). You can control the number format, padding, and so on. So insert a "1" in front of every line (via :s or :g or visual-block insert), highlight that column in visual-block mode, and run one of the commands from the plugin.
If someone wants to put a tab (or some spaces) after inserting the line numbers using the this excellent answer, here's a way. After going into the escape mode, do:
:%s/^/\=line('.').' '/
^ means beginning of a line and %s is the directive for substitution. So, we say that put a line number at the beginning of each line and add 4 spaces to it and then put whatever was the contents of the line before the substitution, and do this for all lines in the file.
This will automatically substitute it. Alternatively, if you want the command to ask for confirmation from you, then do:
:%s/^/\=line('.').' '/igc
P.S: power of vim :)
The best reply is done in a duplicate question.
In summary:
with CTRL-V then G I 0 You can insert a column of zero.
Then select the whole column and increment:
CTRL-V g CTRL-A
See also: https://vim.fandom.com/wiki/Making_a_list_of_numbers#Incrementing_selected_numbers

Resources