Search code examples
vim

vim line number does weird things on lines starting with an 8


I have some commands set up that run the test under my current cursor position. using the command rails test <filename>:<linenumber>. I execute this command through exec '!rails test %:'.line('.') which works fine in most situations. However, for some reason when the line number starts with an 8 it behaves weirdly.

when testing with the command :exec '!echo "%:'.line('.').'"' i get the following outputs

line  7 -> test.txt:7
line  8 -> test.txt
line  9 -> test.txt:9
line 79 -> test.txt:79
line 80 -> test.txt0
line 89 -> test.txt9
line 90 -> test.txt:90

This pattern repeats for the 800s as well. Am i missing something obvious?

EDIT: As someone in the comments suggested i posted my question on the vi-stackexchange.


Solution

  • This command:

    :exec '!echo "%:'.line('.').'"'
    

    is handled in several steps from the inside to the outside:

    1. line('.') is evaluated to 8,

    2. '!echo "%:'.'8'.'"' is evaluated to '!echo "%:8"'

    3. '!echo "%:8"' is parsed by Vim before being sent to :execute, in order to expand various special characters as explained in :help cmdline-special. The "problem" (more like "powerful feature" if you ask me) is that the expansion takes into account :help filename-modifiers and :8 is a legit filename modifier:

      :8 Converts the path to 8.3 short format (currently only on MS-Windows). Will act on as much of a path that is an existing path.

      After expansion, '!echo "%:8"' looks like '!echo "test.txt"' because no conversion was performed. If the number is 80, you get test.txt0 because the file name is left as-is and then there's that trailing 0. And so on for test.txt00, etc.

    4. !echo "test.txt" (or !echo "test.txt0", or !echo "test.txt00", etc.) is finally executed.

    In order to avoid that unwanted expansion, you should evaluate the file name and the line number separately:

    '!rails test '.expand('%').':'.line('.')