Move all comments the top of the file in vim

Python comments begin with a #. Modify to suit other languages. Other uses: Instead of m0 use m$ for end of file or d for deleting all comments.

By: jnash
2009-03-27 18:56:36

These Might Interest You

  • Use sed to remove comments from a file. In this example the comments begin with #. The command '/^#/d' remove line starting with #. The command 's/#.*$//' remove comments at end of lines.

    sed -e '/^#/d' -e 's/#.*$//' in
    agambier · 2012-01-25 15:05:57 4
  • Shows a file without comments (at least those starting by #) - removes empty lines - removes lines starting by # or "some spaces/tabs then #'" Useful when you want to quickly see what you have to customize on a freshly installed application without reading the comments that sometimes are a full 1000 lines documentation :) While posting, I saw this But it's dirty and incomplete, to my mind My original goal was to remove lines like "\t*#" but I can't figure out how to do a egrep '\t' on a command-line. Two workarounds if needed: egrep -v 'press control + V then TAB then #' /your/file or egrep -v -f some_file /your/file #where some_file contains what you want to exclude, example a really inserted TAB

    egrep -v "^$|^[[:space:]]*#" /etc/some/file
    michauko · 2009-05-12 07:14:48 4
  • Strips comments from at least bash and php scripts. Normal # and // as well as php block comments removes all of the: empty/blank lines lines beginning with # lines beginning with // lines beginning with /* lines beginning with a space and then * lines beginning with */ It also deletes the lines if there's whitespace before any of the above. Add an alias to use in .bashrc like this: alias stripcomments="sed -e '/^[[:blank:]]*#/d; s/[[:blank:]][[:blank:]]*#.*//' -e '/^$/d' -e '/^\/\/.*/d' -e '/^\/\*/d;/^ \* /d;/^ \*\//d'"

    sed -e '/^[[:blank:]]*#/d; s/[[:blank:]][[:blank:]]*#.*//' -e '/^$/d' -e '/^\/\/.*/d' -e '/^\/\*/d;/^ \* /d;/^ \*\//d' /a/file/with/comments
    unixmonkey6951 · 2009-11-10 17:47:22 2
  • cd into the directory that contains the file. this is just the usual move command but shortcut'd. say you wanted to move a photo img1.png from ~/photos/holidayphotos into the parent directory which is ~/photos command would be: ~/photos/holidayphotos$ mv img1.png .. I use Ubuntu so this'll work in debian but not sure what else.

    mv file_name.extension ..
    takealeft · 2009-03-09 15:35:58 1

What Others Think

Dare I ask for the usefulness of this command.
haivu · 482 weeks and 2 days ago
Most config files that I edit have lots of commented options and I turn some on just by uncommenting them. (Example: ssh_config) I like to see the options I've turned on grouped together. Hence this. I agree the programming language context isn't too well suited though :)
jnash · 482 weeks and 2 days ago
I think in posted form it's not useful for much, but I didn't know how to do this kind of thing with Vim, and now i do. There's times I want to edit a config littered with comments, and I just want to delete them. Until now, I've just dropped out of vim, ran a similar regex with grep -v, and then copied that output over the file. This is indeed much easier, even if it's just a temple and demo rather than a entirely useful command by itself.
clockworkavian · 482 weeks and 2 days ago
*template. I meant template, not temple. Stupid keyboard.
clockworkavian · 482 weeks and 2 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: