Tag Info

Hot answers tagged

59

Vim sometimes has trouble with files that have unusually long lines. It's a text editor, so it's designed for text files, with line lengths that are usually at most a few hundred characters wide. A database file may not contain many newline characters, so it could conceivably be one single 100 Mb long line. Vim will not be happy with that, and although it ...


50

Although you restrict the commandline arguments there is nothing that prevents the user from using vim to open, edit and overwrite any random file once it is running as root. The user can run sudo vim /etc/httpd/conf/httpd.conf and then clear all that text from the edit buffer then for convenience source an existing file (although that is not even ...


47

In my experience Vim chokes not on large files, but on long lines. Use this command to have mysqldump use shorter lines at the expense of a larger file: $ mysqldump --complete-insert -u -p Additionally, you can open Vim and ask it not to parse your .vimrc file or load any plugins with this command: $ vim -u NONE output.sql Loading Vim in this manner ...


44

There're two reasons: Auto insert comment Auto indenting For pasting in vim while auto-indent is enabled, you must change to paste mode by typing: :set paste Then you can change to insert mode and paste your code. After pasting is done, type: :set nopaste to turn off paste mode. Since this is a common and frequent action, vim offers toggling paste ...


26

You can use another character instead of slash / as delimiter to substitution command. Example using #: :%s#/a/b/f/g/d/g#/s/g/w/d/g/r#


25

While the original vi is still available, I do not think it is much used on current linux or BSD distributions;1 apparently it was dusted off in 2000 after having been mothballed a decade before that, and the last release was 2005. There are various implementations of vi around, which is really now a POSIX specification. These include nvi and elvis, but ...


19

zsh like most modern shells have a choice between two different keyboard mappings for command-line editing: a vi one and an emacs one. In some shells (like tcsh or readline-based ones like bash), the emacs one is the default and probably the one you expect. With zsh, you get emacs mode by default unless $EDITOR or $VISUAL contains vi (if you're a ...


17

You can use the :edit command, without specifying a file name, to reload the current file. If you have made modifications to the file, you can use :edit! to force the reload of the current file (you will lose your modifications).


16

Use the \c escape sequence: /foo\c See also: http://stackoverflow.com/questions/2287440/how-to-do-case-insensitive-search-in-vim


15

There are several options to do so: You can use a terminal multiplexer like screen or tmux. In screen, for example, the shortcut Ctrl+a - a, has the same functiononality as Alt+Tab in graphical environments: switch to the last screen. Or you use vim's internal function. Type :!command in vim's command mode. For example: :!ls -l. After the command ...


15

If you are using Debian then vi is opening as vim because of the entry of vi in /etc/alternatives. Let me break this up for you. When you do ls -l /usr/bin/vi: lrwxrwxrwx 1 root root 20 Jul 1 2014 /usr/bin/vi -> /etc/alternatives/vi As you can see the vi binary is a symbolic link to /etc/alternatives/vi. Now if you do ls -l /etc/alternatives/vi: ...


15

Use the vim paste. What you want is to paste what is on the clipboard buffer "+p This selects the + and pastes it in place. If your using Linux * is the x buffer (the last selected text) Then vim knows its a paste What happens otherwise is vim thinks you have typed the keys being pasted (which include the indentation combined with vim doing auto ...


12

From the command line, you could try paste -d '\0' file1 file2 > file3 That does exactly what you want. Visual Block mode in vim is also perfectly suited for this task. Are you sure you did it correctly? You should Go to visual block mode Select text and press y for yanking Go to the other file, on the upper left corner of the to be paste data (last ...


12

Try using less instead of vim if you want to view a large file directly. Vim tries to do a lot of different stuff when it first loads - scanning the file (potentially in multiple passes) to try to determine what syntax to use, and performing syntax highlighting, and searching for modelines at the top and bottom of the file. Then as you edit the file, vim ...


12

Considering the primary two modes, COMMAND and INSERT, demonstrates the purpose of a modal interface. In INSERT mode you can type normally, inserting text into the document. You can bind keys to perform special functions, although these are generally limited in complexity. COMMAND mode is sort of like an unlimited special function. Something similar ...


12

You can use: vi '+normal 15G25|' myfile.xml


11

On terminals that support setting the scrolling region: tailf() ( # args: <file> [<number-of-header-lines>] trap 'tput csr 0 "$((LINES-1))"' INT tput csr "$((1+${2-1}))" "$((LINES-1))" tput clear { head -n"${2-1}" printf "%${COLUMNS}s\n" "" | tr ' ' = tail -n "$((LINES-1-${2-1}))" -f } < "$1" ) (assumes a shell like ...


11

"load VIM without .vimrc and plugins (clean VIM) e.g. for HUGE files gvim -u NONE -U NONE -N largefile.sql


11

In addition to manually refreshing the file with :edit, you can put into your ~/.vimrc :set autoread to make Vim automatically refresh any unchanged files. Also see :checktime.


11

Because the way you define it py is a shell alias, and Vim doesn't know (nor care) about shell aliases. Use an environment variable instead, perhaps like this: $ PY=/opt/python3.4/bin/python3 $ export PY then in Vim: ... exec '!time ' . fnameescape($PY) . ' %' ... Edit: Added fnameescape(). It's needed if $PY contains characters that have a special ...


10

Yes, PIDs can be reused at any time. What you're seeing in that output is that the process that created that .swp file was 1466. It doesn't necessarily mean that process is still around. Remember that the file is static, it doesn't change just because the process which had it open died. So if 1466 is killed, the file still contains the information that said ...


10

Yes there is a way to automate this. And it starts with selecting the right tool, for the job. In this case you should be using e.g sed and not try to bend vi which was designed for interactive use (and not for automation). The replacement syntax for sed is largely the same as the one for vi. sed -i.backup 's/boy/Boy/g' file-name-1 file-name-2 ...


10

In vi or vim you can ignore case by :set ic, and all subsequent searches will consider the setting until you reset it by :set noic. In less there are options -i and -I to ignore case.


9

Syntax highlighting of less, works just fine on most *nix systems. Even on Cygwin you can do it with the minor adjustment of the shell script path and installing with apt-cyg. apt-cyg install source-highlight export LESSOPEN="| /usr/bin/src-hilite-lesspipe.sh %s" export LESS=' -R ' However, using this drastically slows down browsing of large files. I ...


9

When you run sudo vim you start vim as root. That means that it is the viminfo file in /root that is the problem. You should do rm /root/.viminf*. To make sure of this, run sudo vim and execute this command: :!echo $HOME. This will show you that your home directory is /root. I would recommend that you do not run vim as root, but rather use sudoedit. This ...


9

Vim does not just load the file as-is into memory. It converts it into internal structures (lines, words, etc), performs syntax highlighting using an internal script language, and so on; all of which consumes memory (a whole lot more than a byte for a character) and CPU time.


9

On many Linux systems, the default vi really is a version of Vim, typically one compiled with fewer options (no built-in perl and python support, no GUI, etc) than the one you would get if you installed a Vim package. for example the arrow keys print ABCD instead of moving. This is caused by an inconsistency between your terminal emulator and Vim's ...


8

Use vim's filter functionality. Run: :%!cut -b36- to run the contents of your buffer through the cut command, retaining only bytes 36 and onwards. % means to run the entire buffer through and replace its contents with the output, then ! is the filter command, with the rest of the line as the program to run. This doesn't modify the underlying file unless ...


7

You can do this by replacing the spaces in the line with newlines. :%s/\s/\r/g This will replace on all lines(%s), all spaces (\s) with newlines (\r). You can remove the percent sign to limit the replacement to the current line.


7

You can count words and lines inside vi using vi's own counter: Press g and then CTRL-g. Then the bottom line look for example like this: Col 1 of 11; Line 1 of 106; Word 1 of 344; Byte 1 of 2644 Or use vi's method to call shell commands: :w !wc -w This calls the save (:w) command first and then wc -w and shows the output. Example: :w !wc -w 344 ...



Only top voted, non community-wiki answers of a minimum length are eligible