Questions tagged [vi]

vi is a family of text mode-oriented text editors. For general usage/setup questions please use https://vi.stackexchange.com/ instead.

vi is a family of text mode-oriented text editors which share certain characteristics, such as methods of invocation from the operating system command interpreter, and characteristic user interface features. The portable subset of the behavior of vi programs, and the editor language supported within these programs, is described by (and thus standardized by) the Single Unix Specification and .

vi family is required for in cases when graphic environment with other editors available is not present, has failed to boot, is slow to access remotely or avoided just for that edit as the editor must be launched under root rights.

Developers and administrators who know vi may choose to use it as the main editor.


Command Reference:

STARTING vi
     vi filename    edit a file named "filename"
     vi newfile     create a new file named "newfile"
ENTERING TEXT
     i            insert text left of cursor
     a            append text right of cursor
MOVING THE CURSOR
     h            left one space
     j            down one line
     k            up one line
     l            right one space
BASIC EDITING
     x         delete character
     nx        delete n characters
     X         delete character before cursor
     dw        delete word
     ndw       delete n words
     dd        delete line
     ndd       delete n lines
     D         delete characters from cursor to end of line
     r         replace character under cursor
     cw        replace a word
     ncw       replace n words
     C         change text from cursor to end of line
     o         insert blank line below cursor
                  (ready for insertion)
     O         insert blank line above cursor
                  (ready for insertion)
     J         join succeeding line to current cursor line
     nJ        join n succeeding lines to current cursor line
     u         undo last change
     U         restore current line
MOVING AROUND IN A FILE
     w            forward word by word
     b            backward word by word
     $            to end of line
     0 (zero)     to beginning of line
     H            to top line of screen
     M            to middle line of screen
     L            to last line of screen
     G            to last line of file
     1G           to first line of file
     <Control>f   scroll forward one screen
     <Control>b   scroll backward one screen
     <Control>d   scroll down one-half screen
     <Control>u   scroll up one-half screen
     n            repeat last search in same direction
     N            repeat last search in opposite direction
CLOSING AND SAVING A FILE
     ZZ            save file and then quit
     :w            save file
     :q!            discard changes and quit file

Related tags:

See also vi.stackexchange, a stackexchange site dedicated to vi/vim.

2089 questions
463
votes
22 answers

Renaming the current file in Vim

How should I rename my current file in Vim? For example: I am editing person.html_erb_spec.rb I would like it renamed to person.haml_spec.rb I would like to continue editing person.haml_spec.rb How would I go about doing this, elegantly?
Sam Saffron
  • 128,308
  • 78
  • 326
  • 506
453
votes
12 answers

Replace tabs with spaces in vim

I would like to convert tab to spaces in gVim. I added the following line to my _vimrc: set tabstop=2 It works to stop at two spaces but it still looks like one tab key is inserted (I tried to use the h key to count spaces afterwards). I'm not sure…
David.Chu.ca
  • 37,408
  • 63
  • 148
  • 190
446
votes
9 answers

How do I close all open tabs at once?

If I have 10 tabs opened, I have to close each one using ":q" separately. How can I close them all at once?
Srikanth
  • 11,780
  • 23
  • 72
  • 92
401
votes
22 answers

Move entire line up and down in Vim

In Notepad++, I can use Ctrl + Shift + Up / Down to move the current line up and down. Is there a similar command to this in Vim? I have looked through endless guides, but have found nothing. If there isn't, how could I bind the action to that key…
user4812
  • 5,942
  • 9
  • 30
  • 35
388
votes
9 answers

vim line numbers - how to have them on by default?

I can :set number from within a file I'm editing but how can I have them always be on by default?
Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
382
votes
13 answers

Traversing text in Insert mode

While in Insert Mode in Vim, is there any way to traverse the text moving some characters forward and backward other than using the arrow keys? If I press h, j, k and l while in Insert mode, the actual characters are printed on screen instead of…
Andreas Grech
  • 105,982
  • 98
  • 297
  • 360
332
votes
13 answers

Delete newline in Vim

Is there a way to delete the newline at the end of a line in Vim, so that the next line is appended to the current line? For example: Evaluator(): _bestPos(){ } I'd like to put this all on one line without copying lines and pasting them into…
derekerdmann
  • 17,696
  • 11
  • 76
  • 110
331
votes
19 answers

Copy and paste content from one file to another file in vi

I am working with two files, and I need to copy a few lines from one file and paste into another file. I know how to copy (yy) and paste (p) in the same file. But that doesn't work for different files. How is this done? Also, is there a way to…
macha
  • 7,337
  • 19
  • 62
  • 84
331
votes
13 answers

Any way to delete in vim without overwriting your last yank?

I love vim, but one common gotcha is: yank a line go to where you would like to paste it delete what's there paste your yank, only to discover that it pastes what you just deleted Obviously the workflow is delete first, yank second. But it would…
tedsuo
  • 3,943
  • 3
  • 17
  • 8
330
votes
10 answers

Why do Vim experts prefer buffers over tabs?

I don't understand buffers. When I open 3 files on the same tab and close my window, I'm generally annoyed to find out next time I open one of those files that there are strange swap files lingering and giving me pesky messages. But time and time…
2c2c
  • 4,694
  • 7
  • 22
  • 33
284
votes
7 answers

In vim, how do I go back to where I was before a search?

Programming in vim I often go search for something, yank it, then go back to where I was, insert it, modify it. The problem is that after I search and find, I need to MANUALLY find my way back to where I was. Is there an automatic way to go back to…
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
266
votes
10 answers

Go to beginning of line without opening new line in VI

For ages now I've used SHIFTO and SHIFT$ to move to the beginning and end of a line in vi. However SHIFTO is more for opening a new line above the cursor. Is there any command which just takes you to the start of a line?
rix
  • 10,104
  • 14
  • 65
  • 92
265
votes
19 answers

/etc/apt/sources.list" E212: Can't open file for writing

I am trying to edit sources.list using vi editor but getting the following error while saving the file: /etc/apt/sources.list" E212: Can't open file for writing
codersofthedark
  • 9,183
  • 8
  • 45
  • 70
252
votes
10 answers

How do you get sudo access for a file inside the vi text editor?

Often while editing config files, I'll open one with vi and then when I go to save it realize that I didn't type sudo vi filename Is there any way to give vi sudo privileges to save the file? I seem to recall seeing something about this while…
Paul Wicks
  • 62,960
  • 55
  • 119
  • 146
222
votes
4 answers

In Vim/Vi, how do you move the cursor to the end of the previous word?

In Vim's normal mode: e goes to the end of the next word w goes to the beginning of the next word b goes to the beginning of the previous word How do you move the cursor to the end of the previous word?
ClosureCowboy
  • 20,825
  • 13
  • 57
  • 71