5

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?

Samizdis
  • 1,591
  • 1
  • 17
  • 33
  • possible duplicate of http://stackoverflow.com/questions/3364102/how-to-fill-a-line-with-character-x-up-to-column-y-using-vim – Huluk Jan 18 '13 at 14:27
  • You might want to check out [UltiSnips](http://www.vim.org/scripts/script.php?script_id=2715) and the [`box` snippet](https://github.com/SirVer/ultisnips/blob/master/UltiSnips/all.snippets#L82). If it doesn't get you there, it may get you close to a solution. – John Szakmeister Jan 18 '13 at 15:17

5 Answers5

9
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

Kent
  • 189,393
  • 32
  • 233
  • 301
2

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.

Anders Johansson
  • 3,926
  • 19
  • 19
  • 1
    `col('.')` is a wrong thing to use here. Use `virtcol('.')` as only it works with characters with width not matching byte length (normally tabs and unicode characters) (`col()` returns byte offset from the start of line and `virtcol()` returns number of display cells occupied). – ZyX Jan 18 '13 at 18:03
1

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.

Binary Phile
  • 2,538
  • 16
  • 16
0

I usually just copy and paste an existing line in the comment block (or copy one from another file) and then modify it. If the text you're replacing is about the same size as what you want to write (e.g., if you're changing the author's name), you probably only need to add or delete a few spaces to make everything line up. It's a lot less painful than spacing out to the desired width.

If you ever have a block that gets too long this way, a neat trick is to place the cursor on the 79th column and press dw. That will clear all spaces up to the ! at the end.

Michael Kristofik
  • 34,290
  • 15
  • 75
  • 125
0

My AlignFromCursor plugin offers commands and mappings that align only the text to the right of the cursor, and keep the text to the left unmodified. So, with the cursor in the whitespace left of the trailing !, you can align it with <Leader>ri.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324