Last Updated: December 26, 2018
· jeremymorgan

Vim Command Line Tips


Here are some cool Vim command line tricks:

1. Open at the end of a file

vi + file.txt

The plus sign added before the file name opens up the file with the cursor at the end of the file. This can be handy if you have a huge text file you and you don’t want to scroll down.

2. Open file at specific line

vi +n file.txt

If you add a number (replace n with a number in case you hadn’t guessed) the cursor will go exactly to that line number. Extremely handy for large text files or error messages that specify a line number in a config or source file.

3. Open file at a pattern

vi +/pattern file.txt

This will open the file on the same line as the pattern you specify (first instance of it). This is great if you have a specific phrase to search for.

4. Recover a file after a crash

vi -r file.txt

Use -r to open a file and recover it after a crash. This could save your butt someday.

5. Run a script against a file

vi -s file.txt < script.sh

This trick could be super powerful, but also dangerous. This allows you to run a script against the file while opening, usually a search or something of that sort.

I hope this helps, I’m going to post more stuff as I learn it, there’s a reason they’ve written whole books about Vim, it has a ton of features. If people enjoy these tips, I’ll add some more later.

1 Response
Add your response


regarding tip #5
you can use vim - to read stdout so all script output could be collected
./my_script blah | vim -
or even more advanced
echo "foo\nbar" | vim - +"%s/bar/abc/g | 2"

over 1 year ago ·