Tag Info

Hot answers tagged

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 ...


48

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 ...


46

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).


29

You can do that via scp like this: vim scp://user@myserver[:port]//path/to/file.txt Notice the two slashes // between server and path, which is needed to correctly resolve the absolute path. [:port]is optional. This is handled by vim's netrw.vim standard plugin. Several other protocols are supported.


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 ...


22

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.


22

You could do this by mounting the remote folder as a file-system using sshfs. To do this, first some pre-requisites: sudo apt-get install sshfs #for Debian based OS, use yum or zypper depending on your OS sudo adduser <username> fuse Now, do the mounting process: mkdir ~/remoteserv sshfs -o idmap=user ...


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 ...


18

It's possible to do this without a plugin using the w command, so the buffer contents can be used in a shell command: :w !diff -au "%" - > changes.patch (% is substituted with the path of the file being edited, - reads the buffer from stdin)


17

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 you're using Linux, * is the X/middle-click buffer (the last selected text). Then vim knows it's a paste. Otherwise vim thinks you have typed the keys being pasted and does its own auto-indentation (on top of your copied ...


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: ...


14

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 ...


12

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


11

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 ...


11

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.


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 ...


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 ...


7

You're looking for :set splitright You can also influence this for individual commands, e.g. :rightbelow vsplit


7

All listed keys by you are used. You can inspect a key in vim with :help: :help <key> for example: :help v or checkout this vim cheat sheet.


7

Vim scripts (gvim,vim) can be elegant and are very easy to adapt vi -s edit.vim test.txt where edit.vim contains (the :wq is optional) :%s/boy/Boy/g :%s/girl/Girl/g :g/^$/d :wq where test.txt contains boys & girls boys & girls boy & girl boys & girls here's a generic vim script to clean up a "mucky" text file :" clean.vim :" ...


6

With a large enough value of 'undolevel', Vim should be able to undo the whole day's changes. If you quit Vim in between, you also need to enable persistent undo by setting the 'undofile' option. Vim captures not just a sequential list of commands for undo, but actually a tree of all changes. It also has several commands around undo (cp. :help ...


6

The indexed color palette has the actual rendering open to interpretation - on actual hardware, there were different standards (especially brown vs. dark yellow, brown is more useful and nicer to look at). Just check out this: https://en.wikipedia.org/wiki/Color_Graphics_Adapter On terminal emulators, it depends on the configuration. Most emulators have a ...


5

According to James Hodgkinson's blog, the following command works for me. Note it will refresh the vim screen. :!reset


5

With zsh: vim ./**/*(.) Other shells: find . \( -name '.?*' -prune \) -o -type f -exec vim {} + To open only the (non-hidden) regular files (not directories, symlinks, pipes, devices, doors, sockets...). vim ./**/*(D-.) Other shells, GNU find: find . -xtype f -exec vim {} + To also open hidden files (and traversing hidden directories) and ...


5

One way to solve this problem is to add the character directly to vim as a defined punctuation mark. The way you do this is modify the vim source file, mbyte.c, and then recompile vim. This file is located in the main /src trunk (see https://code.google.com/p/vim/source/browse/src/mbyte.c). The function you want to modify starts like this: /* * Get class of ...


5

You can not do this with vim directly, vim will always resolves the links to find the name of the actual file. From :h E773: For symbolic links Vim resolves the links to find the name of the actual file. The swap file name is based on that name. Thus it doesn't matter by what name you edit the file, the swap file name will normally be the same. You ...


5

Have you tried to use aliases? example Create an alias for the new command: alias ntpversion='ntpq -c "rv 0 version"' Run the command: ntpversion Running ntpversion, after it is set as an alias, will provide the output for your one-liner. http://www.linuxhowtos.org/Tips%20and%20Tricks/command_aliases.htm



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