3

My cursor is on the f of the first line. I'd like to move the word so that it aligns with the word below it. I know I could b i spaces until they align. I'd like to avoid getting in insert mode.

Vim_word_align

  • 2
    Given that you want to insert text between two words, I don't think you have other option than going into insert mode. You can speed up by 3i<space> instead of i<space><space><space>. – nobe4 Dec 20 '16 at 18:28
5

Without entering insert mode (not sure if it's better though):

Example text (cursor on C of the first line):

A  BBCBB
A    BBCBB

Type:

bjywkP

Decomposing:

bj     go back to the start of the word, one line down (below the first B)
yw     yank the next word (being the whitespaces)
k      move up
P      past before the cursor

Edit: Thanks to antony for suggesting using yw instead of vtBy.

  • No so difficult to do once I attach to a map leader or a macro. Thanks. – zundarz Dec 20 '16 at 19:05
  • 3
    You could also use yw instead of vtBy. – Antony Dec 21 '16 at 23:30
1

The following approach is ~3 keystrokes longer than @nobe4's solution, but it answers the question in the title

How to move word under current cursor to begin at current cursor?

more generally: It doesn't depend on another line to yank the whitespace from.

Steps:

h                                          Move one char to the left. Skip this step if you have :set selection=exclusive; :behave mswin in mswin.vim does this.

v b              'Take measure' of the distance you want to shift your text with spaces.

y P              Duplicate that area.

g v                   (Re-)select the duplicate.

r                                            Replace all selected characters by char…

Space

(You cannot shorten the    v ‹measurement movement›  y  part   to    y ‹measurement motion›   because you need the prior selection for re-selecting it afterwards.)


Here's a different example where you can apply the above introduced pattern
v ‹measurement movement› y   P  gv  r Space :

Say you've got

" ↓ Normal mode command(s) go here
cnoremap <expr> <A-b> &cedit. 'b' .'<C-c>'

and you want to align the over the b:

"                              ↓ Normal mode command(s) go here
cnoremap <expr> <A-b> &cedit. 'b' .'<C-c>'

Steps:

First, move the cursor below the (on the o). Then:

v

2 t b or f '                                             'Measure' the shifting distance by selecting the area to shift over.

y P g v r Space                       Duplicate this area, then whiten the duplicate.

g v                                                                                                                                                         Re-select the whitened area.

d k P                                                                                                                        Move it where you need it.


(
In the case of this example, you could save 
first moving into the lower line to take measure there and finally moving the whitespace  
by taking measure 'by eye' directly on the upper line, e.g.:

^ w                                                                                                                                                  Move to the start of the text you want to shift.

v                                                                                                                                                                              Select…

2 t e or f h                                           …till before the char  till (before) which you want to shift. That's the one above the b (which is the line's second e. (Before that  is the line's first h.))

y   P  gv  r Space                       (As usual: Duplicate and whiten the selected text area.)

)

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.