25

I wrote a little Vim function that moves the cursor to the first character of the current line. If the cursor was already on the first character, then the cursor is moved to the first column instead.

" Jump to first character or column
noremap H :call FirstCharOrFirstCol()<cr>

:function! FirstCharOrFirstCol()
:  let current_col = virtcol('.')
:  normal ^
:  let first_char = virtcol('.')
:  if current_col == first_char
:    normal 0
:  endif
:endfunction

How do I call this function silently? I’d rather ‘:call FirstCharOrFirstCol()’ wasn’t displayed in the status line. Simply changing to noremap H :silent call… doesn’t seem to be enough.

  • 7
    Just a small note: in a VimScript file (i.e. *.vim), you never need the leading : characters. – tommcdo May 25 '15 at 14:22
34

You can call the function silently by defining a silent map:

noremap <silent> H :call FirstCharOrFirstCol()<cr>

For more info, see :h :map-<silent>. Note in particular that this will only ensure that the command is not echoed to screen when the mapping is executed. The :silent command is used to silence output from the function itself (see :h :silent).

  • 1
    Nice one, :silent is what I was after. It's worth noting that if you have errors in your function you'll still get the messages unless you use !. I didn't realise I'd left the code in an invalid state and so thought I wasn't using :silent correctly as it wasn't silencing the echos and I could see an error in the output!! – JonnyRaa Aug 7 '18 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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