nanotee / nvim-lua-guide

A guide to using Lua in Neovim
5.83k stars 220 forks source link

Create keymap to open new file adjacent to current file #92

Closed justb3a closed 2 years ago

justb3a commented 2 years ago

Hi,

I'm struggeling a while now with this and tried serveral solutions. My goal is to use the edit command with the path of the currently edited file filled in:

nnoremap <leader>e :e <C-R>=expand("%:p:h") . "/" <CR>
vim.api.nvim_set_keymap('n', '<leader>e', '<cmd>e ???<cr>', {silent = true, noremap = true})

Using it directly just works as expected.

I tried to wrap <C-R> in the termcodes function and several other ways, but none of them worked. Maybe there is even a better way to solve this with lua. Any help is appreciated 🙏🏻

nanotee commented 2 years ago

Hi,

Try replacing <Cmd> with a :. <Cmd> executes the command directly without changing modes which is why <C-R> doesn't work.

(As a sidenote, questions like these are more likely to get an answer on dedicated support forums like the Neovim Discourse)

justb3a commented 2 years ago

Tried this as well, didn't work at first glance. Now I tried it again, I was waiting for the path being shown so that I could extend it. Now I discovered that only if you type something, the path will appear and then it works. Thanks.

(Thanks for the sidenote link ;) )

gegoune commented 2 years ago

You shouldn't have to type anything apart of the keymap itself. I suspect you have another keymap for <leader>e<something> and nvim waits for more input. Try: :nmap <leader>e to see what mappings you have set up.

nanotee commented 2 years ago

Now I discovered that only if you type something, the path will appear and then it works.

Does removing silent = true fix it?

clason commented 2 years ago

PSA: There's a full Lua API now (vim.keymap.*), so I'd probably focus on just that now.

justb3a commented 2 years ago

I use a custom keymap function, silent = true was set by default 🤦🏻‍♀️. Thanks for your help! 🚀