vimneovimvim-macros

How to multiline replace different characters in vim


i have this code snippet. I want to replace all the empty spaces with one space. In IntelliJ i would simply multi-cursor, jump to the -- use option + shift + left arrow then right arrow to select all the spaces and type a space which replaces them.

I'm new to vim and haven't found any solution that achieves this.

vim.opt.showmode = false                      -- we don't need to see things like -- INSERT -- anymore
vim.opt.showtabline = 2                       -- always show tabs
vim.opt.smartcase = true                      -- smart case
vim.opt.splitbelow = true                     -- force all horizontal splits to go below current window
vim.opt.splitright = true                     -- force all vertical splits to go to the right of current window
vim.opt.swapfile = false                      -- creates a swapfile
vim.opt.termguicolors = true                  -- set term gui colors (most terminals support this)
vim.opt.timeoutlen = 1000                     -- time to wait for a mapped sequence to complete (in milliseconds)
vim.opt.undofile = true                       -- enable persistent undo
vim.opt.writebackup = false                   -- if a file is being edited by another program (or was written to file while editing with another program), it 

Thanks.


Solution

  • In my semi-fresh install of Intellij IDEA CE, the correct sequence would be:

    <place cursor>
    <option><option>
    <down>
    <down>
    <down>
    <down>
    <down>
    <down>
    <down>
    <down>
    <down>
    <option-shift-left>
    <(option-shift-)right>
    <space>
    <escape>
    

    In Vim, you could simply use a multi-cursor plugin, there are a few, or do it…

    With a substitution:

    vip
    :'<,'>s/ \{2,}/ /<CR>
    

    where…

    With the "dot formula":

    / \{2,}<CR>
    cgn<space>
    .
    .
    .
    .
    .
    .
    .
    .
    .
    

    where…

    FWIW, I have a custom mapping that makes it possible to use a count before .:

    nnoremap . :<C-u>execute "norm! " . repeat(".", v:count1)<CR>
    

    which shortens the command above quite a bit at the cost of counting the lines so YMMV:

    / \{2,}<CR>
    cgn<space>
    9.
    

    Philosophical considerations: