diff --git a/CHANGES.md b/CHANGES.md
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -9,6 +9,8 @@
 Internal/api/developer-ish changes in the hledger-lib (and hledger) packages.
 For user-visible changes, see the hledger package changelog.
 
+# 1.29.2 2023-04-07
+
 # 1.29.1 2023-03-16
 
 - Hledger.Utils.String:
diff --git a/Hledger/Query.hs b/Hledger/Query.hs
--- a/Hledger/Query.hs
+++ b/Hledger/Query.hs
@@ -752,7 +752,16 @@
 matchesPostingExtra atype (Not q )  p = not $ matchesPostingExtra atype q p
 matchesPostingExtra atype (Or  qs)  p = any (\q -> matchesPostingExtra atype q p) qs
 matchesPostingExtra atype (And qs)  p = all (\q -> matchesPostingExtra atype q p) qs
-matchesPostingExtra atype (Type ts) p = maybe False (\t -> any (t `isAccountSubtypeOf`) ts) . atype $ paccount p
+matchesPostingExtra atype (Type ts) p =
+  -- does posting's account's type, if we can detect it, match any of the given types ?
+  (maybe False (\t -> any (t `isAccountSubtypeOf`) ts) . atype $ paccount p)
+  -- or, try the same test with the original (pre-aliasing/pivoting) posting's account
+  || (fromMaybe False $ do
+      porig <- poriginal p
+      let a = paccount porig
+      t <- atype a
+      Just $ any (t `isAccountSubtypeOf`) ts
+  )
 matchesPostingExtra _ q p             = matchesPosting q p
 
 -- | Does the match expression match this transaction ?
diff --git a/Hledger/Utils/IO.hs b/Hledger/Utils/IO.hs
--- a/Hledger/Utils/IO.hs
+++ b/Hledger/Utils/IO.hs
@@ -21,6 +21,7 @@
 
   -- * Viewing with pager
   pager,
+  setupPager,
 
   -- * Command line arguments
   progArgs,
@@ -78,7 +79,7 @@
 import           System.Console.ANSI
   (Color,ColorIntensity,ConsoleLayer(..), SGR(..), hSupportsANSIColor, setSGRCode, getLayerColor)
 import           System.Directory (getHomeDirectory)
-import           System.Environment (getArgs, lookupEnv)
+import           System.Environment (getArgs, lookupEnv, setEnv)
 import           System.FilePath (isRelative, (</>))
 import           System.IO
   (Handle, IOMode (..), hGetEncoding, hSetEncoding, hSetNewlineMode,
@@ -128,15 +129,37 @@
 -- "Avoid using pshow, pprint, dbg* in the code below to prevent infinite loops." (?)
 
 -- | Display the given text on the terminal, using the user's $PAGER if the text is taller 
--- than the current terminal and stdout is interactive and TERM is not "dumb".
+-- than the current terminal and stdout is interactive and TERM is not "dumb"
+-- (except on Windows, where a pager will not be used).
+-- If the text contains ANSI codes, because hledger thinks the current terminal
+-- supports those, the pager should be configured to display those, otherwise
+-- users will see junk on screen (#2015).
+-- We call "setLessR" at hledger startup to make that less likely.
 pager :: String -> IO ()
 #ifdef mingw32_HOST_OS
 pager = putStrLn
 #else
-pager s = do
+printOrPage' s = do  -- an extra check for Emacs users:
   dumbterm <- (== Just "dumb") <$> lookupEnv "TERM"
-  (if dumbterm then putStrLn else printOrPage . T.pack) s
+  if dumbterm then putStrLn s else printOrPage (T.pack s)
+pager = printOrPage'
 #endif
+
+-- | Make sure our $LESS and $MORE environment variables contain R,
+-- to help ensure the common pager `less` will show our ANSI output properly.
+-- less uses $LESS by default, and $MORE when it is invoked as `more`.
+-- What the original `more` program does, I'm not sure.
+-- If $PAGER is configured to something else, this probably will have no effect.
+setupPager :: IO ()
+setupPager = do
+  let
+    addR var = do
+      mv <- lookupEnv var
+      setEnv var $ case mv of
+        Nothing -> "R"
+        Just v  -> ('R':v)
+  addR "LESS"
+  addR "MORE"
 
 -- Command line arguments
 
diff --git a/hledger-lib.cabal b/hledger-lib.cabal
--- a/hledger-lib.cabal
+++ b/hledger-lib.cabal
@@ -5,7 +5,7 @@
 -- see: https://github.com/sol/hpack
 
 name:           hledger-lib
-version:        1.29.1
+version:        1.29.2
 synopsis:       A reusable library providing the core functionality of hledger
 description:    A reusable library containing hledger's core functionality.
                 This is used by most hledger* packages so that they support the same
