• Latest
  • Trending
  • All

Adventures in Vim: Lee and Jim figure out how to change comment colors

2 months ago

IPL Stars “Lucky” As COVID Ravages India, Says Delhi Capitals’ Chris Woakes

7 mins ago

Sidhu steps up attack on Capt, questions role in sacrilege case

1 hour ago

‘Unite against BJP’: Mamata writes to Oppn leaders

3 hours ago

I beg govt to make arrangements for last rites of COVID victims in dignified manner: Kumaraswamy

3 hours ago

Haryana records 9,742 new Covid-19 cases, highest single-day spike yet

8 hours ago

Mamata to PM Modi- The New Indian Express

8 hours ago

How Long Will You Ignore Climate Crisis

9 hours ago

Need of third vaccineΒ dose based more upon conjecture than data, say experts- The New Indian Express

19 hours ago

PM Modi To Chair 3 High-Level Meetings To Review Covid Situation, Will Address Virtual West Bengal Rally At 5 PM

22 hours ago

Apple to host virtual iPad event, may hint at new AirPods- The New Indian Express

23 hours ago

German MPs quiz Angela Merkel, ministers over Wirecard scandal

1 day ago

Sukhbir threatens protest over delayed payment to farmers

1 day ago
Friday, April 23, 2021
  • PRESS RELEASE
  • ADVERTISE
  • CONTACT
Chann Pardesi Media
  • Home
  • RadioLive
  • News
    • India
    • Punjab
    • International
    • Entertainment
  • Politics
  • Health
  • Business
  • Sports
  • Technology
  • Lifestyle
  • Video
    • All
    • Hindi Songs
    • Punjabi Songs
    Hindi Heart Touching Songs 2021 – Arijit Singh, Atif Aslam, Neha Kakkar, Armaan Malik,Shreya Ghoshal

    Hindi Heart Touching Songs 2021 – Arijit Singh, Atif Aslam, Neha Kakkar, Armaan Malik,Shreya Ghoshal

    Old Vs New Bollywood Mashup Songs 2020 | 90's Bollywood Romantic Mashup Live_Bollywood Mashup 2021

    Old Vs New Bollywood Mashup Songs 2020 | 90's Bollywood Romantic Mashup Live_Bollywood Mashup 2021

    New Hindi Song 2021 January πŸ’– Top Bollywood Romantic Love Songs 2021 πŸ’– Best Indian Songs 2021

    New Hindi Song 2021 January πŸ’– Top Bollywood Romantic Love Songs 2021 πŸ’– Best Indian Songs 2021

    Hindi Heart Touching Songs 2021 – Arijit Singh, Atif Aslam, Neha Kakkar, Armaan Malik,Shreya Ghoshal

    Hindi Heart Touching Songs 2021 – Arijit Singh, Atif Aslam, Neha Kakkar, Armaan Malik,Shreya Ghoshal

    Old Vs New Bollywood Mashup Songs 2020 |New Hindi Songs,Indian Love Songs Mashup_Romantic Songs 2021

    Old Vs New Bollywood Mashup Songs 2020 |New Hindi Songs,Indian Love Songs Mashup_Romantic Songs 2021

    Hindi Heart touching Song 2020 – arijit singh,Atif Aslam,Neha Kakkar,Armaan Malik,Shreya Ghoshal

    Hindi Heart touching Song 2020 – arijit singh,Atif Aslam,Neha Kakkar,Armaan Malik,Shreya Ghoshal

    New Hindi Song 2021 January πŸ’– Top Bollywood Romantic Love Songs 2021 πŸ’– Best Indian Songs 2021

    New Hindi Song 2021 January πŸ’– Top Bollywood Romantic Love Songs 2021 πŸ’– Best Indian Songs 2021

    Ikk Pal Kaka, Full Song, Kaka New Song, New Punjabi Song 2020, Latest New Punjabi Song 2020

    Ikk Pal Kaka, Full Song, Kaka New Song, New Punjabi Song 2020, Latest New Punjabi Song 2020

    Bollywood Romantic Love Songs 2021 πŸ’– New Hindi Songs 2021 JanuaryπŸ’– Bollywood Hits Songs 2021

  • Travel
  • Youtube
No Result
View All Result
Chann Pardesi Media
No Result
View All Result
ADVERTISEMENT
Home Technology

Adventures in Vim: Lee and Jim figure out how to change comment colors

by Chann Pardesi Team
February 11, 2021
in Technology
0
Share on FacebookShare on TwitterShare on Email
ADVERTISEMENT


