Sorting a Chunk of Lines in Vim
I often find a need to sort lines of text when editing files in Vim. Typically, this is things like lists of gems in a Gemfile
or lists of CSS attributes in a Sass stylesheet.
Here's how I do it:
- Make a mark on the first line moving the cursor there and hitting
mt
(You can use a letter other than "t", but I like to remember this as a shortcut for "mark top") - Move the cursor to the bottom line, and type
:'t,.!sort
.
That's it!
You can use this same mark technique for other operations on groups of lines, too. Here are some examples:
-
d't
to delete all the lines from the current line to thet
mark -
:'t,.s/"/'/g
to replace all the double quotes in a block with single quotes
Written by Tyler Hunt
Related protips
1 Response
That's neat! Thanks.
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Vim
Authors
Related Tags
#vim
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#