Home

Awesome

nvim-px-to-rem

Easily work with rem in your css files

A Neovim plugin written in lua to convert px to rem as you type. It also provide commands and keymaps to convert px to rem and a virtual text to visualize your rem values.

<div align="center">

https://github.com/jsongerber/nvim-px-to-rem/assets/18051702/9ac54364-2115-4c9e-8354-f0991f11c82d

</div>

⚡️ Features

📋 Installation

use {
    'jsongerber/nvim-px-to-rem',
    config = function()
        require('nvim-px-to-rem').setup()
    end
}
Plug 'jsongerber/nvim-px-to-rem

" Somewhere after plug#end()
lua require('nvim-px-to-rem').setup()
-- add this to your lua/plugins.lua, lua/plugins/init.lua,  or the file you keep your other plugins:
{
    'jsongerber/nvim-px-to-rem',
    config = true,
    --If you need to set some options replace the line above with:
    -- config = function()
    --     require('nvim-px-to-rem').setup()
    -- end,
}

⚙ Configuration

-- Those are the default values and can be ommited
require("nvim-px-to-rem").setup({
    root_font_size = 16,
    decimal_count = 4,
    show_virtual_text = true,
    add_cmp_source = true,
    filetypes = {
        "css",
        "scss",
        "sass",
    },
})
OptionDescriptionDefault value
root_font_sizeThe font size used to convert px to rem16
decimal_countThe number of decimal to keep when converting px to rem4
show_virtual_textShow the rem value converted in px in a virtual texttrue
add_cmp_sourceAdd a nvim-cmp source to convert px to rem as you type (require nvim-cmp)true
filetypesThe filetypes to enable the plugin on{"css", "scss", "sass"}

nvim-cmp integration

If you want to be able to convert px to rem as you type you need to install nvim-cmp and add the plugin to your cmp sources:

require("cmp").setup({
    -- other config
    sources = cmp.config.sources({
        { name = "nvim_px_to_rem" },
        -- other sources
    }),
})

🧰 Commands

CommandDescription
:PxToRemCursorConvert px to rem under cursor
:PxToRemLineConvert px to rem on the whole line

📚 Keymaps

[!WARNING] This plugin used to provide default keymaps but it was removed as it was poorly chosen. If you used default keymaps you will need to set them yourself.

You can set keymaps like so:

vim.api.nvim_set_keymap("n", "<leader>pxx", ":PxToRemCursor<CR>", { noremap = true })
vim.api.nvim_set_keymap("n", "<leader>pxl", ":PxToRemLine<CR>", { noremap = true })

⌨ Contributing

PRs and issues are always welcome. Make sure to provide as much context as possible when opening one.

🎭 Motivations

Inspired by the VS Code plugin px to rem & rpx & vw (cssrem).
There is two vim plugin to convert px to (r)em but those were missing some feature I wanted such as the virtual text and the nvim-cmp integration:

📝 TODO

📜 License

MIT © jsongerber

Shameless plug

See my other plugins: