Showing posts with label line. Show all posts
Showing posts with label line. Show all posts

Tuesday, December 1, 2009

Starting on a Specific Line

An anonymous reader writes:

You can open a file on the command line and automatically put the cursor on the last line by typing:

vim + file

If you want vim to start at a specific line you can do the following instead:

vim +LINENUMBER file

Thanks for the tip!

Monday, June 1, 2009

Easier Command Line Editing

Assuming you're using the bash shell, the following can be helpful when composing long command lines.

Start typing on the command line and then type Ctrl-x Ctrl-e, it should drop you into your system's default editor (hopefully Vim) and allow you to edit the command line from there. Once finished, save the command line, and bash will run the command.

Friday, April 10, 2009

Pull Into Ex

If you position your cursor over a given word and type :<ctrl-r><ctrl-w> it will pull the value into your current ex mode command line. This is great for yanking long values into substitutions among other things.

Thanks to Nate for the tip!

Friday, September 5, 2008

Repeat Last Command Line

To repeat the last command-line given in ex mode, you can simply enter a count followed by "@:".

Monday, June 30, 2008

Command-Line Vim

Before anybody says anything, I know this can often be better accomplished using sed, perl, <your favorite tool>. That said, you can use any command in the ex toolbox directly from the command-line without actually opening Vim. Any argument preceeding the filename to be edited that begins with a plus symbol (+) is taken as an ex-command. Alternatively, you can use the -c command line flag to indicate an ex-command is being specified. This has two handy uses. For one, you can pre-process a file with an ex-command before it's loaded into Vim. Secondly, you can use Vim in a script non-interactively. For example:

travis@travis-ubuntu:/home/travis% echo "hello world" > fun.txt
travis@travis-ubuntu:/home/travis% vim "+s/hello/goodbye cruel/" "+wq" fun.txt
travis@travis-ubuntu:/home/travis% cat fun.txt
goodbye cruel world

You could get an identical result with:

vim -c "s/hello/goodbye cruel/" -c "wq" fun.txt

As an aside, this is the 100th post to Daily Vim. I want to thank everyone who's contributed to the blog so far and made this a great learning experience. You guys have made this a really fun project, so keep the comments coming!

Wednesday, June 18, 2008

Quick Redraw

You can use z. in Vim to quickly redraw the current cursor line at the center of the window. zt redraws at the top and zb redraws at the bottom. Thanks to Nate for the tip!

Tuesday, June 17, 2008

Pushd + Popd

Although pushd and popd are intended mainly for programming, they fill a nice niche when navigating directory structures. If you're current working directory is some deeply nested location like /home/travis/src/myprojects/vim-patch/includes and you need to cd into /tmp for a bit but know you'll be coming back to the original directory, you should use pushd rather than cd. pushd takes the current directory and pushes it into a stack. popd pops the top directory off the stack and does an automatic cd into it. For example:

travis@travis-ubuntu:/usr/share/vim/vim71/colors% pushd /tmp
/tmp /usr/share/vim/vim71/colors
travis@travis-ubuntu:/tmp% popd
/usr/share/vim/vim71/colors
travis@travis-ubuntu:/usr/share/vim/vim71/colors%

Thursday, June 12, 2008

Comment Out Line Range

This is one of my favorite ex hacks. It's pretty old school and should work all the way back to the original Vi. This assumes your comment character is # (octothorpe), but would work just as well with // comments or any other line-wise comment character. Go to column zero of the first line you want to start your comment block on. Hit ma to set a mark. Now scroll down to column zero of the last line you want to comment out. Issue the following ex command:

:'a, . s/^/# /

That's it. It runs a substitute on the range from the mark stored in register 'a to the current line. Take note that you can specify ranges for any ex command using the same technique.

Friday, March 21, 2008

Perl Mass Substitution

Here's a nice trick I've used many many times over the years. Say that you have a group of files, and you want to replace some text in every file quickly and easily. Perl makes this really easy, and it's ubiquity pretty much guarantees it's around if you're on a *nix platform. For the sake of example, we'll pretend we have a group of html files, and we want to change every occurrence of index.html to index.php. The syntax is as follows:

[edited - thanks Chris!]

perl -pi.bak -e 's/index\.html/index.php/gi' *.html

The command line options provided break down as follows:

-p assumes a while loop over each line of every file and implicitly prints
-i specifies that you want in-place substitution on the file (no redirection of STDOUT required)
-i also takes an option argument of a backup file extension (.bak in this case)
-e tells perl to run the following code on the command line

From there, you're using standard Perl regular expressions. Don't worry if you're not a regexp guru. In the simplest form, you can simply replace one string with another.

The Perl code breaks down as follows:

s(means substitute)/string you want to match/string to replace/
(g means replace multiple instances per line)
(i means case-insensitive matching... aka ignore uppercase and lowercase differences)

Of course if you know regular expressions, you can do all kinds of fancy stuff.

Here's a nice reference.

Friday, November 16, 2007

Command Line History

Want to see the ex commands you've executed as of late? Easy, just type q: from normal mode to open your command-line history. From the command-line history window, you can yank commands or execute them as needed. To go back to normal mode type :q. You can also enter command-line history mode from ex mode by typing ctrl-f.