Code Monkey home page Code Monkey logo

vim-pasta's Introduction

vim-pasta

Pasting in Vim with indentation adjusted to destination context.

About

This plugin remaps p and P (put command) in normal and visual mode to do context aware pasting. What it means is that indentation of pasted text is adjusted properly to match indentation of surrounding code.

Basically it opens new, properly indented line (with o or O) in the place you're pasting to then it pastes the text with ]p. The result is nicely indented code with relative indentation between pasted lines preserved.

Why is it better than ]p alone?

]p (and ]P) adjusts indentation to the indentation of current line. Consider following code:

1  if jola
2    misio
3  end

If you paste with ]p when cursor is in line 1 (if jola) you get it pasted wrong:

1  if jola
2  <first pasted text>
3    misio
4  end

Now, if you paste with ]P when cursor is in line 4 (end) you also get it pasted wrong:

1  if jola
2  <first pasted text>
3    misio
4  <second pasted text>
5  end

vim-pasta takes care of it.

Why is it better than nnoremap p p`[v`]= ?

You can achieve "near-pasta experience" with following in you .vimrc:

nnoremap <leader>p p`[v`]=

It pastes, visually selects pasted text and then re-indents it. In most cases it works quite well. However when you're pasting hand indented code like this:

obj = {
       a: 1,
       b: 2,
     foo: 3,
  barbaz: 4
}

it re-indents it to be like this:

obj = {
  a: 1,
  b: 2,
  foo: 3,
  barbaz: 4
}

I hate when it happens. vim-pasta takes care of it.

Additionally vim-pasta detects type of visual selection that was used for yanking and does its indenting magic only for linewise selections (VISUAL LINE), contrary to above mapping.

Installation

  • With pathogen.vim:

      cd ~/.vim/bundle
      git clone git://github.com/sickill/vim-pasta.git
    
  • With Vundle:

      " .vimrc
      Bundle 'sickill/vim-pasta'
    

Usage

Just paste as usual with p and P. Enjoy!

Configuration

By default pasta is disabled for python, coffeescript, markdown, yaml and slim because for these types desired indentation for new lines can't be easily computed from existing code/text.

To change the list of filetypes for which pasta is enabled you can either use black-listing or white-listing.

To black-list some filetypes put following in your .vimrc:

let g:pasta_disabled_filetypes = ['python', 'coffee', 'yaml']

To white-list some filetypes put following in your .vimrc:

let g:pasta_enabled_filetypes = ['ruby', 'javascript', 'css', 'sh']

Note: if white list is defined no black list checking is performed.

If you don't want pasta to override default p and P mappings you can change it like this:

let g:pasta_paste_before_mapping = ',P'
let g:pasta_paste_after_mapping = ',p'

Author

Marcin Kulik (@sickill)

vim-pasta's People

Contributors

ku1ik avatar deris avatar davidsierradz avatar suy avatar viniciusgerevini avatar

Stargazers

isana avatar Begin avatar tamago324 avatar Yuta Katayama avatar Tsuyoshi CHO avatar  avatar Jonathan Barratt avatar hokorobi avatar Manpreet Singh avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.