David.Chu.ca
David.Chu.ca

Reputation: 38644

how to move a block or column of text

I have the following text as a simple case:

...
abc xxx 123 456
wer xxx 345 678676
...

what I need to move a block of text xxx to another location:

...
abc 123 xxx 456
wer 345 xxx 678676
...

I think I use visual mode to block a column of text, what are the other commands to move the block to another location?

Upvotes: 45

Views: 72822

Answers (5)

SergioAraujo
SergioAraujo

Reputation: 11800

Using an external command "awk".

%!awk '{print $1,$3,$2,$4}' test.txt 

With pure vim

:%s,\v(\w+) (\w+) (\w+) (\w+),\1 \3 \2 \4,g

Another vim solution using global command

:g/./normal wdwwP

Upvotes: 0

Kemin Zhou
Kemin Zhou

Reputation: 6891

One of the few useful command I learned at the beginning of learning VIM is :1,3 mo 5 This means move text line 1 through 3 to line 5.

Upvotes: 16

Paul
Paul

Reputation: 13238

You should use blockwise visual mode (Ctrl+v). Then d to delete block, p or P to paste block.

Upvotes: 67

Klinger
Klinger

Reputation: 4970

Try the link.


Marking text (visual mode)

  • v - start visual mode, mark lines, then do command (such as y-yank)
  • V - start Linewise visual mode
  • o - move to other end of marked area
  • Ctrl+v - start visual block mode
  • O - move to Other corner of block
  • aw - mark a word
  • ab - a () block (with braces)
  • aB - a {} block (with brackets)
  • ib - inner () block
  • iB - inner {} block
  • Esc - exit visual mode

Visual commands

  • > - shift right
  • < - shift left
  • y - yank (copy) marked text
  • d - delete marked text
  • ~ - switch case

Cut and Paste

  • yy - yank (copy) a line
  • 2yy - yank 2 lines
  • yw - yank word
  • y$ - yank to end of line
  • p - put (paste) the clipboard after cursor
  • P - put (paste) before cursor
  • dd - delete (cut) a line
  • dw - delete (cut) the current word
  • x - delete (cut) current character

Upvotes: 29

John Ellinwood
John Ellinwood

Reputation: 14531

  1. In VIM, press Ctrl+V to go in Visual Block mode
  2. Select the required columns with your arrow keys and press x to cut them in the buffer.
  3. Move cursor to row 1 column 9 and press P (thats capital P) in command mode.
  4. Press Ctrl+Shift+b to get in and out of it. (source)

Upvotes: 8

Related Questions