Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I insert a specific character along all lines on a specific column in Vim?

Tags:

vim

 Dates
=======

 Name                                   | Date
-------------------------------------------------
* Battle of the Plains of Abraham       | September 13, 1759
* Proclamation Act                      | October   07, 1763
* Stamp Act                             | March     22, 1765
* Guy Carleton becomes Governor         | April     07, 1766
* Boston Tea Party                      | December  16, 1773
* Quebec Act                            |
* Declaration of Independance           | <====== # How do I insert this bar character
* Treaty of Paris                                 # along the whole column?
* Constitutional Act                
* French Revolution                 
* War of 1812

I want to be able to insert that bar character without having to manually go and insert it. While this is not syntactically correct, this is supposed to be markdown.

like image 969
saif Avatar asked May 22 '11 20:05

saif


People also ask

How do you add a character at the end of each line in Vim?

On a character in the first line, press Ctrl-V (or Ctrl-Q if Ctrl-V is paste). Press jj to extend the visual block over three lines. Press $ to extend the visual block to the end of each line.


2 Answers

I'm only a beginner, but here's what I do:

  • C-v to enter Visual Block (Use C-q on windows)
  • Select column (motion keys hjkl)
  • I
  • Enter text
  • Esc
like image 196
cnicutar Avatar answered Oct 11 '22 04:10

cnicutar


%s/$/                                        /
v/|/s/^\(........................................\)/\1|/
%s/  *$//

Some notes:

  • You will need a : in front of each line if you are in vi's normal visual mode
  • Alternatively, you can put those commands in a command file and type $ ex file < cmds but in that case add an x as the fourth line
  • This works by appending blanks to each line, then changing the right one to a | for lines that don't already have a |, and then deleting any trailing blanks
like image 28
DigitalRoss Avatar answered Oct 11 '22 05:10

DigitalRoss