0

I was looking at this question on Unix & Linux Stack Exchange, and I thought I could write an answer for it using Vim.

I ran the following for a quick test, and was surprised to find it didn't seem to do anything. I was expecting it to replace the first number on the first line with one less than the number of lines in the buffer.

:execute '1s/^[0-9]\+/' . line('$')-1 . '/'

Here is the text I had in the buffer at the time:

5 10
sample_1 gaatatccga
sample_2 gaatatccga
sample_3 gaatatcgca

Why didn't the :execute command shown above replace the 5 with a 3 as intended?

1 Answer 1

2

. has the same precedence as + and -

'1s/^[0-9]\+/' . line('$')-1 . '/' is the same as ('1s/^[0-9]\+/' . line('$')) - 1 . '/'—it's string - number . string, which is not what you want.

To solve your problem:

:execute '1s/^[0-9]\+/' . (line('$')-1) . '/'
  • :h expr-..
  • :h expression-syntax.

Your Answer

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

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