ThePrimeagen/init.lua

remap.lua, Line: 42, Remap with double brackets and word replacement

itsf4llofstars opened this issue · 3 comments

Hello ThePrimeagen;
Thanks for the video "0 to LSP : Neovim RC From Scratch"

I had been looking for something to level-up my Neovim experience, I found it in your video.

I am a bit new to Git, and Github and wanted to try out the issues side of Github, I hope this
is the correct place for such a subject.

In your file

init.lua/lua/theprimeagen/remap.lua

line 42 you have the remap:

vim.keymap.set("n", "<leader>s", [[:%s/\<<C-r><C-w>\>/<C-r><C-w>/gI<Left><Left><Left>]])

Using this remap, with the cursor on the word "little", on the text:

Mary had a little lamb
little lamb little lamb
Mary had a little lamb
whose fleece was white as snow

produces a Neovim command line of:

:%s/\<little\>/little/gI

I'm not sure what this does as I've not seen the double open and closing brackets before. Testing it seems to have my cursor jumping all over the place. I have a similar remap to quickly replace a/all word[s] under the cursor. If the intent is to setup a quick replacement of the word under the cursor, I have:

-- setup a replacment for the word under the cursor globally
vim.keymap.set("n", "<leader>*", ":%s/\\<<C-r><C-w>\\>//g<left><left>")

-- setups a replacement for the word under the cursor with confirm
vim.keymap.set("n", "<leader>&", ":%s/\\<<C-r><C-w>\\>//gc<left><left><left>")

Using these on "little"

Mary had a little lamb
little lamb little lamb
Mary had a little lamb
whose fleece was white as snow

produces a Neovim command line of:

:%s/\<little\>/_/g[c]

Where the underscore is the location of the cursor and typing from here will replace the word[s] little with what you type either globally (no "c" in the remap) or with confimation (with "c" in the remap).

Using your video "Neovim from Scratch" I have since had to replace the backslashes
with escaped backslashes. I am guessing this has something to do with lua, as
my old-school init.vim file did not have the escaped backslashes.

Please let me know if I have stumble upon an issue, or if I've just not realized the actual intent of the mapping. Thanks for all you have done for us with the use of Vim and Neovim.

I was reluctant to start using Vim, you got me Vim-ing like a pro. I was not impressed with Neovim, you changed that. Now I feel that I am Coconut Oil level 10, thanks to your help on, Neovim with lua, Thanks again.

Cheers;
itsf4llofstars

The remap that the Primeagen has:

vim.keymap.set("n", "<leader>s", [[:%s/\<<C-r><C-w>\>/<C-r><C-w>/gI<Left><Left><Left>]])

Is nearly exactly the same as your remap:

vim.keymap.set("n", "<leader>*", ":%s/\\<<C-r><C-w>\\>//g<left><left>")

Except for how it starts replacing the word. The Primagen’s remap will keep the word so you can simply use <Backspace> to edit the word.

You can see that the word is repeated in the substitution section in the produced Vim command:

:%s/\<little\>/little/gI

Which simply means the word is kept and you can edit it instead of typing the whole word out, unlike yours where the entire word is deleted and you have to type a new word, because in the produced command for yours, the word isn’t repeated in the substitution section:

:%s/\<little\>//g

Another minor difference between your remap and the Primagen’s remap is that his replacement is case sensitive, as his has an I flag at the end of the command.



I'm not sure what this does as I've not seen the double open and closing brackets before.

The [[ and ]] creates a multi line raw string in Lua. This means that every character is as it is, and backslashes don’t have any special meaning like in \n and hence don’t have to be escaped. To add a new line to the string, you simply add a new line.



Using your video "Neovim from Scratch" I have since had to replace the backslashes
with escaped backslashes. I am guessing this has something to do with lua, as
my old-school init.vim file did not have the escaped backslashes.

That is correct, since you’re using regular strings in Lua instead of raw strings (you’re using double quotes for your strings), which would mean you’d have to escape the backslashes.

Thanks so much for this great explanation. I have looked at the differences between our two mappings and fully understand them now. Although I do like the blank space between forward slashes (where prime has the word and uses backspace) I am adopting the brackets and capital I. You have answered all my issues thanks. I'm going to close this issue with comment, still a bit new to issues and closing my own, so hope this is correct.

cheers;
itsf4llofstars

No worries! Glad to be of help to you!