0

I have a mapleader macro to sort IP addresses and delete leading 0 in IP addresses

let mapleader='ip'
map <leader>ips :%s/0\+\([0-9a-f]\)/\1/ | %sort n /.*\./ | %sort n /\.\d\+\./ | %sort n /\./ | %sort n u

This works but it always selects the whole text.

I am searching for a solution where I can visually select with V lines and then call a map that only this lines get sorted.

How can I do this?

1
  • 2
    Won't s/0\+\([0-9a-f]\)/\1/ turn 102 into 12?
    – filbranden
    Jul 7 at 14:07
2

A simple version just reuses the range provided in visual mode:

xnoremap <leader>ips :s/0\+\([0-9a-f]\)/\1/ | *sort n /.*\./ | *sort n /\.\d\+\./ | *sort n /\./ | *sort n u
  • x for visual but not select mode
  • noremap for a non-recursive mapping
  • * for the equivalent range as '<,'>, which is a pain to type. It gets filled in automatically when you press : in visual mode though.
2
  • Thanks but it still does not work (It seems I miss something?) I made in .vimrc let mapleader=',' xnoremap \<leader\>ip :s/0\+([0-9a-f])/\1/ \<bar\> *sort n /.*\./ \<bar\> *sort n /\.\d\+\./ \<bar\> *sort n /\./ \<bar\> *sort n u\<CR\> If I select the Lines with V and then press : I got in the status line :'<,'> and I complete it with map leader "," and ip :'<,'>,ip Return At once an error occurs E481: No range allowed What is my error?
    – VIM_User
    Jul 7 at 20:06
  • @VIM_User assuming this was meant to be a comment on this post, I've converted it. If you're the OP, we can merge the accounts… the previous commenter was correct. Type ,ip from visual mode. The mapping does the : for you.
    – D. Ben Knoble
    Jul 8 at 0:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.