Who doesn't like cyan comments in their text editor? Lee Hutchinson, that's who.
Enlarge / Who doesn’t like cyan comments in their text editor? Lee Hutchinson, that’s who.

Jim Salter

One fine Monday morning, Ars Technica Senior Technology Editor Lee Hutchinson came to me with a problem: the colors in his text editor, in his humble opinion, had Begun To Suck.

In Lee’s 20 years or so of Vim usage, he’d gotten accustomed to comment lines in his code and configuration files being rendered in dark blue. But after upgrading a machine to Ubuntu 20.04, Vim started rendering comments in cyanβ€”and since the “Identifier” syntax category also rendered in cyan, he was unhappy enough about it to decide to change the defaults.

At first blush, Vim seems to adhere to roughly the same configuration standard that many if not most Unix-like systems and applications doβ€”there’s a set of systemwide configurations in /etc, which can be overridden individually per user by changes made in an optional configuration file in that user’s home directory. In Vim’s case, that’s ~/.vimrcβ€”just like Bash configurations can be overridden in ~/.bashrc.

But when Lee tried to make his One Simple Change to Vim’s syntax highlightingβ€”turn comments from the new cyan back into the dark blue, which he preferredβ€”things got interesting.

The Hutchinson way to configure comment highlighting

After a little googling, the command Lee found to change comment color seemed to be pretty simple: highlight comment ctermcfg=19, where 19 is the color code Vim uses for dark blue. The problem is, making the change in ~/.vimrc didn’t actually work.

To be more specific, it did workβ€”brieflyβ€”but almost immediately after opening the file, the comments changed from dark blue back to cyan again. On a local, fast machine, the change happened too quickly to notice; but Lee was ssh’ing into a remote machine, and that gave just enough delay to see his color preference applied initially but quickly reverted.

After significant googling, Lee discovered an ugly workaround. There’s a very old joke that Vim isn’t actually a text editor at allβ€”it’s an operating system in its own right, which simply masquerades as a text editor. Like most good jokes, this one’s a bit over the top but has a kernel of truth to itβ€”Vim config files don’t simply assign values to configuration variables; they can actually run code in their own right.

Advertisement

In Lee’s case, he decided that, since there was a roughly 100ms delay between his dark blue comments being applied and Vim changing them back, he could just outwait the program by waiting 100ms to apply the change in the first place:

function DelayedSetVariables(timer)
    highlight comment ctermfg=19
endfunction

let timer=timer_start(16,'DelayedSetVariables')

Sure enough, the ugly hack worked: now, instead of seeing dark blue comments initially that then flashed back to the hated cyan, Hutchinson saw cyan comments that then flashed to his preferred dark blue.

This worked well enough for his purposes… but what’s the point of being a senior technology editor if you can’t run a problem past a technology reporter who reports to you?

Advertisement

The wrong way… actually, several wrong ways

When Lee brought his kinda-solved problem to me, it certainly sounded like a bugβ€”I might not be a Vim user myself, but with more than 20 years of Unix-like OS experience under my own belt, I also expected a user-profile configuration file to cleanly overwrite a system-wide configuration. The unhinged ranting coherent, focused problem report Lee offered me included a warning: there were, in his words, “about 20 different places where Vim configuration changes get applied,” so tracking down the problem was unusually sticky.

I’m not a Vim user myselfβ€”I’m one of those heathens who never saw any particular reason to learn more about Vim than the :q! needed to get the hell out of itβ€”but my immediate suspicion was that a bug was causing Vim configuration files to be applied out of order. So I googled how to check what configurations had been applied to a running Vim instance: turns out there’s a special command :scriptnames that will provide you with exactly that.

  1: /usr/share/vim/vimrc
  2: /usr/share/vim/vim81/debian.vim
  3: /usr/share/vim/vim81/syntax/syntax.vim
  4: /usr/share/vim/vim81/syntax/synload.vim
  5: /usr/share/vim/vim81/syntax/syncolor.vim
  6: /usr/share/vim/vim81/filetype.vim
  7: ~/.vimrc
  8: /usr/share/vim/vim81/plugin/getscriptPlugin.vim
  9: /usr/share/vim/vim81/plugin/gzip.vim
 10: /usr/share/vim/vim81/plugin/logiPat.vim
 11: /usr/share/vim/vim81/plugin/manpager.vim
 12: /usr/share/vim/vim81/plugin/matchparen.vim
 13: /usr/share/vim/vim81/plugin/netrwPlugin.vim
 14: /usr/share/vim/vim81/plugin/rrhelper.vim
 15: /usr/share/vim/vim81/plugin/spellfile.vim
 16: /usr/share/vim/vim81/plugin/tarPlugin.vim
 17: /usr/share/vim/vim81/plugin/tohtml.vim
 18: /usr/share/vim/vim81/plugin/vimballPlugin.vim
 19: /usr/share/vim/vim81/plugin/zipPlugin.vim
 20: /usr/share/vim/vim81/scripts.vim
 21: /usr/share/vim/vim81/syntax/perl.vim
 22: /usr/share/vim/vim81/syntax/pod.vim
