Skip to content

yujinyuz/very-magic-slash.nvim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

very-magic-slash.nvim 🧙

Installation

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

Usage

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

Why?

This plugin may not be for you if you are comfortable with vim's current behavior when searching.

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