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
98
votes
8 answers

How can I permanently display the path of the current file in Vim?

I know CTRLg displays the current file you're working on. Is there a way to modify my .vimrc such that the filename/path is always displayed?
zallarak
  • 5,287
  • 7
  • 38
  • 54
97
votes
3 answers

How do you open a file from within Vim?

I know how to open a file in Vim from a terminal (vim fileName). What I can't figure out is how to open a file when I'm already within Vim. I tried :r fileName, but that appears to read (or append) the file into the unsaved buffer I have open. It…
jshock
  • 1,373
  • 2
  • 11
  • 17
97
votes
10 answers

Vim yanking range of lines

I'm a C# developer who has just recently decided to expand my knowledge of the tools available to me. The first tool I've decided to learn is Vi/Vim. Everything has been going well so far, but there are a couple of questions I can't seem to find…
jnadro52
  • 3,454
  • 3
  • 21
  • 17
97
votes
1 answer

How to include forward slash in vi search & replace

I have a file that contains the string usrbin. I want to search for usrbin and replace it with /usr/bin/. I tried :%s/usrbin/usr/bin/g, but it's showing error E488: Trailing characters. How do I include a forward slash in a search and replace?
user1578447
  • 1,071
  • 1
  • 8
  • 4
95
votes
9 answers

Hide all (not)matching lines in Vim

Is it possible to show/hide all matching lines in vi or Vim? Not highlight but just show only those lines. For example I have a text with word the word ERROR. How do I make it show only lines containing ERROR and how to show only lines without…
Alex Bolotov
  • 8,781
  • 9
  • 53
  • 57
92
votes
7 answers

How can I insert text in the middle of the line to multiple lines in Vim?

Say I have ten lines and I want to prepend text to some word that occurs in those lines? It does not have to be at the beginning of the line. From: sdfsd foo sdfsd sfsd foo fsdf sdfsdf foo sdfsdf to: sdfsd bar(foo sdfsd sfsd bar(foo…
Sasha
91
votes
7 answers

Auto-indent spaces with C in vim?

I've been somewhat spoiled using Eclipse and java. I started using vim to do C coding in a linux environment, is there a way to have vim automatically do the proper spacing for blocks? So after typing a { the next line will have 2 spaces indented…
zxcv
  • 7,391
  • 8
  • 34
  • 30
91
votes
5 answers

Vim - yank into search register

Is there any easy/quick way to "yank" into vim's "last search" register ("/)? From the vim documentation, it appears that the answer is no, but that it can be assigned via a "let" command: It is writable with ":let", you can change it to have…
Dan
  • 10,303
  • 5
  • 36
  • 53
88
votes
7 answers

vi to delete from the beginning of the line till the cursor

How can we use vim to delete characters from the beginning of the line till the cursor. Say, we have a string "hello world" while the cursor is on "w". How can we delete from "h" till "w".
blue123
  • 2,937
  • 7
  • 27
  • 29
83
votes
7 answers

Package(Plugin) Management for Vim

Emacs 24 looks like it will have a package manager. What package management options are there for Vim?
ntimes
  • 1,269
  • 2
  • 12
  • 10
81
votes
6 answers

Using normal-mode motions in command-line mode in Vim

Is modal editing possible in command-line mode? Some examples: After writing !ls ~/foo/bar I want to db to delete bar I executed the above command and now I want to change ls to mv and jump back to $
Natan Yellin
  • 6,063
  • 5
  • 38
  • 57
80
votes
11 answers

How to generate a number sequence in file using vi or Vim?

Is there a way to generate a number sequence in vi or Vim? For example, for an arbitrary range of lines i  through j (where i < j) in a file opened in Vim, is there a way to generate a number sequence from number 1 on line i all the way through…
Sangeeth Saravanaraj
  • 16,027
  • 21
  • 69
  • 98
80
votes
6 answers

How to do column editing in vim?

Vim is pretty powerful when editing by line - the search/replace tools are modeled to work linewise. But what if I want to change a particular column across all lines? For example, how can I change the 80th column in my file to a # easily?
Lazer
  • 90,700
  • 113
  • 281
  • 364
78
votes
2 answers

Returning to previous line with Vim

I was wondering if there's a way to return to the previous line you were on in Vim. Say, for example, I'm writing C code and I just wanted to add an #include at the top of the page: I press gg and go to the top, add the #include, and then I want to…
Amit
  • 7,688
  • 17
  • 53
  • 68
75
votes
6 answers

Tabs and spaces in vim

How do I prevent vim from replacing spaces with tabs when autoindent is on? An example: if I have two tabs and 7 spaces in the beginning of the line, and tabstop=3, and I press Enter, the next line has four tabs and 1 space in the beginning, but I…
RQ