在行尾切换分号(或其他字符) [英] Toggle semicolon (or other character) at end of line

查看:32
本文介绍了在行尾切换分号(或其他字符)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在行尾附加(或删除)分号是一种常见的操作.然而像 A; 这样的命令会修改当前光标位置,这并不总是理想的.

Appending (or removing) a semicolon to the end of the line is a common operation. Yet commands like A; modify the current cursor position, which is not always ideal.

是否有一种直接的方法来映射命令(例如 ;;)来切换分号是否出现在行尾?

Is there a straightforward way to map a command (e.g. ;;) to toggle whether a semicolon appears at the end of a line?

我目前在我的 vimrc 中使用这个命令来追加:

I'm currently using this command in my vimrc to append:

map ;; A;<Esc>

推荐答案

这样的事情会起作用

nnoremap ;; :s/\v(.)$/\=submatch(1)==';' ? '' : submatch(1).';'<CR>

这使用了一个替代命令并检查最后一个字符是否是分号,如果是,则将其删除.如果它不是将其附加到匹配的字符.这在替换部分使用 \= 来执行 vim 表达式.

This uses a substitute command and checks to see if the last character is a semicolon and if it is it removes it. If it isn't append it to the character that was matched. This uses a \= in the replacement part to execute a vim expression.

如果您不想匹配任何任意字符,您可以将其包装在一个函数中并传入您想要匹配的字符.

If you wan't to match any arbitrary character you could wrap it in a function and pass in the character you wanted to match.

function! ToggleEndChar(charToMatch)
    s/\v(.)$/\=submatch(1)==a:charToMatch ? '' : submatch(1).a:charToMatch
endfunction

然后映射将切换分号.

nnoremap ;; :call ToggleEndChar(';')<CR>

这篇关于在行尾切换分号(或其他字符)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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