I want to check if number of lines are bigger than 10 in Vim.
I tried to use this code in
if line("$") > 10 colorscheme currentline endif
Edit: I made a colorscheme with
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.Sign up to join this community
I think there are several points to address in your question:
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
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
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.