在vim中找到光标下的字符

时间:2011-04-18 11:33:24

标签: vim

在vim中,如何在光标下找到下一个字符?我想要类似*但是对于单个字符而不是单词。

示例:

H|o|w are you?

转到:

How are y|o|u?

我想要它的原因是因为我的文件中存在一个看起来很奇怪的字符(我甚至不知道如何键入),我想快速删除它们。

6 个答案:

答案 0 :(得分:71)

另见

g a (在光标下显示字符为ascii)

g 8 (将光标下的字符显示为utf8,包括Unicode内容,十六进制代码等)

最有用的是:

8 8

Find an illegal UTF-8 byte sequence at or after the
        cursor.  This works in two situations:
        1. when 'encoding' is any 8-bit encoding
        2. when 'encoding' is "utf-8" and 'fileencoding' is
           any 8-bit encoding
        Thus it can be used when editing a file that was
        supposed to be UTF-8 but was read as if it is an 8-bit
        encoding because it contains illegal bytes.
        Does not wrap around the end of the file.
        Note that when the cursor is on an illegal byte or the
        cursor is halfway a multi-byte character the command
        won't move the cursor.

<强>更新

使用 Tim Pope's vim-characterize plugin 获取完整的UNICODE名称和数据:

  

在Vim中,按某个字符上的ga会显示其中的字符   十进制,八进制和十六进制。

     

Characterize.vim将其与现代化相结合   以下补充:

     
      
  • Unicode字符名称:U + 00A9 COPYRIGHT SYMBOL
  •   
  • Vim有向图(插入字符后输入):Co,cO
  •   
  • 表情符号代码:: copyright:
  •   
  • HTML实体:©
  •   

答案 1 :(得分:32)

在一行中,您可以使用fo,然后使用;继续(或,向后)。

在多行上,您必须先使用/o,然后使用n继续(或N向后)。

或者,您的问题可能通过使用regexp和替换来解决,即:%s/[your odd character]//g

要设法复制并粘贴“奇数字符”,您应该使用 v 进行视觉选择字符,然后 y ESC

然后输入: :%s/<CTRL+r>"//g

<CTRL+r>"将在命令行中复制复制寄存器的内容。

答案 2 :(得分:13)

以下内容将<leader>z(通常为\z

:nnoremap <leader>z xhp/<C-R>-<CR>

请告诉我这是否适合您。

答案 3 :(得分:2)

然后只需使用规则将它们全部替换

:%s/search/replacement/

这会将search的所有出现替换为replacement

答案 4 :(得分:0)

好吧,对于一次性快速黑客只需使用*,它就什么也找不到,但是你要按/,&lt; up&gt;并编辑模式。仅在vim将字符识别为单词的一部分时才有效。

答案 5 :(得分:0)

:exec '%s/'.getline('.')[col('.')-1].'//g'

如果它是特殊字符,你必须添加反斜杠,但由于所有特殊字符都是可输入的,我想它不是。

编辑:要使用多字节字符,请将getline('.')[col('.')-1]替换为matchstr(getline('.'), '.', col('.')-1)(参见this answer)。

相关问题