Find character under cursor in vim

Vim

Vim Problem Overview


In vim, how can I find the next occurrence of the character under the cursor? I want something like * but for a single character instead of a word.

Example:

H|o|w are you?

goes to:

How are y|o|u?

The reason I want it is because there is a strange looking character (one that I don't even know how to type) all over my file and I want to remove them all quickly.

Vim Solutions


Solution 1 - Vim

Also look at

ga (show character under cursor as ascii)

g8 (show character under cursor as utf8, including Unicode stuff, hex codes etc)

And most usefully:

8g8

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.

Update

Use Tim Pope's vim-characterize plugin to get full UNICODE names and data:

> In Vim, pressing ga on a character reveals its representation in > decimal, octal, and hex. > > Characterize.vim modernizes this with the > following additions: > > - Unicode character names: U+00A9 COPYRIGHT SYMBOL > - Vim digraphs (type after to insert the character): Co , cO > - Emoji codes: :copyright: > - HTML entities: ©

Solution 2 - Vim

On a single line you can use fo and then ; to go forward (or , backward).

On multiple line, you must use /o and then n to go forward (or N backward).

Alternatively, your problem might be solved by using regexp and substitute, ie :%s/[your odd character]//g

To manage to copy and paste your "odd character", you should go in visual using v to select the character, then yESC.

Then type : :%s/<CTRL+r>"//g

<CTRL+r>" will copy the content of the copy register in the command line.

Solution 3 - Vim

Following will map <leader>z (usually \z)

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

Let me know if this works for you.

Solution 4 - Vim

Then simply use a rule to replace them all

:%s/search/replacement/

This replaces all occurences of search with replacement.

Solution 5 - Vim

Well, for one-time quick-hack just use *, it will find nothing, but you than press /, <up> and edit the pattern. Only works if vim recognizes the character as part of word, unfortunately.

Solution 6 - Vim

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

If it's special character, you'll have to add backslash, but since all special characters are typeable, I suppose it's not.

Edit: for it to work with a multi-byte char, replace getline('.')[col('.')-1] by matchstr(getline('.'), '.', col('.')-1) (see this answer).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpriomsrbView Question on Stackoverflow
Solution 1 - VimseheView Answer on Stackoverflow
Solution 2 - VimXavier T.View Answer on Stackoverflow
Solution 3 - VimRumple StiltskinView Answer on Stackoverflow
Solution 4 - VimhalfdanView Answer on Stackoverflow
Solution 5 - VimJan HudecView Answer on Stackoverflow
Solution 6 - VimJan HudecView Answer on Stackoverflow