Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go to next character under cursor in Vim

Tags:

vim

editor

As stated in the header, I would like to go to the next occurrence of the character currently under the cursor in Vim. I know about f <character>, etc., but I specifically want "this" character. How do I do this?

like image 912
gustafbstrom Avatar asked Dec 15 '15 09:12

gustafbstrom


People also ask

How do I jump to a character in Vim?

You can type f<character> to put the cursor on the next character and F<character> for the previous one.

How do I jump to the next word in Vim?

Press w (“word”) to move the cursor to the right one word at a time. Press b (“back”) to move the cursor to the left one word at a time. Press W or B to move the cursor past the adjacent punctuation to the next or previous blank space. Press e (“end”) to move the cursor to the last character of the current word.

How do you go to the middle of a line in Vim?

Vim Screen NavigationM – Go to the middle line of current screen. L – Go to the last line of current screen. ctrl+f – Jump forward one full screen.

How do I go to a previous match in vim?

Basic searching Type ggn to jump to the first match, or GN to jump to the last.


3 Answers

It does not exist natively.

If you want, you can create a new mapping (bound to <leader>a for example). Add the following to your .vimrc and restart:

nnoremap <leader>a vy/<C-R>"<CR>


" vy : copy the character under the cursor
" / : open search
" <C-R>" : paste the character
" <CR> : launch the search

Then each following search will be available via the n key as usual.

(Leader is by defaut the \ key but can be remapped with the command :set mapleader = )

This is not part of the question, but I should add that for easy navigation, it is a good idea to have look at easymotion plugin and know that it exists.

like image 101
Xavier T. Avatar answered Sep 23 '22 07:09

Xavier T.


I'm not aware of a built-in equivalent to * for the character under the cursor but the mappings below should cover your requirements:

nnoremap <silent> <key> "zyl:normal! f^Rz^M
nnoremap <silent> <key> "zyl:normal! F^Rz^M
nnoremap <silent> <key> "zyl:normal! t^Rz^M
nnoremap <silent> <key> "zyl:normal! T^Rz^M

^R is obtained with <C-v><C-r> and ^M with <C-v><CR>.

like image 38
romainl Avatar answered Sep 19 '22 07:09

romainl


You can do:

yl:normal! f<Ctrl-R>"<CR>

Explanation:

yl— yank (copy) the character under cursor into the " register
:normal — execute a Normal mode command that follows
f<Ctrl-R>"<CR> — insert the contents of the " register and run the f{char} command.

Of course, you'll want to create a mapping for the command to make it usable.

like image 28
Eugene Yarmash Avatar answered Sep 22 '22 07:09

Eugene Yarmash