vim 如何快速创建调试消息

发布于 2024-09-06 20:27:04 字数 337 浏览 4 评论 0原文

我正在 Rails 上使用 ruby​​,但这对于这个问题来说并不重要。假设我有一个这样的声明,

error = 'this is an error message'

我注意到我最终做了很多这样的

error = 'this is an error message'
puts "error = #{error.inspect}"

事情,我确信可以编写一个宏,它将完成最左边=左侧的工作,然后与模板一起创建另一行如上所示。

我在 mac 上使用 mvim。关于我应该从哪里开始寻找开发我想要的东西的任何指示。

I am using ruby on rails but that does not matter much for this question. Let's say that i have a statement like this

error = 'this is an error message'

I have noticed that I end up doing this a lot

error = 'this is an error message'
puts "error = #{error.inspect}"

I am sure a macro can be written which would take the work on the left hand side of left most = and then create another line along with template shown above.

I am using mvim on mac. Any pointer in terms of where I should start to look for developing what I want.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

趴在窗边数星星i 2024-09-13 20:27:04

我录制了一个简单的宏来执行您的示例。要记录宏,请键入 q,后跟要放入宏的寄存器(约定要求 qq)。要播放宏,请输入 @,然后进行宏注册。您可以在 :help reporting 处查看此

宏。要编写宏,请使用以下命令(这里是寄存器中的外观)

^yEoputs "error = #{^Op.inspect}"^[

^ 移动到第一个非空白 yE行的字符

被拉到空格分隔的单词的末尾。

o 让您在下一行进入插入模式

puts "error = #{ 是您键入的文本

^Octrl+O(大写字母 o) - 这允许下一个且仅下一个命令在命令模式下运行,即...

p放置被拉出的单词,运行此命令后您仍处于插入模式

.inspect}" 是您键入的文本,最后...

^[ 是 < kbd>Esc

I recorded a simple macro that does your sample. To record a macro type q followed by what register you want the macro to be put in (convention calls for qq). To play the macro type @ then the macro register. You can view this at :help recording

To write the macro, use the following commands (and here is how is should look in the register)

^yEoputs "error = #{^Op.inspect}"^[

^ moves to the first non whitespace character of the line

yE yanks to the end of the space separated word.

o Puts you in insert mode on the next line

puts "error = #{ is the text that you type out

^O is ctrl+O (capital letter o) - this allows the next, and only the next command to be run in command mode, which is...

p Puts the yanked word, after this command is run you're still in insert mode

.inspect}" is the text that you type and finally...

^[ is Esc

哭了丶谁疼 2024-09-13 20:27:04

我会选择:

nnoremap µ :s/^\s*\(\k\+\)\s*=.*/&\rputs "\1 = #{\1.inspect}"/<cr>

:s 展示了完成工作以及匹配分配的变量(如果有)的优势。使用 ywp 等经典命令执行相同的操作会更加麻烦。

如果模板变得更加复杂,我们可以依赖模板文件扩展器,只要它们能够轻松调用 viml 函数,例如 matchstr()。当然,在这种情况下,我会使用 mu-template 和以下模板文件:

VimL:" $Id: {rtp}/template/ruby/inspect.template
VimL: let s:value_start  = '¡'
VimL: let s:value_end    = '¡'
VimL: let s:reindent     = 1
VimL: let s:marker_open  = '<+'
VimL: let s:marker_close = '+>'
VimL: let s:varname = matchstr(getline(line('.')-1), '^\s*\zs\k\+\ze\s*=')
VimL: if empty(s:varname) |throw "the previous line don't assign any variable" |endif
puts "¡s:varname¡ = #{¡s:varname¡.inspect}"<++>
VimL:"vim: encoding=utf-8

I would go for:

nnoremap µ :s/^\s*\(\k\+\)\s*=.*/&\rputs "\1 = #{\1.inspect}"/<cr>

:s presents the advantage of doing the job plus matching the assigned variable if any. Doing the same thing with classical commands like yw, p, etc would be more cumbersome.

If the template become more complex, we can rely on template-file expanders as long as they easily permit to call viml function like matchstr(). Of course, in that case I would use mu-template with the following template-file:

VimL:" $Id: {rtp}/template/ruby/inspect.template
VimL: let s:value_start  = '¡'
VimL: let s:value_end    = '¡'
VimL: let s:reindent     = 1
VimL: let s:marker_open  = '<+'
VimL: let s:marker_close = '+>'
VimL: let s:varname = matchstr(getline(line('.')-1), '^\s*\zs\k\+\ze\s*=')
VimL: if empty(s:varname) |throw "the previous line don't assign any variable" |endif
puts "¡s:varname¡ = #{¡s:varname¡.inspect}"<++>
VimL:"vim: encoding=utf-8
爱*していゐ 2024-09-13 20:27:04

如果您正在即时执行这些操作,则片段片段可能如下所示:

${1:error} = '${2:error message here}'
puts "error = #{$1.inspect}"

另一方面,如果您只想输出预先存在的变量以进行调试。尼克-坎佐内里的宏可能更有用。

If you're doing these on the fly, a snipmate snippet could look like this:

${1:error} = '${2:error message here}'
puts "error = #{$1.inspect}"

If, on the other hand you're just wanting to output pre-existing variables for debugging purposes. Nick-Canzoneri's macro may be more useful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文