Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Placing characters at a specific column in vi

Tags:

vim

vi

Is there a way to place a character at a specific line in vim, even if the line is short?

For example, I'm contributing to a project which has a comment block style which is 79 columns wide, with a comment character at either end e.g.

!--------------!
! Comment      !
! More Comment !
!--------------!

but it's really annoying to space over to it, even with guessing large numbers (35i< SPACE>< ESC>)

Is there a simple command which will do this for me, or a macro or something I could write?

like image 633
Samizdis Avatar asked Jan 18 '13 14:01

Samizdis


3 Answers

set ve=all

then you could move (h,j,k,l) to anywhere you want. no matter how short your line is.

there are 4 options block, all, insert, onemore for details:

check :h virtualedit

in this way, after you typed short comment, then type <ESC>080l to go to the right place to the tailing !

you can map it too, if it is often used

then it works like this:

enter image description here

like image 170
Kent Avatar answered Oct 02 '22 11:10

Kent


Put this in your .vimrc file and restart vim:

inoremap <F1> <C-r>=repeat(' ', 79-virtcol('.'))<CR>!<CR>

With this just press F1 (or whatever key you map) in insert mode after entering comment text to automatically pad with spaces and insert a ! in column 79.

Another simple way is to keep an empty comment box of the correct size somewhere, yank/paste it where needed and just Replace the spaces in it with your comment each time.

If you want to reformat a box that is too short, one way is to start from the comment in your example, make a Visual Block (Ctrl+v) selecting the single column just to the left of its right-hand edge, yank it (y), then repeatedly paste it (p). This will successively move the entire right-hand side of the comment one step, extending the box rightward. Repeat until it has the desired length.

If you already entered the comment text, you can use a macro to add the right-hand ! mark at the correct place. For example, record a macro (qa) that appends more characters than are needed for any line (e.g. 80ASpaceEsc), then use the goto column (|) to go to the correct place (79|) and replace the excess characters from there (C!Esc), then move down one line (j), and stop recording (q). Repeating this macro (@a) then "fixes" each line in turn and moves to the next. In total: qa80A<space><esc>79|C!<esc>jq and then @a whenever needed. Sounds complex but is convenient once you have it.

like image 34
Anders Johansson Avatar answered Oct 02 '22 11:10

Anders Johansson


There are certainly good answers here already, particularly the virtualedit answer. However, I don't see the method which seems most intuitive to me. I would create an empty line for the last row of the comment which is just surrounded by the exclamation points. Then I would yank and paste a new copy of the empty line, go to the old empty line and go to the point at which I want to edit and use overstrike mode (R) to add my text without affecting the placement of the ending exclamation point.

Sometimes the simplest methods, while slightly more clunky, are the easiest to use and remember.

like image 33
Binary Phile Avatar answered Oct 02 '22 10:10

Binary Phile