Vim Jump to CSS Definition from Class or ID in HTML
Reading time: 2min
Recently I came across a great Vim function on Stack Overflow. Once added
to your .vimrc
, it allows you to jump from a class or ID within an HTML
document directly to the associated styles in your CSS.
Place this function in your .vimrc
file:
I scoped the function's query to my style directory to speed up the search.
Simply change */styles/**/*.scss
to reference your SCSS/LESS/CSS/etc directory
(don't forget to change the extension if you are not using SCSS).
Inside Vim, open an HTML document and place your cursor over a class or ID and run the function by typing:
Once the query finishes, Vim will open the file and take you to the correct style definition.
Bonus: If you love your keyboard shortcuts like I do, add a quick shortcut to your
.vimrc
:
This will allow you to simply hit your leader key followed by a closing bracket:
]
to call the function.