114 votes

refresh changed content of file opened in vi(m)

TL;DR Skip to the Wrap-up heading for the vimrc lines to add to do make your life better. Manually Run :checktime Check if any buffers were changed outside of Vim. This checks and ...
Tom Hale's user avatar
  • 30.5k
111 votes

What is `^M` and how do I get rid of it?

This worked for me :e ++ff=dos The :e ++ff=dos command tells Vim to read the file again, forcing dos file format. Vim will remove CRLF and LF-only line endings, leaving only the text of each line ...
Stryker's user avatar
  • 1,319
106 votes

Is Vim immune to copy-paste attack?

Short answer: In many situations, Vim is vulnerable to this kind of attack (when pasting text in Insert mode). Proof of concept Using the linked article as a starting point, I was able to quickly ...
Anthony Geoghegan's user avatar
104 votes

How to enable and use code folding in Vim?

Fold by default Vim's default folding method is manual meaning that the folds are created manually; otherwise, there is no fold to be closed or opened using za, zo, or zc as you described. But, you ...
cartoonist's user avatar
  • 1,406
80 votes

Change default editor to vim for _ sudo systemctl edit [unit-file] _

First method, you can add this line to ~/.bashrc: export SYSTEMD_EDITOR=vim And then sudo visudo and add this line: Defaults env_keep += "SYSTEMD_EDITOR" Start new bash session to take effect, ...
林果皞's user avatar
  • 5,156
79 votes

Opening a vertical terminal in Vim 8.1

You can use the :vert[ical] command modifier: :vert term :vertical works with any command that splits a window, for example: :vert copen :vert help vert
Daniel Díaz Carrete's user avatar
75 votes

How to send the ESC signal to vim when my esc key doesn't work?

Ctrl-[ sends the same character to the terminal as the physical Esc key. The latter is simply a shortcut for the former, generally.
DopeGhoti's user avatar
  • 76.2k
62 votes

How can I use vim to remove first few characters of the selected lines using commands?

Place cursor on first or last ; Press Ctrl+v to enter Visual Block mode Use arrow keys or j, k to select the ; characters you want to delete (or the other "first few characters") Press x to ...
techraf's user avatar
  • 5,941
58 votes

How can I do a 'change word' in Vim using the current paste buffer?

The Vim way is to learn how to intentionally use the yank, delete and other registers. Once you know these, you will easily find your own key sequences to do this. Register "0 is the yank ...
00prometheus's user avatar
54 votes

Why do Vim colors look different inside and outside of tmux?

I had the similar issue before. Comments in blue in Vim were hard to read. In .tmux.conf I set this: set -g default-terminal "screen-256color" And in .vimrc: set background=dark Now it looks as ...
Vitali Plagov's user avatar
52 votes

How to invoke vim editor and pipe output to bash

vipe is a program for editing pipelines: command1 | vipe | command2 You get an editor with the complete output of command1, and when you exit, the contents are passed on to command2 via the pipe. ...
muru's user avatar
  • 73k
50 votes

How many shells deep I am?

When I read your question, my first thought was $SHLVL.  Then I saw that you wanted to count vim levels in addition to shell levels.  A simple way to do this is to define a shell function: vim() { ( ...
Scott - Слава Україні's user avatar
49 votes

`Delete` key doesn't work on vim insert mode for deleting previously typed content

Just put this to your .vimrc : set backspace=indent,eol,start
Gawain Lau's user avatar
48 votes

Vim: what are all the possible swapfile extensions?

tl;dr swp, swo, ..., swa, svz, svy, ..., sva, ..., saa. Upon reaching the last one it triggers an error. The particular piece of code that you're looking for (and comment) is in memline.c: /* ...
Thomas Dickey's user avatar
45 votes

How can I set "vi" as my default editor in UNIX?

In recent versions of Ubuntu you use the alternatives system to manage the default, editor, e.g.: update-alternatives --set editor /usr/bin/vim.basic To see which editors are available for use: ...
Dobes Vandermeer's user avatar
45 votes

vim cut&paste not working in Stretch / Debian 9

Solution: change mouse=a to mouse=r in your local .vimrc. The problem with setting this in /usr/share/vim/vim80/defaults.vim as the accepted answer says, is that it will be overwritten on every ...
Bernhard Laven's user avatar
44 votes

Use case sensitive search for lower-case string using smartcase

Use \C (uppercase) in your search pattern See :help /ignorecase: "\C" does the opposite: Force matching case for the whole pattern.
Philippos's user avatar
  • 13.5k
44 votes

Extend Default Configuration of vim

You can source the global Vim configuration file into your local ~/.vimrc: unlet! skip_defaults_vim source $VIMRUNTIME/defaults.vim set mouse-=a See :help defaults.vim and :help defaults.vim-...
emyller's user avatar
  • 556
43 votes

How does `:w !sudo tee %` work

The structure :w !cmd means "write the current buffer piped through command". So you can do, for example :w !cat and it will pipe the buffer through cat. Now % is the filename associated with the ...
Stephen Harris's user avatar
42 votes

In vi/vim, how do I append to a file instead of overwriting it?

From :h :w: :w_a :write_a E494 :[range]w[rite][!] [++opt] >> Append the specified lines to the current file. :[range]w[...
muru's user avatar
  • 73k
41 votes

How to get rid of "nano not found" warnings, without installing nano?

I found my own answer and so I'm posting it here, in case it helps someone else. In the root user's home directory, /root, there was a file alled .selected_editor, which still retained this content: ...
Teekin's user avatar
  • 737
37 votes

Copy/paste into SSH'd VIM from local (Windows) clipboard

On my windows laptop, pasting into remote vim, this is the only thing I could get to work: Ctrl+C was fine for copying.. but I needed Shift+Insert to Paste ! (A good reason to get a laptop where ...
SherylHohman's user avatar
37 votes

How many shells deep I am?

You could count as many time you need to go up the process tree until you find a session leader. Like with zsh on Linux: lvl() { local n=0 pid=$$ buf until IFS= read -rd '' buf < /proc/$...
Stéphane Chazelas's user avatar
35 votes

How to make Vim display colors as indicated by color codes?

Now with vim 8 you can use terminal mode :terminal and then in that terminal do cat myfile and go back to normal mode with Ctrl-w N. This will display ANSI color codes correctly. By automating these ...
Johannes Riecken's user avatar
34 votes

disabling mouse support in `vim` in a `gnome-terminal` environment

I've found what cause this bad behavior with many linux flavors : /usr/share/vim/vim80/defaults.vim # may be "vim81" depending on your vim version it's 'sourced' if there's no ~/.vimrc but even if ...
Gilles Quénot's user avatar
34 votes

Why do Vim colors look different inside and outside of tmux?

tmux doesn't support the terminfo capability bce (back color erase), which vim checks for, to decide whether to use its "default color" scheme. That characteristic of tmux has been mentioned a few ...
Thomas Dickey's user avatar
34 votes

How many shells deep I am?

Use echo $SHLVL. Use the KISS principle. Depending on your program's complexity, this may be enough.
user2497's user avatar
  • 747
33 votes

In Vim , how can I copy from several lines? (not a range);

You can append to a register instead of erasing it by using the upper-case letter instead of the lower-case one. For example: :1y a # copy line 1 into register a (erases it beforehand) :3y A ...
lgeorget's user avatar
  • 14k
32 votes

Editing text file with Vim does not update tail -f

If you edit a file with vim, typically it reads the file into memory, then writes a new file. So tail is now operating on an out of date copy of the file (which remains in the file system until tail (...
Bob Eager's user avatar
  • 3,610

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