Press ENTER or type command to continue

Lee hadn’t been kidding about the vast array of configuration files to look through: my system loaded 22 separate configuration files, 15 of which took effect after the .vimrc in my home directory! Thus began the start of a long, winding, and ultimately fruitless primrose path: I wanted to find instances of the comment color being changed somewhere after my ~/.vimrc, and it turned out that just wasn’t happening.

The only place I could find where comment color was set to Cyan was in /usr/share/vim/vim81/syncolor.vim, a couple of spaces ahead of my personal .vimrc. In theory, the change in ~/.vimrc should have overridden the one in syncolor.vimβ€”but in practice, without Lee’s ugly timer hack, the only way I could find to change the comment color was within syncolor.vim itself.

" Many terminals can only use six different colors (plus black and white).
" Therefore the number of colors used is kept low. It doesn't look nice with
" too many colors anyway.
" Careful with "cterm=bold", it changes the color to bright for some terminals.
" There are two sets of defaults: for a dark and a light background.
if &background == "dark"
  SynColor Comment      term=bold cterm=NONE ctermfg=Cyan ctermbg=NONE gui=NONE guifg=#80a0ff guibg=NONE

Changing ctermfg=Cyan inside syncolor.vim to ctermfg=19β€”or, better yet, ctermfg=DarkBlue, which produced an easier-to-read shade of blueβ€”worked as expected, and it produced the output Lee wanted without the god-awful timer hack. But it applied the change systemwide, not just to Lee’s own user accountβ€”and more importantly, it didn’t explain how or why the original change in ~/.vimrc refused to work as expected.

I still smelled an out-of-order bug, so I dug further.

" Vim syntax support file
" Maintainer:   Bram Moolenaar 
" Last Change:  2001 Sep 12

" This file sets up the default methods for highlighting.
" It is loaded from "synload.vim" and from Vim for ":syntax reset".
" Also used from init_highlight().

According to the comments at the top of syncolor.vim, the changes within that file were applied in three casesβ€”when synload.vim is parsed during Vim initialization, when the user issues the command :syntax reset, and within the Vim function init_highlight(). I knew neither Lee nor I was calling for :syntax reset, so I proceeded to find the invocation of syncolor.vim from within synload.vim.

" Set the default highlighting colors.  Use a color scheme if specified.
if exists("colors_name")
  exe "colors " . colors_name
else
  runtime! syntax/syncolor.vim
endif

If I put the simple highlight comment ctermfg=19 back into my ~/.vimrc, and commented out the runtime! syntax/syncolor.vim in synload.vim, I believed everything should work properly: this would still qualify as an ugly hack, of course, but it would narrow down where the problem behavior was coming from and allow me to write a more exact bug report to file with the Vim project.

Unfortunately, it didn’t work that way: even with runtime! syntax/syncolor.vim commented out, the Cyan comments that file specified overrode the simple setting in my ~/.vimrc. This meant the configurations there were being called by Vim’s init_highlight() function after it parsed ~/.vimrc.

On the one hand, this certainly still smelled like a bug to me: I couldn’t override a simple configuration setting from my user-level rc file. On the other hand, did I mention the 20+ years of open source experience? I needed to make certain I wasn’t missing something obvious that would cause a bug report to just get rejected with a #WONTFIX because I’d missed some deliberate Vim idiosyncrasy.

Finding the right way

Since Vim’s configuration files had self-documenting comments, the time had come to read them more thoroughly. I’d already learned that the contents of syncolor.vim were applied by init_highlight() and synload.vimβ€”but I needed to dig further.

I couldn’t get any further with the documentation comments at the top of synload.vim or syncolor.vim, but the next clue came from the code in syncolor.vim itself:

if syntax_cmd == "enable"
    " ":syntax enable" keeps any existing colors
    command -nargs=* SynColor hi def 
    command -nargs=* SynLink hi def link 
  elseif syntax_cmd == "reset"
    " ":syntax reset" resets all colors to the default
    command -nargs=* SynColor hi 
    command -nargs=* SynLink hi! link 
  else
    " User defined syncolor file has already set the colors.
    finish
  endif

