0

I would like to easily transform these two lines :

One line.
long line with many words in this example

into the line :

One long line with many words in this example line.

How I currently do it :

  • place cursor on the l of long
  • enter visual mode
  • go to the end of the line with $ key
  • go one character backward (to deselect the line-break character)
  • hit x to cut the words in the line and leave visual mode
  • place cursor on the space betwen One and line
  • hit p
  • add a space after example

How I wish it was possible to do it :

  • place cursor anywhere on second line
  • hit shift-v to visualize the second line
  • hit x (optionally y to keep second line)
  • place cursor on the space betwen One and line
  • hit a magick key
  • if needs be, insert the space

Remarks :

  • In general I would like not to keep the trailing blank characters from the second line.
  • it is already possible to paste a line that you cut with shift-v then x but it will be pasted as a line, before (or after) the current line.
  • if the two lines are side by side and if one simply wants to move the second line at the end of the first one, one can use the join command (that has options regarding spaces to insert).

Any help is appreciated !

1

To be honest I don't see the point of creating a custom mapping (what you called magic key) to a simple scenario like this. You have many ways to achieve this, without even using visual mode. One approach could be:

gJBdf.A <esc>p
  • gJ: on the first line, join with the second without including spaces
  • w: move to second word
  • df.: delete 'one.'. This could be replaced with d<count>f<char> or d/<pattern><cr>
  • A<space><esc>: insert an space at the end
  • p: past the deleted contents

If this has to be repeated several times it would be easier to record a macro using some pattern on the target lines.

Going back to your magic key:

nnoremap <F3> i<c-r>"<backspace><space><esc>

The ctrl+r in insert mode places the contents of the specified register in place (the unnamed register " in this case, which holds the last copied/deleted text), so it will drop the end of the first line to next line due to the carriage return deleted along the second line. The mapping then replaces this \n with a space and leaves insert mode. Note that this is based on the 'backspace' options containing eol, which is part of $VIMRUNTIME/mswin.vim. This option can be saved and restored by the mapping if you don't use it, or you could join the lines in insert mode.

  • Shouldn't gJwdf.A <esc>p be gJBdf.A <esc>p ? (tested without any configuration : vim -u NONE).gJBdf.A <esc>p is better than what I had. <br>Your mapping works well with my setup (amix's vimrc) but no without any configuration. Thanks a lot ! – Gabriel Devillers Jul 13 '16 at 11:44
  • @voxel_f indeed, I use a mapping on gJ (from superuser.com/a/890587/141923) and the mapping I suggested depends on a option. I edited the question with the corrections, but please note that the <F3> mapping is more an example, will could/should adapt it to your needs. – mMontu Jul 13 '16 at 12:15
  • I chose to map i<c-r>"<backspace><space><esc> on <Leader>p and I selected your answer because I like to keep on using Vx to cut the line. – Gabriel Devillers Jul 18 '16 at 7:50
1

You could use something like that:

nnoremap <leader>v 0v$hd

Then your key flow would become

  • Go anywhere on the second line
  • Hit leaderv to get yank the content of the second line and delete it
  • Go to the space first line (e.g. fspace
  • Hit p to put the second line between the two words.

(You could also remove the last d of the mapping to make leaderv only visually select the line to let you do whatever you want with it)


EDIT As OP pointed out in the comment, using ^ instead of 0 is a good way to get rid of leading whitespaces:

nnoremap <leader>v ^v$hd
  • Thanks a lot ! It works nice with my setup (but again not when using vim -u NONE, however it might not be a good idea to test this way). Advantage over @mMontu's answer : by replacing 0 with ^, trailing blank characters will be skipped. – Gabriel Devillers Jul 13 '16 at 12:04
  • I don't see why it wouldn't work with vim -u NONE (Remember that the default leader is \ ). Also yes replacing 0 by ^ can be a good idea. – statox Jul 13 '16 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.