0

I would like to sort some lines, ignoring a leading part of them.

Here's an example from my .spacemacs file:

'(
     better-defaults
     elixir
     emacs-lisp
     erlang
     evil-commentary
     finance
     git
     haskell
     html
     ivy
     javascript
     lua
     markdown
     my-eshell
     my-org
     nginx
     nixos
     (org :variables org-projectile-file "TODO.org")
     python
     (ruby :variables ruby-test-runner 'rspec)
     shell-scripts
     spell-checking
     (syntax-checking :variables syntax-checking-enable-tooltips nil)
     vinegar
     yaml
)

I would like to sort these, but I need to ignore the leading ( for that.

Similarly, here's an excerpt from my configuration.nix:

packages = with pkgs; [
      ## Basics
      atool
      bashInteractive
      entr
      ethtool
      fasd
      fd
      file
      fzf
      git
      git-crypt
      gitAndTools.hub
      google-cloud-sdk
      gparted
      hdparm
      htop
      i7z
      inotify-tools
      iperf
      lm_sensors
      neovim
      p7zip
      parted
      pwgen
      ripgrep
      sshfs
      # tarsnap
      tmux
      tree
      unzip
      wget
      xclip
      zip
];

Here, I would like to sort the lines, ignoring the leading # part.

How would I do this in Emacs?

  • Define "ignore". Do you want to sort only the lines between those delimiters? Do you want to sort both delimiter lines before or after the others? – Drew Sep 1 '18 at 22:41
3

You can try M-x sort-regexp-fields ^.*$ \_<.+?\_>

  • ^.*$ is the record regexp, it splits the region into lines
  • \_<.+?\_> is the key regexp, it matches the first symbol in the line
1

If you mean that you want to sort only the lines between the delimiters then just narrow the buffer to include only those lines, then sort them.

  1. To narrow the buffer to the lines you want to sort: Select them, then C-x n n.

  2. To sort the lines then showing (in the narrowed buffer): Use M-x sort-lines or another sort command. See the Emacs manual, node Sorting.

  3. To show all of the lines again (remove the buffer restriction): Use C-x n w.

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.