Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
There is a similar question on moving the searched string to the end of the file or to the beginning of file
:g /<pattern>/m0 -> beginning of the file.
:g /<pattern>/m$ -> beginning of the file.
Is there way to search and move to the beginning of a line. Any tutorial link to learn this ?
Thank you
You can do movement inside the current line with :substitute alone, no need for :g. You need to augment the <pattern> so that the text before / after is also captured (in \(...\) groups). This lets you "rearrange" the matches in the replacement part.
Move (first, for last use .* instead of .\{-}) match to beginning:
:%s/^\(.\{-}\)\(<pattern>\)/\2\1/
Move to end:
:%s/\(<pattern>\)\(.*\)$/\2\1/
The syntax of the :g command allows you to put any Ex command where your examples have m0 and m$. The :move command (short form :m) can take any number as an argument, and there are other possibilities.
If you want to move a line after Line 17, then use :m17. If you want to move it before Line 17, then use :m16 or :m17- (short for :m17-1). I often find it useful to mark a line (ma in Normal mode or :mark a) and then
:g/<pattern>/m'a-
:help :global
:help :move
:help {address}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a text file of words, one per line, called A.
I have another text file B.
How can I find all lines in B what have at least one of the words from A as a prefix?
I was hoping to be able to do this from the command line maybe using grep but any other command line solution would be great too.
For example, if A is
apple
bob
cheese
and B is
aple
bob123
ches
I would like the line bob123 to be returned.
One approach uses bash's process substitution and sed to add a regular expression beginning-of-line ^ anchor to each line of A, and then tells grep to use it as a list of regular expressions to search for:
$ grep -f <(sed 's/^/^/' a.txt) b.txt
bob123
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
If 3549,2152,4701 in first column then remove the entry:
sample data:
18106|1.0.4.0/22
3549|1.0.10.0/24
5413|1.0.0.0/16
2152|1.4.0.0/16
3549|1.0.8.0/22
4701|1.0.0.0/8
Expedted output:
18106|1.0.4.0/22
5413|1.0.0.0/16
How to achieve this?
For your pattern to match only on the first field you have to anchor the expression to the start of the line:
grep -v -E '^(3549|2152|4701)\|'
The ^ marks the beginning of the line (and $ would mark the end of the line)
The -E activates enhanced regular expressions so you don't have to \ escape pipes and parentheses, and the -v inverses the search (returning only lines that do not match).
The ^ matches the start of the line then parentheses with the pipe symbol marks alternatives (3549, 2152 or 4701), and \| stands for the pipe symbol itself which your first field ends with, and needs to be escaped by the backslash so it's not treated as another alternation.
Be careful to use single quotes around it because otherwise the shell itself will interpret some of the special characters.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
In vim, when I press p, text was pasted to after the cursor? How can I change this to paste the text before the cursor?
My problem comes from this situation: there are hundreds of lines propositions in a text file. I have to check manually if they are right. If a certain line is right, I should put a mark * before that line, so I hope I can paste text before the cursor.
P
Note that P is capitalized.
Typing :help p gives more info about both p and P.
Try capital P.
Lowercase p puts after the cursor.
Capital P puts before the cursor.
Here is an alternative way that I think fits your usecase better:
I*<esc> insert a * at the beginning of the line
j move to next line
. repeat last edit if the line is "right"
After that, your work id going to look like jjjj.j.jjj.jjj.j.j.jjjjjjj.jj.. You are free to use the <down> key, if you like it better.
Also, but that might be cheating, if the "right" lines exhibit some sort of pattern the :global command would help:
:%g/pattern/norm I*
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Unwrap text in vim
How to convert hard wrap into soft wrap in a text file, using vim or (if simpler) some other standard GNU/Linux tool? And this while preserving paragraphs as such. It would be easy to remove all line breaks, but not serviceable.
By hard wrap I mean a document where each line ends at most at (for example) column 80.
I noticed that the program aquamacs has a function to do this, but I don't know what its output looks like, and it is Mac OS X only.
You can do this:
:%norm vipJ
It will unwrap all the paragraphs in you text. However, if you want to do it manually, simply do ipJ inside visual mode on each paragraph you want to unwrap.
I also found another way to achieve this
:g/^\s*\n.*\S$/+norm vipJ
which means:
:g "Execute command when pattern matches
Pattern:
^\s*\n "A line with only spaces or tabs (or none)
\n.*\S$ "A line with anything but ending with a non-space character
Command:
+norm vipJ "Join the lines in the paragraph
Please note that you'll need an empty line before the first paragraph too.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need a macro/function that searches the current document, starting at the top, for a specific pattern. Stopping at the first line that isn't commented out.
For example in this vim script:
"This is the first line
"This is the third line
if exists("did_something")
It would stop at if line, return true if it found the pattern and false otherwise.
All help appreciated!
Let's say your pattern is the letter 'e'.
To search for a line that is not commented out, but contains an e:
/^\%(\s*"\)\#!.*e
This uses a negative lookahead (\#!) to confirm that the start of the line (^) isn't followed by a the beginning of a comment (whitespace and then a double quote \s*") but is followed by an 'e' (.*e). The \%( and \) makes the enclosed pattern an atom that other operators (like negative lookahead) can operate on as a unit.
To run a command on matching lines, use :g
:g/^\%(\s*"\)\#!.*e/echo "found one"
To see if the current line matches, use match()
:echo match( getline(line('.')), '^\%(\s*"\)\#!.*e' ) >= 0 ? 'true' : 'false'
The regex is pretty much always the same, the question is what do you want to do with matching lines?