Let's start by removing those useless colons:
function! GotoClass(className)
normal! 1G
normal! /class\s\+ . a:className<CR>
endfunction
nnoremap <key> :call GotoClass(input('Class name: '))<CR>
Now, the most obvious issue is that :help :normal
can't do concatenation.
The following line literally searches for class
, followed by one or more whitespace characters, followed by a space, followed by any character except \n
, followed by a space, followed by a:className<CR>
:
normal! /class\s\+ . a:className<CR>
which is unlikely to find any match, which explains why your function only moves the cursor to the top of the buffer. Also, the <CR>
is useless in this context.
A simple fix would be to use :help :execute
, which does concatenation:
function! GotoClass(className)
normal! 1G
execute "normal! /class\s\+" . a:className
endfunction
nnoremap <key> :call GotoClass(input('Class name: '))<CR>
But we can do better. Instead of abusing :normal
, which doesn't really add any value here, let's use :help :/
to replace these two lines:
normal! 1G
execute "normal! /class\s\+" . a:className
with a single line:
function! GotoClass(className)
execute "1/class\s\+" . a:className
endfunction
nnoremap <key> :call GotoClass(input('Class name: '))<CR>
Let's not stop there! Is that custom function really necessary? We could use :help search()
and get rid of it:
nnoremap <key> <Cmd>1call search('class\s\+' . input('Class name: '))<CR>

At this point I wanted to go even further with :help searchdecl()
, which would have allowed me to get rid of concatenation, but it proved to be a lot less reliable than I expected so I'm afraid I reached the end of the line.
search()
function.:normal!
will not recognize<CR>
as a "return" character. You can work around that by using:execute "normal! /class\<CR>"
instead. In your actual example, you're trying to do some concatenation (with. a:className
) so you need to use a string anyways, another reason to use:execute
. But as Christian pointed out,search()
is better. See:help :execute
and:help search()
for all the details.