-1

I want to check if number of lines are bigger than 10 in Vim.

I tried to use this code in vimrc file:

if line("$") > 10
    colorscheme currentline
endif

Edit: I made a colorscheme with currentline name.

2
  • 4
    Number of lines of what?
    – Matt
    Commented Feb 18, 2021 at 19:06
  • Welcome to Vi and Vim!
    – D. Ben Knoble
    Commented Feb 19, 2021 at 15:14

1 Answer 1

1

I think there are several points to address in your question:

Firstly, if line("$") > 10 does test if the number of lines in the current buffer is greater than 10, or more precisely as :h line() explains that tests if the line number (line()) of the last line of the buffer ($) is greater than 10.

Now there are a couple of things which can't work if you put this code in your vimrc:

  • colorscheme currentline is probably not what you think it is: :h :colorscheme is used to set a colorscheme (i.e. change the colors used by vim to display its UI and the text in your buffers), and I think it is unlikely that you have a colorscheme named currentline (unless you created it yourself I can't find such a colorscheme online). If you want to check which colorscheme are available you can use :colorscheme and use tab to autocomplete the available ones.

    If you want to check that your condition works you could use something more simple like echom "more than 10 lines" this will leave an output that you can find back with :messages

  • The second issue which is more important is that your condition checks for the lines in the current buffer but when your .vimrc is sourced you don't have a current buffer, so the condition will never be true. I think what you want to do is to execute this code when you read a file into a buffer (either because you gave its path in the command line when invoking vim, or with a command like :e /path/to/file).

    To do so you have several options, either use an autocmd (:h autocmd) with an event like :h BufEnter. Or create a ftplugin (:h ftplugin). If you are not familiar with these feature you should read the doc carefully and maybe check the relevant sections in vimcast or learnvimscriptthehardway.

Either way I think your question falls in the category of the XY problem you should create another one explaining more precisely what you are trying to achieve and what is not working. So I'm voting to close this question but comments where too limit to let me express all of that.

2
  • 2
    Thank you. I tried to use autocmd and it works. Sorry about bad question.
    – sbh
    Commented Feb 20, 2021 at 10:35
  • No problem about the question just don’t hesitate to give more details next time :)
    – statox
    Commented Feb 20, 2021 at 12:31

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