Sublime Text 2 block editing transform - text

how to transform text block to get it in one line
for example
Hello
Sublime
Text
Editor!
should become
Hello Sublime Text Editor!
??
Thanks!

You don't need a regular expression to do this. First, select the text:
Then hit CtrlJ on Windows/Linux or ⌘J on OS X to join the lines together:

This can be done for example by running a Perl regular expression replace searching for (?:[\t ]*\r?\n[\t ]*)+ and using a single space character as replace string.
(?:...) is a non marking group.
[\t ]* finds 0 or more tabs or spaces.
\r?\n finds an optionally present carriage return and a line-feed.
And the + means 1 or more times of the regular expression in the non marking group which in other words finds optionally existing trailing whitespaces at end of a line, the line termination and optionally existing whitespaces at beginning of next line.

Related

Is substitution linking possible in the vi editor?

I am trying to edit with two conditions in the vi.
example text)
hello world!
-apple watermelon
test text
two condition must be met.
start with -
I want to replace only the first letter of a word with a html tag (The first letter may not be the alphabet)
I tried substitution via pipe in vi, but i cant
Expected
hello world!
-<b>a</b>pple <b>w<b>atermelon
test text
You can use
:g/^-/s#\<\(.\)#<b>\1</b>#g
Meaning:
for every line that starts with - (:g/^-/)
substitute
the first character after a break (\<\(.\)) (while capturing it)
replace it with <b>\1</b> (\1 referencing the captured value)
multiple times per line (the g flag, remove this unless :set gdefault? returns nogdefault)

how to remove specific characters in vi or vim editor

I have some txt in vi:
|NC_004718|29751nt|SARS
|NC_045512|29903nt|Severe
|NC_004718|29751nt|SARS
|NC_045512|29903nt|Severe
|NC_004718|29751nt|SARS
now I want to replace remove everything after NC_004718, my expected output is:
NC_004718
NC_045512
NC_004718
NC_045512
NC_004718
How to do it? Thanks.
I would recommend using a substitution with regular expression to match the entire string and to capture what you would like to keep in parentheses. That way you can then replace the entire string with just the match.
:%s/^|\([^|]\+\)|.\+/\1/
To break down what is happening:
% means that you want to apply the command to each line within the file.
s means that you are doing substitution command (on each line). The s command has a syntax of s/<regular expression pattern>/<replacement>/<flags>
The regular eression pattern in the above command is ^|\([^|]\+\)|.\+.
^ means match from the line start.
| matches the character |.
\([^|]\+\) matches all characters except for the character |. Note that the real regular expression is actually ([^|]+), the additional \ characters are there because Vim needs to know that they are intended to be special characters for processing and not exact characters it needs to match. Also note that the parentheses are there to capture the match into a group (see below).
| again matches the actual character |.
.\+ matches all characters until the end of the line. Note that the . is considered special character by default but + still needs a preceding \.
The replacement text is only \1. This denotes that Vim should replace the text with whatever was captured in the first group (i.e. the first set of parentheses).
There are no flags with this command so there is nothing after the last /.
For example,
:g/NC_\d\+/normal! ygnV]p
:g/regex/ to match lines
normal! to execute Normal mode commands
ygn to yank the text previously matched by :g
V to select the whole line
]p or p to replace the line with the match
If you have only lines like those you have shown try:
:%norm xf|D

Search and delete to end of line in vim

I am trying to clean up some code and I am trying to find a good way of achieving the following:
I am a #decent
guy
and I want:
I am a guy
I tried using
:g/#/d
but the whole line gets deleted and I only want to delete until the end of line. What is the best way to achieve this in vim?
Thank you.
That won't because the usage of that command:
:[range]g/pattern/cmd
defaults to range being the whole line, and you are not doing any substitution anyway.
Use:
:%s/#.\+\n//g
instead.
# Matches a literal #.
.\+\n Matches everything until the end of line, and a new line.
// Replaces the entire match with nothing.
With :global you would want something like
:global/#/normal! f#D | join
or
:global/#/substitute/#.*// | join
Try this instead:
:s/ # .*\n/ /
Explanation:
You were using the wrong command, as they may look similar to new users.
:[range]g/VRE/act Globally apply the "act"ion (one letter command) to all lines (in range, default all file) matching the VRE (pattern)
:[range]s/VRE/repl/f Substitute within lines (in range, default current line) the matching VRE (pattern) with the "repl"acement using optional "f"lags
Now about the pattern, I think this candidate cover most cases (all but comments at the beginning of a line and comments without space after pound sign)
# litteral space, then hash tag, then space again
.* dot for any character, star to mean the previous may occur many times or even be absent
$ dollar at end to stay at "end of line", but \n to catch en EOL here
press d + shift 4 or d + $, which means delete to end of the line
d means delete
shift 4 or $ means cursor to end of the line

