Vim Tips Wiki

Talk:Remap join to merge comment lines

Back to page

Revision as of 05:54, March 29, 2011 by Sashahart (Talk | contribs)

1,619pages on
this wiki

bug in the visual mapping

There is (what users would likely see as) a bug in this snippet:

vnoremap <silent> <buffer> J :<C-U>'<+1,'>s/^\s*"\s*/<Space>/e<CR>gvJ

- running this map on a highlighted region started with one or more blank lines strips the comment leader from the first nonblank line. This is because the first nonblank line is only protected if it is the first line of the selection, but when there is a preceding line this is not the case.

This could be amended to search for the first non-whitespace like this:

vnoremap <silent> <buffer> J :<C-U>'<;/\s/+1,'>s/^\s*#\s*/<Space>/e<CR>gvJ

That doesn't work if the first line is one of the comment lines, so we can have it back up a line before searching:

vnoremap <silent> <buffer> J :<C-U>'<;-1/\s/+1,'>s/^\s*#\s*/<Space>/e<CR>gvJ

This works all right for me, not that I like it much... It would be better to actually search for the first line with a comment character, but this is becoming a really awkward one-liner. It's already pretty hard to read, and still only works in visual mode - so binding it to J will mean inconsistent results without a second solution for nonvisual modes. But that second solution can easily handle the visual case too, and not require a mapping with different behavior. So there are several good reasons to wrap this up in a function or command. Is there any good reason to prefer the "opaque one-liner" approach?

Sashahart 05:22, March 29, 2011 (UTC)

Around Wikia's network

Random Wiki