Skip to content

yujinyuz/vms.nvim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

[v]ery [m]agic [s]lash.nvim 🧙

demo.mov

Installation

lazy.nvim
{
  'yujinyuz/vms.nvim',
  event = 'VeryLazy',
  opts = {},
}
Packer
require('packer').startup(function()
  use({
    'yujinyuz/vms.nvim',
    config = function()
      require('vms').setup()
    end,
  })
end)
Paq
require('paq')({
  { 'yujinyuz/vms.nvim' },
})
Neovim native package
git clone --depth=1 https://github.com/yujinyuz/vms.nvim.git \
  "${XDG_DATA_HOME:-$HOME/.local/share}"/nvim/site/pack/vms/start/vms.nvim

Usage

  • Press / in normal mode, and it will automatically become /\v
  • Type :%s/, and it becomes %s/\v
  • Also works with :g and g!.

Why?

You probably won't need this plugin if you haven't heard of magic mode in vim, yet. This is just a convenience plugin so if you're comfortable manually typing :s/\v then that should be enough.

You can have a very basic implementation of this plugin by copying the lines below which was extracted from my config:

-- Automatically add vim magic to search and substitute
-- See: https://vim.fandom.com/wiki/Simplifying_regular_expressions_using_magic_and_no-magic
vim.keymap.set({ 'n', 'v' }, '/', '/\\v', { noremap = true })
vim.keymap.set({ 'n', 'v' }, '?', '?\\v', { noremap = true })

vim.keymap.set('c', '/', function()
  -- Get the previous command-line text
  local line = vim.fn.getcmdline()
  -- Check if the previous text is "%s"
  if line == '%s' or line == "'<,'>s" then
    return '/\\v'
  end

  return '/'
end, { noremap = true, expr = true })

vim.keymap.set('c', '?', function()
  -- Get the previous command-line text
  local line = vim.fn.getcmdline()
  -- Check if the previous text is "%s"
  if line == '%s' or line == "'<,'>s" then
    return '?\\v'
  end
  return '?'
end, { noremap = true, expr = true })

That worked for my basic use case, not until I learned more about the :h global command and that I also wanted to apply very magic to it as well. But then it had an inverse equivalent, which is :h vglobal

The basic version also does not work with complicated ranges such as :.,$ which means from the current line up until the end of file.

That made my config quite complicated, so I extracted it into this plugin.

Related Resources

Acknowledgments

  • Inspired by wincent/loupe plugin where I just extracted the very magic slash part.

Known Issues

  • None so far