5

I would like to auto-indent text blocks that I have just put. At a minimum, I would like to retab the new block of text, since otherwise I sometimes end up with tabs in files that shouldn't have tabs when I copy them from files that do have tabs without noticing.

2
  • This does not directly answer your question but regarding visibility of tabs, I use this :set listchars=tab:\|\ ,trail:_,extends:>,precedes:<,nbsp:% list
    – cychoi
    May 30, 2015 at 0:20
  • @cychoi I have an autocmd on buffer-load that checks for tabs and uses something like that to turn them bright yellow if they are present, but otherwise I don't want to see leading spaces so I only include trailing spaces in listchars. I suppose I could include tabs as well, though.... May 30, 2015 at 0:25

1 Answer 1

2

This is my current solution, but I'm certianly open to better ones:

nnoremap p pV`]=
nnoremap P PV`]=

This makes use of the ] built-in mark, which is the "last character of the previously changed or yanked text" (from :help marks).

I'd prefer not to remap primitive fundamental keys such as p, and this won't work with other methods of pasting (e.g. the middle-click in X11). Also, I haven't used this for very long so I'm not sure yet if there are any behavioral oddities.

6
  • 3
    You could also do [p or ]p.
    – romainl
    May 28, 2015 at 20:12
  • @romainl I didn't know about that, but that does indeed seem to do what I want. May 28, 2015 at 21:09
  • 1
    @romainl Actually, that appears to remove leading spaces when pasting below a blank line, which is not necessarily what I want. May 28, 2015 at 21:52
  • Which is why there are both a [p (which is probably better written as ]P) and a ]p
    – lcd047
    May 29, 2015 at 5:01
  • [p and ]p are to be used when you want to paste while keeping the indent of the line above or below. They are not magical replacements for P and p.
    – romainl
    May 29, 2015 at 7:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.