0

I'm writing a function that should run a linter and then populate the location list with appropriate linting messages.

The problem is that when I call the function (by running :Phpcs), the cursor jumps to the top of the file, which is undesired, since I plan on running the linter every time I save the file and then check the location list before committing any changes to the repository.

I have cut everything unneeded away and the following is what is left in my .vimrc

I have tested the function in both Vim 8.2.43000 and Nvim 0.6.1 - Both installed from Homebrew on Mac OS Big Sur.

function! RunPhpcs()
    echo "Hello world"
endfunction

command! Phpcs execute RunPhpcs()

The desired functionality is that when I run the function it does not jump to the beginning of the file.

I have read through a somewhat similar, but different, question and tried both getpos()/setpos() and winsaveview()/winrestview() but with no luck.

What am I missing to make the function call not jump to the beginning of the file when executed?

1 Answer 1

1

Maybe could you use call instead of execute:

function! RunPhpcs()
    echo "Hello world"
endfunction

command! Phpcs call RunPhpcs()

The execute function moves the cursor to the result of the evaluated expression. If your function doesn't explicitly returns a value it returns 0 and the cursor is moved at the start of the file.

You can experiment that by running execute 2.

1
  • Using call instead of execute solved the problem! - Thanks!
    – Fizk
    Commented Apr 11, 2022 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.