user1475895
user1475895

Reputation: 11

mapping # in vim

In my .vimrc I have mapped the #-key to a macro for commenting out/in lines of code.

Unfortunately # in vim already has a function - it searches backwards for the word beneath the cursor.

What I would now like to have is a way to map this functionality to another key-sequence (ideally I would like to have Control-* for that as * alone searches forward).

Does anyone know how to achieve this?

Many thanks!

Upvotes: 1

Views: 94

Answers (3)

peeyush
peeyush

Reputation: 2931

I use \+c for commenting and \+d for removing comments. The mappings are following :

:map \c <ESC>:s,^\(\s*\)[^/ \t]\@=,\1// <ESC>,e<CR>j$a
:map \d <ESC>:s,^\(\s*\)// \s\@!,\1<ESC>,e<CR>j$a

Above mappings are used in command mode. Taken from one answer on SO, which I am currently unable to find.

Upvotes: 0

Idan Arye
Idan Arye

Reputation: 12633

Like Ingo Karkat said, mapping Ctrl+certain keys is impossible in vim. However, you can map Alt+8 instead:

noremap <A-8> #

I suggest Alt+8 instead of Alt+* because if you wanted to bend your hand in unnatural ways to press more than one modifier keys to perform a command, you would probably be using Emacs instead of Vim.

Upvotes: 0

Ingo Karkat
Ingo Karkat

Reputation: 172768

Unfortunately, Ctrl + * cannot be used; I would propose \*; it's longer to type, but backwards searches are probably not that common.

:nnoremap <Leader>* #

Upvotes: 2

Related Questions