2 votes

How to encapsulate a visual selection with preprocessor logic?

I want to Visually select some lines, and then apply a substitution so that the tags are added before and after it. Like this, for example, xnoremap <leader>0 "="#if 0\n#endif\n"&...
Matt's user avatar
  • 20.6k
2 votes

How to encapsulate a visual selection with preprocessor logic?

There are many, many ways to do it. What I'm currently using In lh-cpp, I have the following definition to wraps the lines, thanks to my lh-brackets plugin: :Brackets #if\ 0 #endif -insert=0 -nl -...
Luc Hermitte's user avatar
  • 17.3k
2 votes

How to encapsulate a visual selection with preprocessor logic?

Without knowing at least part of the source code, it's hard to provide a "best" answer. To expand on my earlier comment, let me lay out two other possible ways to add those preprocessor ...
Friedrich's user avatar
  • 1,693
2 votes
Accepted

How to evaluate vim script in the substitution field and put text with it?

You can find help about the \= sub-replace-special command under :help :s\= (agreed, that's not very obvious.) In any case, you can see that it says "When the {string} starts with \= it is ...
filbranden's user avatar
  • 28.7k
2 votes

How to repeat substitution on the consequent lines?

I would do: :2,3s If you don't specify a search pattern or a replace string the previous one is used.
Vivian De Smedt's user avatar
2 votes

Can't stop the global command using subcommand :substitute (:s) with the check flag (c)

The :g command enter in a loop that q, Esc doesn't break For your case I would do: :%s/.*\d.*//gc Remark: Ctrl c break the loop of Vim and Neovim (for both nvim and nvim-qt) but not for gVim.
Vivian De Smedt's user avatar
2 votes
Accepted

Best way to keybind subsequent substitutions in visual mode

To avoid the double '<,'> problem the old way is to add <C-u> at the start of the mapping: vim.api.nvim_set_keymap( "v", "<Leader>q", ":<C-u>'<...
Vivian De Smedt's user avatar
1 vote

Best way to keybind subsequent substitutions in visual mode

The point is that Vim switches from mode to mode as it reads buttons. No matter if they come from keyboard or mapping. So you only start from Visual mode. But then you get into cmdline, then into ...
Matt's user avatar
  • 20.6k
1 vote

Can't stop the global command using subcommand :substitute (:s) with the check flag (c)

:help :g can definitely be aborted with <C-c>, as per the documentation: Use "CTRL-C" to interrupt the command. The /g, /c, etc. flags are features of :help :s. :g doesn't have that ...
romainl's user avatar
  • 40.1k
1 vote

Add newline after every paragraph in log file in vi editor

I would do: :%s/\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2}/\r&/ Or: :%s/\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d/\r&/ It inserts a new line after every date in the format you specified.
Vivian De Smedt's user avatar
1 vote
Accepted

Search and replace text in visual range but not entire line

You could do: :'<,'>s/\%V /-/ Where \%V match only if the next character is within the visual range. For more information see :help /\%V
Vivian De Smedt's user avatar
1 vote

in-place replacement of code in HTML documents with TOhtml?

OK, I need to think about something else than freaking Magento. Pandoc would help with the first step but the rest can be done with pure Vim commands… Tell :help :TOhtml to only generate fragments: :...
romainl's user avatar
  • 40.1k
1 vote

How to repeat substitution on the consequent lines?

When I find myself with a command to repeat that may not lend itself to easy repetition... I resort to macros, and I typically use the "q register for these quick macro situations: qq The ...
CrashNeb's user avatar
  • 203
1 vote

How to repeat substitution on the consequent lines?

I use the following (sensible) Visual-mode mapping for &: xnoremap & :&&<cr> (Note that the duplicate :&& preserves flags, too; I actually also have :nnoremap & :&...
D. Ben Knoble's user avatar
  • 25.8k
1 vote

How to repeat substitution on the consequent lines?

You should have provided a :help :range to :help :s in the first place: :.,+2s/\$\(\d\)/\1/ Anyway, you can use :help :&, here, which also takes a range: :.+,.+2& where: the range starts at ...
romainl's user avatar
  • 40.1k
1 vote

How to encapsulate a visual selection with preprocessor logic?

After a lot of trial and error, I found something that works: s/\(\%V\_.*\%V\n\)/#if 0^M\1#endif^M/ Not sure how or why this one works, but the others didn't though. If someone has an explanation, ...
Opifex's user avatar
  • 217
1 vote
Accepted

Is there a way to substitute a text that has regex formatted number ranges and replace with all combinations?

I doubt a simple solution exist for any regular expression but for you case you could: Define the following function: function! GenerateLineRange(start, end, i1, i2) let ret = '' for i in ...
Vivian De Smedt's user avatar
1 vote
Accepted

Command to separate nested brackets over multiple lines

This is easy to improvise in two steps: :s/[(),]\n\@!/&\r/g :'[,norm == or as a one-liner: :s/[(),]\n\@!/&\r/g|'[,norm == where: we substitute every (, ), or , in the current line with ...
romainl's user avatar
  • 40.1k
1 vote
Accepted

How can I replace duplicate line prefixes with blanks?

I would run the following commands: :g/^./move 0 :%s/\v^(.*)\ze.*\n\1/\=substitute(submatch(1), '.', ' ', 'g') :g/^./move 0 g/^./move 0 moves the text upside down. Since the logic has to run from ...
Vivian De Smedt's user avatar
1 vote
Accepted

What is the syntax to use character classes inside Vim regex bracket expression?

You are right the character atom (e.g.: \s, \S, \d, ...) cannot be part of a collection ([]) To get the reference in the doc you can look for the NOTE in the section: :help collection: NOTE: The other ...
Vivian De Smedt's user avatar
1 vote

Substitution command in neovim remaps not working

why not write it directly in unix format? :w ++ff=unix
Christian Brabandt's user avatar
1 vote
Accepted

How to evaluate Lua in substitution field instead of vim script?

What you could do is: :'<,'>s/\vs([A|B])\.size\(\)/\=luaeval('({A="a",B="b"})["'..submatch(1)..'"]')/g The expression first build the ({A="a", B="b&...
Vivian De Smedt's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible