Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

How does one re-use a just-executed command in vim?

For example, I open a file with:

vi myTestFile

Then in normal mode, I search for a word using:

/mysearchword\c <Enter>

Now I want to search for another similar but slightly different word. But this requires me to type the whole thing again:

/mysearchword_1\c <Enter>

It would have been much easier if I could just edit the current command that was just executed, and append "_1" to it rather than typing the whole thing again. Is this possible?

share|improve this question

1 Answer 1

up vote 4 down vote accepted

Up arrow works similar as in command line terminal. In your example, just type / and then up arrow. It will place your previous search command in the Vim's command line. It works with : started commands as well. Moreover, you can use up and down arrows to browse in your commands history.

Note, that this feature requires Vim to work nocompatible mode. Make sure, you have line

set nocompatible

at the beginning of you ~/.vimrc file - it must be the first instruction, before all others.

If you don't have ~/.vimrc (it is not created by default), just create one:

$ vim ~/.vimrc

edit as needed and save it.

share|improve this answer
    
up arrow has no effect for me. –  faizal Aug 11 at 17:53
    
@faizal please read carefuly. You must first type / or : (depending on command type you want to repeat) and then up arrow. –  Mikołaj Bartnicki Aug 11 at 17:54
    
It works with me. @faizal are you on vim or vi? Which version? vi -v. –  Alaa Ali Aug 11 at 17:55
    
@MikołajBartnicki yes, that does not work for me. @Alaa Ali i am using the default vi available with Ubuntu 14.04. vi -v shows me VIM version 7.4.52. It also shows "Running in Vi compatible mode". –  faizal Aug 11 at 17:57
1  
Instead, i did sudo apt-get install vim as i started realizing lots of vim features are missing on my system, like the vimtutor and :help. The vi command was probably starting vim.tiny, which comes in Ubuntu by default and has a minimal feature set. Now, after the vim installation, i can use the vim command and it runs automatically without compatibility mode. ~/.vimrc still does not exist, but the problem is resolved. –  faizal Aug 11 at 18:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.