从vim中的寄存器中删除换行符? [英] remove newlines from a register in vim?

查看:55
本文介绍了从vim中的寄存器中删除换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一长串不同的大学名称.我正在尝试通过整理具有某些唯一关键字的行来构建同义词表;也就是说,在文件中,我将确定哈佛是此处的关键字:

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|

我一直在通过搜索光标下的单词,将带有该单词的行拖入寄存器,将寄存器粘贴到另一个文件中,然后使用连接命令J"将这些行连接在一起来做到这一点:

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

这工作得很好,但我希望它被简化.具体来说,我想知道如何从寄存器中删除换行符,这样我就不必在最后一步中使用 JJJJ 手动加入行.我想搜索包含光标下单词的所有行,将它们复制到a"寄存器中,从a"寄存器中删除换行符,然后粘贴a"寄存器的内容.

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.

有什么想法吗?

我知道:

  • 如何全局搜索和替换,例如%s/\n//g
  • 如何在缓冲区中搜索和替换 'foo' 从单词到寄存器的内容,例如:%s/foo/a/g
  • 如何在缓冲区中搜索寄存器的内容并替换为 'foo',例如:%s/a/bar/g

我需要知道的:

  • 如何从寄存器 'a' 到寄存器 'b' 中搜索和替换 'foo' 为 'bar'
  • 如何从寄存器 'a' 到寄存器 'a' 中搜索和替换 'foo' 为 'bar'

推荐答案

要直接使用寄存器 a 中的文本进行操作,您可以使用以下命令:

To do it directly with text in register a you could use this command:

:let @a=substitute(strtrans(@a),'\^@',' ','g')

然后,当您将寄存器 a 粘贴回时,您应该会得到想要的结果.

Then you should get results you want when you paste register a back in.

这篇关于从vim中的寄存器中删除换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