问题
I have a long list of different names of universities. I'm trying to build a synonym table by collating lines with certain unique keywords; that is, in the file, I'll identify that Harvard is the keyword here:
Harvard Business School|
Harvard College|
Harvard School of Divinity|
and paste them into another file as
Harvard Business School|Harvard College|Harvard School of Divinity|
I have been doing this by searching for the word under the cursor, yanking lines with that word into register, pasting the register into the other file, and using the join command "J" to join the lines together:
[ clear register 'a' ]
0"ay0
[ move cursor to 'Harvard" and yank lines with keyword into register a ]
:g/\<<CTRL-R><CTRL-W>\>/y A
[ move to other screen and paste ]
"ap
[ join lines ]
JJJJJ
This works just fine, but I'd like it to be streamlined. Specifically, I would like to know how to remove newlines from a register so that I don't have to use JJJJ to manually join the lines in the last step. I'd like to search for all lines containing the word under the cursor, copy them into the 'a' register, remove newlines from the 'a' register, then paste the contents of the 'a' register.
Any ideas?
EDIT: I know:
- how to search and replace globally, e.g. %s/\n//g
- how to search and replace 'foo' in a buffer from a word to the contents of a register, e.g. :%s/foo/a/g
- how to search for the contents of a register and replace with 'foo' in a buffer, e.g. :%s/a/bar/g
What I need to know:
- how to search and replace 'foo' with 'bar' from register 'a' to register 'b'
- how to search and replace 'foo' with 'bar' from register 'a' to register 'a'
回答1:
To do it directly with text in register a
you could use this command:
:let @a=substitute(strtrans(@a),'\^@',' ','g')
Then you should get results you want when you paste register a
back in.
回答2:
You don't need to remove newlines from register or press more then one J
: with the following mapping it will be "apgVJ
:
nnoremap <expr> gV "`[".getregtype(v:register)[0]."`]"
This mapping makes you able to select pasted text just after it was pasted. J
on selected region joins all lines in region (unless region has only one line: here J
acts like J
without a selection, joining current line with next). gV
tip can also be used to do substitution: "apgV:s/{pattern}/{replacement}<CR>
is more convenient then using "ap:let @a=substitute(@a, '{pattern}', '{replacement}', '{flags}')
unless you define some smart mapping.
Another tip is that register a
can be cleared using qaq
normal mode sequence: qa
starts recording a new macro and q
immediately stops it leaving empty string in a
register where macro is recorded to.
回答3:
Late to the game, but since everybody else seem to have solved the wrong problem and since Google provided a solution for the problem that I had, I feel obliged to provide my solution.
Accumulate pattern matches as one line in "a
:g/pattern/ norm "apkJ"addu
or using the unnamed register
:g/pattern/ norm pkJddu
It doesn't remove newlines in the register, since it doesn't have to.
If you remove the u
at the end of the norm-code, it will consume already used lines in the file, that would perhaps make the manual processing of the file easier, since you don't have to read stuff that has already been taken care of.
Cite:
[ join lines ] JJJJJ
A question, why not use 5J
, or visual mode, or :%j
(joins all lines in a file)?
Off topic:
"How do you mark text between "" in code blocks,
not to be colour coded?"
回答4:
If I can assume your text file looks like the following:
Harvard Business School|
Harvard College|
Harvard School of Divinity|
I would use
:%s/\n//g
to globally search and replace all \n new lines. sometimes, you may need to use ^M (hit control+v and ENTER) in place of \n.
I'm afraid your list may be more complicated then this, however, in which case my solution will be much too 'simple'
来源:https://stackoverflow.com/questions/6228079/remove-newlines-from-a-register-in-vim