Home Explore Blog CI



neovim

42th chunk of `runtime/doc/vimeval.txt`
2cf4d5cbd1c58b4ccbcaf388d90e850ce866bb53d82facf60000000100000fb5
 a single command, the first error message is
usually the most specific one and therefore converted to the error exception.
   Example: >
	echo novar
causes >
	E121: Undefined variable: novar
	E15: Invalid expression: novar
The value of the error exception inside try conditionals is: >
	Vim(echo):E121: Undefined variable: novar
<							*except-syntax-error*
But when a syntax error is detected after a normal error in the same command,
the syntax error is used for the exception being thrown.
   Example: >
	unlet novar #
causes >
	E108: No such variable: "novar"
	E488: Trailing characters
The value of the error exception inside try conditionals is: >
	Vim(unlet):E488: Trailing characters
This is done because the syntax error might change the execution path in a way
not intended by the user.  Example: >
	try
	    try | unlet novar # | catch | echo v:exception | endtry
	catch /.*/
	    echo "outer catch:" v:exception
	endtry
This displays "outer catch: Vim(unlet):E488: Trailing characters", and then
a "E600: Missing :endtry" error message is given, see |except-single-line|.

==============================================================================
9. Examples						*eval-examples*

Printing in Binary ~
>
  :" The function Nr2Bin() returns the binary string representation of a number.
  :func Nr2Bin(nr)
  :  let n = a:nr
  :  let r = ""
  :  while n
  :    let r = '01'[n % 2] .. r
  :    let n = n / 2
  :  endwhile
  :  return r
  :endfunc

  :" The function String2Bin() converts each character in a string to a
  :" binary string, separated with dashes.
  :func String2Bin(str)
  :  let out = ''
  :  for ix in range(strlen(a:str))
  :    let out = out .. '-' .. Nr2Bin(char2nr(a:str[ix]))
  :  endfor
  :  return out[1:]
  :endfunc

Example of its use: >
  :echo Nr2Bin(32)
result: "100000" >
  :echo String2Bin("32")
result: "110011-110010"


Sorting lines ~

This example sorts lines with a specific compare function. >

  :func SortBuffer()
  :  let lines = getline(1, '$')
  :  call sort(lines, function("Strcmp"))
  :  call setline(1, lines)
  :endfunction

As a one-liner: >
  :call setline(1, sort(getline(1, '$'), function("Strcmp")))
<

scanf() replacement ~
							*sscanf*
There is no sscanf() function in Vim.  If you need to extract parts from a
line, you can use matchstr() and substitute() to do it.  This example shows
how to get the file name, line number and column number out of a line like
"foobar.txt, 123, 45". >
   :" Set up the match bit
   :let mx='\(\f\+\),\s*\(\d\+\),\s*\(\d\+\)'
   :"get the part matching the whole expression
   :let l = matchstr(line, mx)
   :"get each item out of the match
   :let file = substitute(l, mx, '\1', '')
   :let lnum = substitute(l, mx, '\2', '')
   :let col = substitute(l, mx, '\3', '')

The input is in the variable "line", the results in the variables "file",
"lnum" and "col". (idea from Michael Geddes)


getting the scriptnames in a Dictionary ~
						*scriptnames-dictionary*
The `:scriptnames` command can be used to get a list of all script files that
have been sourced.  There is also the `getscriptinfo()` function, but the
information returned is not exactly the same.  In case you need to manipulate
the output of `scriptnames` this code can be used: >
    " Get the output of ":scriptnames" in the scriptnames_output variable.
    let scriptnames_output = ''
    redir => scriptnames_output
    silent scriptnames
    redir END

    " Split the output into lines and parse each line.	Add an entry to the
    " "scripts" dictionary.
    let scripts = {}
    for line in split(scriptnames_output, "\n")
      " Only do non-blank lines.
      if line =~ '\S'
	" Get the first number in the line.
	let nr = matchstr(line, '\d\+')
	" Get the file name, remove the script number " 123: ".
	let name = substitute(line, '.\+:\s*', '', '')
	" Add an item to the Dictionary
	let scripts[nr] = name
      endif
    endfor
    unlet scriptnames_output

==============================================================================

Title: Exception Handling Examples and Practical Vimscript Functions
Summary
This section provides examples of exception handling behaviors and presents useful Vimscript functions for tasks like binary conversion, sorting lines, emulating sscanf(), and parsing the output of the ':scriptnames' command into a dictionary. It demonstrates how Vim handles errors, particularly when syntax errors are involved, and offers practical solutions to common scripting challenges.