replace a string with other string in the same line

Vi editor
Input file text format:
'': hello.Code1
'': hello.Code2
'': hello.Code3
Required output text format:
'Code1': hello.Code1
'Code2': hello.Code2
'Code3': hello.Code3
Idea is I have to copy all the values after "." to the single quotes ''. I can use Vi or SED etc. Linux based. or MAC based.
I have more that 2000 lines in the text file
Thanks
You can use a macro in vim. Something like:
/\.^Mly$?'^MPj0
Assuming you're at the start of the first line. Start recording. To record into the q register, hit qq and then:
i) Search for the dot
/\.^M
ii) Go one character to the right, and yank to the end of the line
ly$
iii) Reverse search the quote: '
?'^M
iv) Paste the content and go down a line and move to the start.
Pj0
You can then just repeat the action. Assuming you recorded it in the q register:
2#q
(Note: ^M is <Enter>)
This can be done quite simply with a substitute command and capturing groups. Try the following regex:
:%s/''\(.*\)\.\(.*\)/'\2'\1.\2
This says, Search for quotes '', followed by anything captured into group 1 \(.*\), followed by a literal dot \., followed by anything captured into group 2 \(.*\). This will put
: hello
Into group 1, and
CodeN
into group 2. So then we replace it with group 2 in quotes '\2' followed by group 1 \1, followed by a dot \. and group 2 again \2.
If you put \v at the beginning of the regex, you can get rid of a lot of the backslashes and make it more readable:
:%s/\v''(.*)\.(.*)/'\2'\1.\2
You could also do this with a %normal command. That makes a set of keystrokes be applied to every line in the buffer. I would try this:
:%norm f.ly$0p
This says, On every line, do the following keystrokes :%norm Move forward to a '.' f., move one character to the right l, yank everything to the end of this line y$, move to the beginning of this line 0, and paste what we just yanked p

How to delete the last word from each line in vim?

Please let me know, How I can remove the last word from each line using vim commands?
Before :
abcdef 123
xyz 1256
qwert 2
asdf 159
after :
abcdef
xyz
qwert
asdf
Similarly please let me know how to remove the second word from each line using vim command?
Use the following regex (in ex mode):
%s/\s*\w\+\s*$//
This tells it to find optional whitespace, followed by one or more word characters, followed by optional whitespace, followed by end of line—then replace it with nothing.
The question's been answered already, but here's what I'd more likely end up doing:
Record a macro:
qq to record a macro into register "q"
$ to go to the end of the line
daw to delete a word
q to stop recording
Then select the rest of the lines:
j to go down a line
vG to select to the end of the file
And apply the macro:
:norm #q
Some similar alternatives:
:%norm $daw
qq$dawjq (note the added j) then 999#q to replay the macro many times. (Macro execution stops at the first "error" -- in this case, you'd probably hit the bottom of the file, j would not work, and the macro would stop.)
The key for this is the :substitute command; it is very powerful (and often used in vi / Vim).
You need to come up with a regular expression pattern that matches what you want to delete. For the last word, that's whitespace (\s), one or more times \+ (or any number (*), depending on how you want to treat single-word lines), followed by word characters (\w\+), anchored to the end of the line ($). Note that word has a special meaning in Vim; you may want to use a different atom (e.g. \S). Voila:
:%s/\s\+\w\+$//
For the second word, you can make use of the special \zs and \ze atoms that assert for matches, but do not actually match: Anchored at the start (^), match a word, then start the match for a second one:
:%s/^\w\+\s\+\zs\w\+\s\+//
Soon, you'll also want to reorder things, not just remove them. For that, you need to know capturing groups: \(...\). The text matched by those can then be referred to in the replacement part. For example, to swap the first and second words:
:%s/^\(\w\+\s\+\)\(\w\+\s\+\)/\2\1/
For details, have a look at the help, especially :help :substitute and :help pattern.
To remove the second word from the start of a line, use the following:
:%s/^\(\s*\w\+\s\+\)\w\+\s*/\1/
Update
To treat special characters as part of the word, you have to use the \S (which matches all non-whitespace characters) instead of \w (which matches only word characters [0-9A-Za-z_]). Then, the command would be:
:%s/^\(\s*\S\+\s\+\)\S\+\s*/\1/

Resources