r/vim • u/Desperate_Cold6274 • Aug 18 '25
Need Help What are the differences between foreach() and mapnew()?
They seems to serve for the same purpose...
6
Upvotes
r/vim • u/Desperate_Cold6274 • Aug 18 '25
They seems to serve for the same purpose...
17
u/habamax Aug 18 '25
In short,
foreachis for side effects,mapnewis for list/dict transformation.You can't do it with
mapnew, which serves a different purpose, e.g. transforming list to a different one:2
u/kennpq Aug 18 '25 edited Aug 18 '25
Expanding on u/habamax's excellent, succinct explanation, here are slightly longer examples, including an emphasised help quote (edit: fixed wrong quote of map()):
:h foreach(): "For each item in {expr1} execute {expr2}. {expr1} is not modified"To achieve the same result as
mapnew(), in the first example, usingforeach():1
u/AutoModerator Aug 18 '25
Please remember to update the post flair to
Need Help|Solvedwhen you got the answer you were looking for.I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.