Giter VIP home page Giter VIP logo

vim-indent-object's Introduction

vim-indent-object

Intro

Vim text objects provide a convenient way to select and operate on various types of objects. These objects include regions surrounded by various types of brackets and various parts of language (ie sentences, paragraphs, etc).

This plugin defines a new text object, based on indentation levels. This is very useful in languages such as Python, in which the syntax defines scope in terms of indentation. Using the objects defined in this plugin, an entire if structure can be quickly selected, for example.

Install

  • vim-plug
Plug 'michaeljsmith/vim-indent-object'

Usage

This plugin defines two new text objects. These are very similar - they differ only in whether they include the line below the block or not.

Key bindings Description
<count>ai An Indentation level and line above.
<count>ii Inner Indentation level (no line above).
<count>aI An Indentation level and lines above/below.
<count>iI Inner Indentation level (no lines above/below).

Note: the iI mapping is mostly included simply for completeness, it is effectively a synonym for ii.

Just like regular text objects, these mappings can be used either with operators expecting a motion, such as d or c, as well as in visual mode.

In visual mode the mapping can be repeated, which has the effect of iteratively increasing the scope of indentation block selected. Specifying a count can be used to achieve the same effect.

FeedBack

vim-indent-object was written by Michael Smith [email protected]. The project repository is kept at:

http://github.com/michaeljsmith/vim-indent-object

Any feedback or criticism is welcome, and can be mailed to the author at the above email address. Alternatively issues can be raised on the project website.

vim-indent-object's People

Contributors

juanghurtado avatar michaeljsmith avatar pbyrne avatar schlichtanders avatar wsdjeg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vim-indent-object's Issues

Bug: vai is not indenting the last line

Hi, Thanks for this awesome plugin

I am facing one wierd issue, regarding vai and vaI
Below is an excerpt of Vagrant file.

    end
    
    # starting provisionig only after all nodes are up
    if i == node_count
        config.vm.provision "ansible" do |ansible|
            ansible.playbook = "prereq.yaml"
            ansible.limit = "all"
            ansible.verbose = "v"
            ansible.groups = {
                "dp-yarn-master" => ["master"],
                "dp-yarn-slave" => ["node-[1:#{node_count}]"],
                "[dp-yarn:children]" => ["dp-yarn-master", "dp-yarn-slave"]
                }
            }
        end
    end

if I do vai from line number 15, isn't it supposed to select line number 16 also?
But vaI is working as expected.

Preserve cursor position for yank

I'm not sure if this plugin is still maintained.

When typing yai, the cursor moves to the beginning of the selection.

When typing vai, the cursor moves to the end of the selection.

There's some logic for moving when vai is used because you could continue selecting downwards. However, what is the rationale for moving the cursor when yai is typed? One possible justification is that if you're in a method and yank it, moving to the top would let you paste the recently yanked text ie to duplicate the method.

It would be nice to have the choice of moving the cursor or not.

Inner block selection

First of all -- this plugin is great! Thanks!

Now for the issue.
I am not sure but it looks like trailing empty lines should not be selected with vii.

For example if I want to change init method I would press dii and trailing line is not deleted. But if I do the same with viid then it would be deleted.

def __init__(self, parent):
    parent.title('tkBelveder: work edition. Ver {}'.format(cfg.VERSION))

    menu = tk.Menu(parent)
    gamemenu = tk.Menu(menu)
    menu.add_cascade(label="Game", menu=gamemenu)
    gamemenu.add_command(label="Reset", command=self.reset)

    parent['menu'] = menu

def quit(self, event):
    event.widget.quit()

PS
If you think it should be selected then make linewise selection --> after vii if you press k the previous line is not fully selected.

Getting error when using operator with text objects

Nvim: 0.4.3

I am getting the below error when I do something like dai or cai inside any function or class.

Error detected while processing function <SNR>45_HandleTextObjectMapping[1]..<SNR>45_TextObject:
line  180:
E447: Can't find file "class" in path
Error detected while processing function <SNR>45_HandleTextObjectMapping[1]..<SNR>45_TextObject:
line  180:
E447: Can't find file "def" in path

