How to write this same mapping in lua

The mapping below uses a ternary vimL code

-- https://stackoverflow.com/a/26504944/2571881
:nnoremap <silent><expr> <Leader>h (&hls && v:hlsearch ? ':nohls' : ':set hls')."\n"

I also have this map helper:

local map = function(key)
    -- get the extra options
    local opts = {noremap = true}
    for i, v in pairs(key) do
        if type(i) == 'string' then opts[i] = v end
    end

    -- basic support for buffer-scoped keybindings
    local buffer = opts.buffer
    opts.buffer = nil

    if buffer then
        vim.api.nvim_buf_set_keymap(0, key[1], key[2], key[3], opts)
    else
        vim.api.nvim_set_keymap(key[1], key[2], key[3], opts)
    end
end

It allows me to create mappings like this:

map{'n', '<space>', '/'}

You write a function that performs that logic and map it to the binding you want.

function foo()
if vim.opt:hlsearch and vim.v.hlsearch then
  -- stuff
else
  -- more stuff
end
end

Then you set the mappings like the many examples out there. Using require(‘myfunc’)

1 Like

I have found the documentation on the site
https://doc.fastgit.org/en-gb/guide.html#the-usage-of-web

In fact is pretty simple, just follow these instructions:

GitHub operations, you can use the command clone like:

git clone https://github.com/author/repo

To use FastGit, you need to change it into:

git clone https://hub.fastgit.org/author/repo

Just like what you see, FastGit is physically a proxy of GitHub, and what you need to do is just replacing the URL.

How is it?

I want to map this nmap <silent>mn :Zoom<cr> into its equivalent in lua. Something that would be put into init.lua for example.

Now I am using a different map helper

-- map helper
local function map(mode, lhs, rhs, opts)
    local options = {noremap = true}
    if opts then options = vim.tbl_extend('force', options, opts) end
    vim.api.nvim_set_keymap(mode, lhs, rhs, options)
end

The above function helps me writing maps like these:

-- showme the most recent edited files
map('n', '<leader>o', ':lua require("telescope.builtin").oldfiles()<cr>')
-- map('n', 'u', [[:norm u<cr>]])

map('n', '<F6>', '<cmd>lua require("tools").toggle_colors()<cr>')

-- -- jumping on change list
-- map('n', '<m-o>', 'g;')
-- map('n', '<m-i>', 'g,')

-- Resizing Windows (M is alt, C is Ctrl)
map("n", "<M-C-Up>", ":resize +2<CR>")
map("n", "<M-C-Down>", ":resize -2<CR>")
map("n", "<M-C-Left>", ":vertical resize -2<CR>")
map("n", "<M-C-Right>", ":vertical resize +2<CR>")

By the way I have a function to swich colorschemes

local M = {}


M.toggle_colors= function()
    local current_color = vim.g.colors_name
    if current_color == 'monokai' then
        vim.cmd('let ayucolor="light"')
        vim.cmd('colorscheme ayu')  
        vim.cmd('echo g:colors_name')
    elseif current_color == 'ayu' then
        vim.cmd('set background=light')
        -- local custom_paper = require'lualine.themes.papercolor_light'
        -- require'lualine'.setup{ options = {theme = custom_paper}}
        vim.cmd('colorscheme PaperColor')
        vim.cmd('echo g:colors_name')
    elseif current_color == 'PaperColor' then
        vim.cmd('colorscheme gruvbox-flat')
        vim.cmd('set background=dark')
        vim.cmd('echo g:colors_name')
    elseif current_color == 'gruvbox-flat' then
        vim.cmd('colo zenflesh')
        vim.cmd('echo g:colors_name')
    elseif current_color == 'zenflesh' then
        vim.cmd('colo zenbones')
        vim.cmd('echo g:colors_name')
    else
        vim.cmd('colo monokai')
        vim.cmd('echo g:colors_name')
    end
end

return M