I need to fool vim to think buffer starts at column n

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

I need to fool vim to think buffer starts at column n

Magnus Woldrich
Hi list,

I'd like to stop vim from modifying the first n columns of a buffer, no
matter what I do in it.

Consider the following data, where the number isn't vims internal line
numbers, but actual data:

   50 ./bin/
   51 ./cgi/
   52 ./dev/
   53 ./devlaleh/
   54 ./emu/
   55 ./etc/

Now, I'd want to perform any action in this buffer, for example a simple:

:%s/ /_/

and I need vim to *think* that the buffer starts at column 9.

I know that I can solve this with mappings and functions, but this isn't
an option in this case.

How would I go about solving this please?

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: I need to fool vim to think buffer starts at column n

Gary Johnson-4
On 2018-12-03, Magnus Woldrich wrote:

> Hi list,
>
> I'd like to stop vim from modifying the first n columns of a buffer, no
> matter what I do in it.
>
> Consider the following data, where the number isn't vims internal line
> numbers, but actual data:
>
>   50 ./bin/
>   51 ./cgi/
>   52 ./dev/
>   53 ./devlaleh/
>   54 ./emu/
>   55 ./etc/
>
> Now, I'd want to perform any action in this buffer, for example a simple:
>
> :%s/ /_/
>
> and I need vim to *think* that the buffer starts at column 9.
>
> I know that I can solve this with mappings and functions, but this isn't
> an option in this case.
>
> How would I go about solving this please?

There are a couple of ways to do that.

One would be to delete the first 8 columns of the buffer into
a register, then put them back when you're done editing.  E.g.,
in normal mode,

    gg8|<C-v>0G"ad

where <C-v> is the single character Ctrl-V, do your editing, then

    gg0"aP

Another would be to prefix your patterns with at atom that matches
after a specific column, e.g.,

    :%s/\%>8c /_/

See

    :help /\%>c

Regards,
Gary

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: I need to fool vim to think buffer starts at column n

Tim Chase
In reply to this post by Magnus Woldrich
On 2018-12-03 17:40, Magnus Woldrich wrote:

> I'd like to stop vim from modifying the first n columns of a
> buffer, no matter what I do in it.
>
> Consider the following data, where the number isn't vims internal
> line numbers, but actual data:
>
>    50 ./bin/
>    51 ./cgi/
>    52 ./dev/
>    53 ./devlaleh/
>    54 ./emu/
>    55 ./etc/
>
> Now, I'd want to perform any action in this buffer, for example a
> simple:
>
> :%s/ /_/
>
> and I need vim to *think* that the buffer starts at column 9.

While I'm not sure there's any generic "protect this column", you can
tweak your substitute command to have it ignore the first nine
characters:

  :%s/\%>9c /_/g

which you can read more about at

 :help /\%c

(there are nuances regarding "/\%c" vs. "/\%v" depending on
virtual-column aspects)

Alternatively, if it's harder to do, you can enter visual block mode,
select all of columns 1-9, delete it to a named register, do whatever
modifications you need to, then paste it back in.

-tim





--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: I need to fool vim to think buffer starts at column n

Tim Chase
On 2018-12-03 11:27, Tim Chase wrote:
> On 2018-12-03 17:40, Magnus Woldrich wrote:
> > and I need vim to *think* that the buffer starts at column 9.  
>
>   :%s/\%>9c /_/g

Whoops, off by one.  I misread that as "I want to protect through
column 9".  So use "8" instead.

Amused that as I pushed <send>, Gary's nigh-identical response
appeared. :-)

-tim


--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: I need to fool vim to think buffer starts at column n

Magnus Woldrich
Gary, Tim,

Thank you for your suggestions. The %>8c trick works, and I also found another way:

  :%s;\v\./\zs;;

However:

Since I don't want to type this every time, I made a cnoremap like so:

  cnoremap %s %s/\v\.\/\zs

Which works, but what about all other operations that you can do on a buffer?
cnoremapping 'g' for example isn't great, and I'd like to perform any
arbitary function on the content in the buffer.

The most ideal solution would be to have vim completely ignore the first
columns, no matter what I do in the buffer.

I'm not sure what direction I should go at this point.

Check this short svg animation to see what I'm doing:
http://japh.se/vidir_col.svg

And this is the project itself: https://github.com/trapd00r/vidir

Cheers,
japh

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: I need to fool vim to think buffer starts at column n

Erik Christiansen
In reply to this post by Magnus Woldrich
On 03.12.18 17:40, Magnus Woldrich wrote:
> Hi list,
>
> I'd like to stop vim from modifying the first n columns of a buffer, no
> matter what I do in it.

To comply with the requirement "no matter what I do in it", ISTM to be
necessary to use something like "cut" to strip the first n-1 columns
into another file, then prepend them again after exiting the vim session.
Otherwise there'll always be ways to, even accidentally, circumvent a
soft barrier in vim.

Mind you, that's a problem if you add/delete/reorder lines in the edit
session - unless those actions are carried out by macros which operate
on both files. But then, those macros could also be circumvented. ;-)

It is an evasive little squirrel to catch.

Erik

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: I need to fool vim to think buffer starts at column n

Christian Brabandt
In reply to this post by Magnus Woldrich

On Di, 04 Dez 2018, Magnus Woldrich wrote:

> Gary, Tim,
>
> Thank you for your suggestions. The %>8c trick works, and I also found another way:
>
>  :%s;\v\./\zs;;
>
> However:
>
> Since I don't want to type this every time, I made a cnoremap like so:
>
>  cnoremap %s %s/\v\.\/\zs
>
> Which works, but what about all other operations that you can do on a buffer?
> cnoremapping 'g' for example isn't great, and I'd like to perform any
> arbitary function on the content in the buffer.
>
> The most ideal solution would be to have vim completely ignore the first
> columns, no matter what I do in the buffer.
>
> I'm not sure what direction I should go at this point.
>
> Check this short svg animation to see what I'm doing:
> http://japh.se/vidir_col.svg
>
> And this is the project itself: https://github.com/trapd00r/vidir

Sounded similar although I thought this was originally from Joey Hess.

I suppose, it's not really "ignore the first couple of characters", as
they may matter if you order the lines differently, isn't it? Else your
perl script could simply leave out putting those numbers into the buffer
in the first place and just add them after the file has been written
when you actually need them.

I don't think there are good alternatives. You can conceal the first
couple of characters, but that won't help against users changing those
unexpectedly.

I have written the NrrwRgn plugin, that might be an alternative. It can
open a selected region inside another window and on ':w' will write the
changes back into the original buffer. That won't help against ordering
the lines differently.

Best,
Christian
--
Letzte Worte des Briefbombenlegers:
  "Wie war noch mal die Faxnummer?"

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.