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! Commented Apr 11, 2022 at 9:56

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.