RSS

Nano global search and replace tabs to spaces or spaces to tabs

May 17

I have been an avid user of nano/pico since about 1999, and yes, many naysayers think it is crap for programming, but it works for me, and I like it.

That being said, one of the major issues I’ve had is that the Xorg select/paste always copies tab characters as the corresponding number of spaces.  So, when I select text in one file, paste into another, I have to replace all the spaces with tabs.


Typically I paste into gedit first, do the replace there, then c/p into the file.  This preserves the tabs.

(If you’re still wondering why I use nano, I just like having my editor accessible as long as I have server access.  I never got used to vi, and nano is more than effective for me.)

I have always thought nano should be able to search and replace tabs and spaces, but I could never get it to work.  Even without the gedit technique, I would typically just replace all double spaces to nothing, then manually insert the tabs.  My workarounds, again, are generally sufficient.  I have not NEEDED search and replace of tabs within nano, but today I decided I wanted to really find out if I could.

And it required some digging …

But, I finally found it: verbatim input!

Nano has a feature to disable character interpretation, and for one character, accept input literally.

To turn it on (again it’s for just the first character typed), hit alt-SHIFT-V (alt-V without shift may trigger x-windows menus), then just hit the tab key (it may or may not show a note that you’re in verbatim input mode).

You only need to do this in the search / replace prompts.  Obviously, you can type tabs directly into the file.

So an example — let’s say you want to convert any instance of 8 spaces to a tab character.

Here is the command stack:

control-W (search)
control-R (replace)
hit space 8 times, then hit enter
alt-shift-V (verbatim input)
hit the tab key, hit enter

Proceed as normal.

Hopefully this post is easier to find than the seemingly impossible digging I just undertook …

 

Posted by on May 17, 2011 in Computers, Programming

12 Comments

Tags: , , ,

12 responses to “Nano global search and replace tabs to spaces or spaces to tabs

  1. Christopher Boyer

    May 17, 2011 at 2:03 pm

    VI FTW!!!

     
    • Jeremy

      May 17, 2011 at 2:30 pm

      😛
      FWIW, since I use nano to edit the files of this site, your comment appears courtesy of nano. 😀

       
  2. Jay

    May 28, 2011 at 12:16 pm

    Ah, thanks, man!! I’ve needed this for a long time, myself. Perfect!

     
  3. Jesus

    August 9, 2012 at 2:52 pm

    If this works, I love you

     
  4. Roman

    September 7, 2012 at 8:46 am

    Thanks!

     
  5. Andras

    October 2, 2012 at 12:28 am

    In my xterm, I had to use Esc instead of Alt, so esc-shift-v, but it wordked! I luv you. 😉

     
  6. Zurd

    October 24, 2012 at 4:37 am

    alt-shift-v and nano ftw! Thanx!

     
  7. Michael Nordmeyer

    December 27, 2012 at 9:14 pm

    On OS X I had to use shift-esc-v in Terminal via SSH.

    Thanks a lot for making nano so much better.

     
    • Enrico Moiso

      December 13, 2013 at 2:57 pm

      Thanks a lot to both of you

       
  8. Frimkron

    September 16, 2013 at 1:45 pm

    Really handy to know! Thank you 😀

     
  9. Sven

    May 21, 2014 at 10:50 am

    Exactly what i was searching for. Thanks

     
  10. Florian

    December 6, 2016 at 1:11 pm

    I use nano with the very same rationale, and this hint just made it a snap to convert from TSV to CSV in nano. Thanks a ton!

     

Leave a Reply

Your email address will not be published. Required fields are marked *