Have ii and ai select incrementally smaller indents

So if I have a text:

a
a
  b
  b
    c
    c
  b
  b

If my cursor is on "c" and I type vai, then it selects the two lines with "c". If I press ai again, then the selection should expand to contain lines with "b" as well. If I press ai again, then lines with "a" should be selected as well. This is how the vim-ruby-object plugin works; the text object successively applies to a large portion of the text. You can use ii to successively select smaller portions.

Feature request: block text object

Thank you very much for this project. It makes working with source code much easier. ๐Ÿ™‚
Often, I find myself positioning the text cursor on the top line of an indent region (the line before the next indent level starts). So, I have to go down one line before I can use ii, ai and so on. So I created a simple patch to add block mappings (ab, ib, aB, iB) which increment the line number by one and then call your mappings. That works quite well, but it is an ugly hack. That is my current patch:

diff --git a/plugin/indent-object.vim b/plugin/indent-object.vim
index bd4075e..d276f81 100644
--- a/plugin/indent-object.vim
+++ b/plugin/indent-object.vim
@@ -36,6 +36,20 @@ onoremap <silent>iI :<C-u>cal <Sid>HandleTextObjectMapping(1, 1, 0, [line("."),
 vnoremap <silent>aI :<C-u>cal <Sid>HandleTextObjectMapping(0, 1, 1, [line("'<"), line("'>"), col("'<"), col("'>")])<CR><Esc>gv
 vnoremap <silent>iI :<C-u>cal <Sid>HandleTextObjectMapping(1, 1, 1, [line("'<"), line("'>"), col("'<"), col("'>")])<CR><Esc>gv
 
+" Simple extra mappings for a block object (the current/selected line is interpreted as top line)
+" Mappings excluding line below.
+onoremap <silent>ab :<C-u>cal <Sid>HandleTextObjectMapping(0, 0, 0, [line(".")+1, line(".")+1, col("."), col(".")])<CR>
+onoremap <silent>ib :<C-u>cal <Sid>HandleTextObjectMapping(1, 0, 0, [line(".")+1, line(".")+1, col("."), col(".")])<CR>
+vnoremap <silent>ab :<C-u>cal <Sid>HandleTextObjectMapping(0, 0, 1, [line("'<")+1, line("'>")+1, col("'<"), col("'>")])<CR><Esc>gv
+vnoremap <silent>ib :<C-u>cal <Sid>HandleTextObjectMapping(1, 0, 1, [line("'<")+1, line("'>")+1, col("'<"), col("'>")])<CR><Esc>gv
+
+" Mappings including line below.
+onoremap <silent>aB :<C-u>cal <Sid>HandleTextObjectMapping(0, 1, 0, [line(".")+1, line(".")+1, col("."), col(".")])<CR>
+onoremap <silent>iB :<C-u>cal <Sid>HandleTextObjectMapping(1, 1, 0, [line(".")+1, line(".")+1, col("."), col(".")])<CR>
+vnoremap <silent>aB :<C-u>cal <Sid>HandleTextObjectMapping(0, 1, 1, [line("'<")+1, line("'>")+1, col("'<"), col("'>")])<CR><Esc>gv
+vnoremap <silent>iB :<C-u>cal <Sid>HandleTextObjectMapping(1, 1, 1, [line("'<")+1, line("'>")+1, col("'<"), col("'>")])<CR><Esc>gv
+
+
 let s:l0 = -1
 let s:l1 = -1
 let s:c0 = -1

What do you think? Is this a feature that would be useful to others? Does a simple way exist to integrate this in your existing code?

sort a indent file

I use indent-object for sort a block of indent files (css, python) with this map

   nmap <Leader>ss vii!sort<cr>

example, in a css file

  width:100px;
  height:100px;

<leader>ss

  height:100px;
  width:100px;

but how I can sort a complete file ? example:

 footer {
        width:100px;
        height:100px;
 }
 body {
        color: #000;
 }

to

 body {
        color: #000;
 }
 footer {
        height:100px;
        width:100px;
 }

doesn't work with repeat '.'

it doesn't work well with repeat, when you move to another indented text, it doesn't re-calculate. is it expected behavior?

make use of il to cover one more useful text blocks

hi Michael:

I've beening using vim-indent-object as an extra text-object to handle my indented text blocks
http://www.vim.org/scripts/script.php?script_id=3037

it's extremely useful in a daily base, because most of my text files are indentation-based.

however, there is one common scenario that is not (and seems should be ) covered by it:

.task 5
do this
and do that

.task 6

this is more urgent

 do this
 and do that

I know this is not a programming scenario , but I remember even in some script there is such a need to action on this kind of blocks.
currently both of the above are not covered under the text object.

and currently il is just linked to ii:

"
iI (I)nner (I)ndentation level (no lines above/below).
Note that the iI mapping is mostly included simply for completeness, it is
effectively a synonym for ii.
"

so I think it's good to make il mapping to the blocks that I mentioned above?

thanks!

regards
ping

selection of the empty line

In the following snippet I am on the second empty line. I do press vii and selection is propagated from the next line till the previous empty one. Could we just have all the stuff selected? I mean if the nextnonblank and prevnonblank lines have the same indent then just use it to select the block?

def __init__(self, parent):
    parent.title('tkBelveder: work edition. Ver {}'.format(cfg.VERSION))

    menu = tk.Menu(parent)
    gamemenu = tk.Menu(menu)
    menu.add_cascade(label="Game", menu=gamemenu)
    gamemenu.add_command(label="Reset", command=self.reset)

    parent['menu'] = menu

    self.canv = tk.Canvas(parent, width=cfg.WIDTH, height=cfg.HEIGHT,
                          background='#f0f0f0')
    self.canv.pack(fill=tk.BOTH, expand=True)

    parent.bind('<Escape>', self.quit)

    self.draw_board()

Is <count> really working?

I'm probably missing something, but v2ii, for example, does not selects 2 indentation levels for me.

Given this example with cursor at cccc:

aaaa
    bbbb
        cc[|]cc

...pressing vii + ii does select both cccc and bbbb lines. However, pressing v2ii selects only cccc line.

Where am I wrong? :)

do not select a line

Place cursor on self.draw_cell(i,j) and press vii -- the line is not selected. If you press dii then current symbol is deleted.

def draw_board(self):
    for i in range(cfg.COL_COUNT):
        for j in range(cfg.ROW_COUNT):
            self.draw_cell(i, j)

way to remap ?

i would prefer to interchange what vaI and vai.
for example in julia which has end statements and the standard acceptable formatting is

function something (...)
   code
   more code
   even more code
end

vai is quicker than vaI
i would like to use the quicker vai to select the end along with the indented block

Don't stop the cursor on the first line of selection when I use `vii`

Great plugin, I love it because it is much more lightweight that textobj-user stuffs.

Some people prefer to use viw, vip, vii before any action (delete / change) then they take some actions when they confirm the selection is what they want.

It is safer to select a text-object before taking any action, especially for big text objects containing many characters. So, I always use vii with this plugin.

But I noticed that after select text with vii the cursor stopped on the first line of the selection not the last line, for example:

          this is line 1
<cursor>  this is line 2
          this is line 3

The cursor is on line 2, when I type vii the three lines are selected, and the cursor should located on the last line, but it rewind to the first line. It is different from other text object selecting behavior.

When I use viw to select a word, the cursor stops on the last character of the selection, while vip will move the cursor to the last line of the selection.

I suppose vim-indent-object's selection behavior should correspond to other existing text-object's. Stopping cursor on the last line or last character of the selection is important because when I am editing I usually continue moving cursor down, if I made a wrong selection and cancel it by ESC, the cursor is usually close to the origin position before selection.

Is it possible to make an update for this ? If so I don't need press an o command after vii.

Adding a FOSS license

Hi, I love and use this plug-in every day. It makes working with code in Vim so much better! My workplace is cautious about allowing code without a LICENSE file and it'd be a big help if this plug-in could use a FOSS license. I'm sure others in the same position and would appreciate it too. Could a FOSS license please be added if this plug-in is meant for public use?

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.