Clearly, there was some proper way to set user-defined colors, since this if block specifically avoided setting them up if a “user defined syncolor file” already had. So the next step was to Google “vim user defined syncolor file.” The top search result was the source for syncolor.vim itself on Github, but the second result brought me to Vim documentation at SourceForge.

Performing a ctrl-F syncolor in-browser search on this 5,128-line document eventually got me to the information I needed, about 90 percent of the way down the page:

If you want to use different colors for syntax highlighting, you can add a Vim
script file to set these colors.  Put this file in a directory in
'runtimepath' which comes after $VIMRUNTIME, so that your settings overrule
the default colors.  This way these colors will be used after the ":syntax
reset" command.

For Unix you can use the file ~/.vim/after/syntax/syncolor.vim.

Finally, I’d found the right answer to the deceptively simple question “How do I change comment color within Vim?”: after creating ~/.vim, ~/.vim/after, and ~.vim/after/syntax, you can finally create the file ~/.vim/after/syntax/syncolor.vimβ€”and changes made to syntax highlight colors there applied the way that Lee and I expected them to.

Petting the shaggy dog

Hopefully, you’ve learned something along the way as you read this god-awful shaggy dog story of configuring a Linux application. Maybe you, too, just wanted to change some colors in a text editorβ€”in which case I’ve led you down an absurdly long path just to get to a relatively short answer.

But more importantly, I hope the exercise in full can serve as a broader exercise in troubleshooting. Happy Linux-ing!



Source link

Tags: AdventuresChangecolorscommentFigureJimLeeVim
Share197Tweet123Send

Related Posts

Technology

Apple to host virtual iPad event, may hint at new AirPods- The New Indian Express

April 22, 2021
Technology

The Morning After – Engadget

April 21, 2021
Technology

UK drone startup sees.ai gets go ahead to trial beyond-visual-line-of-sight (BVLOS) flights – TechCrunch

April 20, 2021
Technology

This electric fastback is the next Audi A6 e-tron, due in late 2022

April 19, 2021
Load More
  • Trending
  • Comments
  • Latest

Counting of votes for 7 MCs, 109 councils begins in Punjab

February 17, 2021

Punjabi singer Diljaan dies in road accident near Amritsar

March 30, 2021

Cricketers T Natarajan & Shardul Thakur Receive The Mahindra Thar As Promised By Anand Mahindra, Share Pics On Social Media

April 2, 2021

IPL Stars “Lucky” As COVID Ravages India, Says Delhi Capitals’ Chris Woakes

0

Diamond (Full HD) | Gurnam Bhullar | New Punjabi Songs 2018 | Latest Punjabi Song 2018

0

DILBAR Lyrical | Satyameva Jayate |John Abraham, Nora Fatehi,Tanishk B, Neha Kakkar,Dhvani, Ikka

0

IPL Stars “Lucky” As COVID Ravages India, Says Delhi Capitals’ Chris Woakes

April 23, 2021

Sidhu steps up attack on Capt, questions role in sacrilege case

April 23, 2021

‘Unite against BJP’: Mamata writes to Oppn leaders

April 23, 2021
ADVERTISEMENT
Chann Pardesi Media

24x7 Online News From India
Chann Pardesi Media is your news, entertainment, music fashion website. We provide you with the latest breaking news and videos straight from the entertainment industry.

Categories

  • Business
  • Entertainment
  • Health
  • Hindi News
  • Hindi Songs
  • India
  • International
  • Lifestyle
  • Panjab
  • Politics
  • Punjabi Songs
  • Sports
  • Technology
  • Travel
  • Youtube
No Result
View All Result

Recent Posts

  • IPL Stars “Lucky” As COVID Ravages India, Says Delhi Capitals’ Chris Woakes
  • Sidhu steps up attack on Capt, questions role in sacrilege case
  • ‘Unite against BJP’: Mamata writes to Oppn leaders
  • Home
  • Disclaimer
  • DMCA
  • Privacy Policy
  • Cookie Privacy Policy
  • Terms and Conditions
  • Contact

Copyright Β© 2021 - Chann Pardesi Media.

No Result
View All Result
  • Home
  • Radio
  • News
    • India
    • Punjab
    • International
    • Entertainment
  • Politics
  • Health
  • Business
  • Sports
  • Technology
  • Lifestyle
  • Video
  • Travel
  • Youtube

Copyright Β© 2021 - Chann Pardesi Media.