Vim Tips Wiki
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
FANDOM
Fan Central
BETA
Games
Anime
Movies
TV
Video
Wikis
Explore Wikis
Community Central
Start a Wiki
Don't have an account?
Register
Sign In
Sign In
Register
Vim Tips Wiki
1,649
pages
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
Editing
Format pasted text automatically
Back to page
Edit
Edit source
View history
Talk (0)
Edit Page
Format pasted text automatically
We recommend that you
log in
before editing. This will allow other users to leave you a message about your edit, and will let you track edits via your
Watchlist
.
Creating an account
is quick and free.
The edit appears to have already been undone.
Anti-spam check. Do
not
fill this in!
{{TipImported |id=272 |previous=271 |next=273 |created=2002 |complexity=basic |author=RobertKellyIV |version=6.0 |rating=32/11 |category1=Usage |category2= }} If a user would like to paste text into a buffer and have that text indented properly so that the text matches surrounding indents, the following command can be given: <pre> ]p </pre> Some users prefer to have all of their text pasted with indenting intact. In order to make this easier, the <code>]p</code> command can be mapped to the <code>p</code> command, so that whenever <code>p</code> is used, <code>]p</code> will be executed. The following can be added to the .vimrc to accomplish this: <pre> :nnoremap p ]p :nnoremap <c-p> p </pre> This simply maps normal mode <code>p</code> to what <code>]p</code> normally does. While <code>ctrl+p</code> now performs just <code>p</code> without the indenting functionality. ==Correcting bad indent while pasting== Unfortunately, the <code>]p</code> command will only adjust indent to match the current line, it will not re-indent the pasted text to correct it according to your current indent rules. You can do this as well, using the special mark, <code>`]</code>. This will jump to the last character of the paste, so you could change the p mapping above to: <pre> :nnoremap p p=`] </pre> This takes advantage of the fact that a paste operation will place the cursor at the beginning of the inserted text, and uses the <code>=</code> operator to [[Indenting source code|indent]] the entire inserted text. ==References== *{{help|:nnoremap}} *{{help|p}} *{{help|id==}} *{{help|tag=`%5D|label=`]}} *{{help|tag=%5Dp|label=]p}} ==Related plugins== * [https://github.com/sickill/vim-pasta vim-pasta] allows for pasting with automatic adjusting of indentation to destination context. ==Comments==
Summary:
Please note that all contributions to the Vim Tips Wiki are considered to be released under the CC-BY-SA
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Help
(
view source
)
Template:Navigation
(
view source
)
Template:TipImported
(
view source
)
Follow on IG
TikTok
Join Fan Lab