cursorline/cursorcolumn highlighting

165 views
Skip to first unread message

Bee

unread,
Dec 24, 2009, 5:28:44 PM12/24/09
to vim_use
When I present source code, it is easier if I use cursorline/
cursorcolumn highlighting to direct eyes to the relevant section.

I wanted a function key to turn on and off the highlighting, but did
not want to dedicate more than one of those valuable resources. So...
in this example F7 cycles thru the cursorline and cursorcolumn
highlighting.

For Vim versions older that 7, it only moves the line to the center of
the screen.

I am somewhat new to Vim scripting, please comment or suggest
improvements.

" toggle cursorline/cursorcolumn or center line
nmap <F7> zz
if version >= 700 " NONE turns off underlining
highlight CursorLine NONE ctermbg=Yellow
highlight CursorColumn NONE ctermbg=Yellow
let s:lico=0
function LiCo()
let s:lico=s:lico>2 ?0 :s:lico+1
let &cursorline=s:lico % 2
let &cursorcolumn=s:lico / 2
endfun
nmap <silent> <F7> :call LiCo()<cr>
endif
imap <F7> <c-o><F7>
vmap <F7> <c-c><F7>gv

Gary Johnson

unread,
Dec 25, 2009, 12:17:54 AM12/25/09
to vim_use
On 2009-12-24, Bee wrote:
> When I present source code, it is easier if I use cursorline/
> cursorcolumn highlighting to direct eyes to the relevant section.
>
> I wanted a function key to turn on and off the highlighting, but did
> not want to dedicate more than one of those valuable resources. So...
> in this example F7 cycles thru the cursorline and cursorcolumn
> highlighting.
>
> For Vim versions older that 7, it only moves the line to the center of
> the screen.
>
> I am somewhat new to Vim scripting, please comment or suggest
> improvements.

You might be interested in

:set {option}!
:set inv{option}

either of which will toggle the value of {option}. See

:help E518

and skip to the fourth paragraph down.

Regards,
Gary


Bee

unread,
Dec 25, 2009, 10:45:57 PM12/25/09
to vim_use
On Dec 24, 9:17 pm, Gary Johnson <garyj...@spocom.com> wrote:
> You might be interested in
> :set {option}!
> :set inv{option}

I know about them, but did not know the 'reset'.
:se[t] {option}&

I had been using:
:nmap <silent> <F7> :set cursorline!<cr>
:nmap <silent> ,<F7> :set cursorcolumn!<cr>

But decided that I wanted to cycle 1-4 with ONE key
and to learn something new:
1) off
2) cursorline
3) cursorcolumn
4) both cursorline and cursorcolumn

How can LiCo() be made buffer local?

let s:lico=0
function LiCo()
let s:lico=s:lico>2 ?0 :s:lico+1
let &cursorline=s:lico % 2
let &cursorcolumn=s:lico / 2
endfun

I tried b:lico in place of s:lico, it did not work.

Gary Johnson

unread,
Dec 26, 2009, 3:07:46 AM12/26/09
to vim_use

That's odd. I just tried your function with b:lico and it worked
fine for me: each call to LiCo() advanced the state of the cursor
highlights as you described. I only tried it with one buffer.

Regards,
Gary


Om Prakash Singh //Kotak /Bank

unread,
Dec 26, 2009, 4:55:52 AM12/26/09
to vim...@googlegroups.com

Dear All,

This mail does not poertains to me. Please mark the same to right
person.

Thanks and Regards
Om Prakash Singh | Asst. Manager |Kotak Mahindra Bank Ltd.
5/27, Vishal Khand, Gomtinagar, Lucknow 226010 | D: (0522) 4151910 | M:
(+91) 9936250828

Regards,
Gary


--
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php


DISCLAIMER:
This communication is confidential and privileged and is directed to and for the use of the addressee only. The recipient if not the addressee should not use this message if erroneously received, and access and use of this e-mail in any manner by anyone other than the addressee is unauthorized. The recipient acknowledges that Kotak Mahindra Bank may be unable to exercise control or ensure or guarantee the integrity of the text of the email message and the text is not warranted as to completeness and accuracy. Before opening and accessing the attachment, if any, please check and scan for virus.

Bee

unread,
Dec 26, 2009, 10:34:51 AM12/26/09
to vim_use
Gary Johnson wrote:
> That's odd. I just tried your function with b:lico and it
> worked fine for me: each call to LiCo() advanced the state of
> the cursor highlights as you described. I only tried it with
> one buffer.

Yes, odd. When I run the following I get errors.
Both terminal versions Vim 7.2.315 and Vi 6.2

" toggle cursorline/cursorcolumn or center line

highlight CursorLine NONE ctermbg=Yellow
highlight CursorColumn NONE ctermbg=Yellow

let b:lico=0
function LiCo()
let b:lico=b:lico>2 ?0 :b:lico+1
let &cursorline=b:lico % 2
let &cursorcolumn=b:lico / 2


endfun
nmap <silent> <F7> :call LiCo()<cr>


Error detected while processing function LiCo:
line 1:
E121: Undefined variable: b:lico
E15: Invalid expression: b:lico>2 ?0 :b:lico+1
line 2:
E121: Undefined variable: b:lico
E15: Invalid expression: b:lico % 2
line 3:
E121: Undefined variable: b:lico
E15: Invalid expression: b:lico / 2

--
Bill
Santa Cruz, California

Gary Johnson

unread,
Dec 26, 2009, 1:22:12 PM12/26/09
to vim_use

I thought about that possibility last night. Your code initializes
b:lico only once, so for only one buffer. Here's a way to fix that
problem without using an autocommand. Change this

> let b:lico=0
> function LiCo()
> let b:lico=b:lico>2 ?0 :b:lico+1
> let &cursorline=b:lico % 2
> let &cursorcolumn=b:lico / 2
> endfun

to

function LiCo()
if !exists("b:lico")
let b:lico=0
endif


let b:lico=b:lico>2 ?0 :b:lico+1
let &cursorline=b:lico % 2
let &cursorcolumn=b:lico / 2
endfun

Untested, but I think that syntax is right.

Regards,
Gary


Bee

unread,
Dec 26, 2009, 1:50:19 PM12/26/09
to vim_use

Thank you, that works and is a very good to know.

Reply all
Reply to author
Forward
0 new messages