Search in Vim above a line [duplicate] - search

This question already has answers here:
Search only between specific line numbers?
(7 answers)
Closed 8 years ago.
Is there anyway in vim to search for a word token above a certain line? For example I have a 12,000 line log file and I'm wondering if I can find a string sequence above a certain line number. The string sequence reoccurs a painful amount of times to just keep iterating through, so I would like to limit my search to above a specified line. I suppose I could just delete everything afterwards and then search, but that seems like a pain. I'm relatively new to vim so I don't have all the keywords down yet.

See :help search-range.
You’d probably use \%<l for this. If, in your 12,000-line file, you wanted to search for the string “test” only above line 4,800, you could use
/\%<4800ltest
\%<l \%>l are used for “above” and “below” search ranges. The one above (\%<l) will look for matches above the specified line number; the other (\%>l) will look for matches below the line number.

Related

Remove until end of the line after the same character from different locations in multiple lines [duplicate]

This question already has answers here:
Delete all characters after "." in each line
(5 answers)
Closed 17 days ago.
I want to remove anything from : to the end of line in all of the following lines in Vim:
key1: A
key2_long: B
key3_longerrr: C
key: D
So the result would be:
key1
key2_long
key3_longerrr
key
For a single line I use f:d$. What are better ways to do this for all lines, wihtout repeating f:d$ for all lines?
:%s/:.*//g
It searches and replaces what matches the regex between the first and second / with what is between the second and third in all the file.
you can find more on this in the documentation, look for substitute
If you want to stick with f:d$ (which should be f:D), you can use :normal for a one-off macro:
:,+3normal f:D
or:
vjjj:normal f:D
See :help :range and :help :normal.

How to change part of a variable/function name in Vim [duplicate]

This question already has answers here:
Changing parts of CamelCase words in vim
(2 answers)
Closed 1 year ago.
I usually have variables/function names like:
loginUser()
registerUser()
saveUserData()
checkBaggageTag()
So, as you see, some of them repeat the User word, and so when I copy paste and need to rename one of them, I do it this way: loginUser() I'll do cw and write registerUser.
Here is the question, is there a way to do cw but only change the word up to the first Uppercase letter? (U in User for example) so I can avoid retyping the word User? Of course, I can always do vtUc and then type register but you know, that's 4 keys...is there a way to it in fewer?
Thanks for your help ^_^
Using command t: ctU (change till the next U).
Using search: c/UEnter
Searching to the next upper-case latin letter (not only U): c/\uEnter
There are definitely fancy plugins for this (smth. about "motion" and "case"). But on most occasions, IMO, one is able to count, e.g. 5s or 8s etc.

vim: Marks getting deleted/lost [duplicate]

This question already has answers here:
Make vim keep mark when I delete the line the mark is on
(2 answers)
Closed 6 years ago.
I've tried to find an answer to this on the googles, but been unsuccessful. The problem is this:
In vim I delete a line that contained a mark; so I guess the mark is also deleted. Now I can't jump back to that location any more. I'm coding so there is a lot of line deleting going on. It's a pain having to manually find the place that I set the mark again.
Is there a way around this? I want vim to jump to aprox the same location where the mark used to be. Either the same line number, or the closest guess.
You can try `. which is a position of the last change occurred in the current buffer. Then you can mark it again.

Vim: Convert hard wrap to soft wrap [duplicate]

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.

Multi-line Highlight in Vim

I am currently writing a plugin in Vim that needs to highlight arbitrary lines in a file
at the same time.
My approach so far has been implementing match with the line number to highlight it, but the problem is that I would need to add | for every other line in a file and append that information and then call it every time the window gets redrawn.
There is another problem with match in my case, and that is that a line that may not have any whitespace would not look highlighted (match only highlights existing text/whitespace).
So even if I had match rewrite the window and highlighting all the lines I need, from a user's perspective this wouldn't be to useful if the highlighting doesn't show anything if there is no whitespace/text.
Can I get any suggestions in how to effectively show/display/highlight (I'm open to different implementations to solve my problem) arbitrary lines in a file at the same time regardless of amount of text or whitespace?
Edit: My main request is to be able to highlight lines by line number not by regex
matching. So any solution should need to be flexible enough to accept a Line number to match.
Edit: signs is the answer to my problem, and I found this tutorial the best way to grasp and implement what I needed: http://htmlpreview.github.io/?https://github.com/runpaint/vim-recipes/blob/master/text/07_navigation/12_bookmarking_lines_with_visible_markers.html
I would use region rather than match. Here is part of my manuscript syntax file that highlights speech:
:syntax region msSpeech start=/"/ end=/"\|\n\n/
:highlight msSpeech guifg=#000088
It starts with a double quote and ends with another double quote or the end of the paragraph. It will highlight multiple lines if need be.

Resources