119 votes
Accepted

vi - How go to line N?

To make vi start at a particular line in a file, add +line_num to the command you use to start vi. Replace line_num with the line number, for example: vi +14 file.py You can also use the ex command ...
Antoine Subit's user avatar
109 votes
Accepted

Use of touch and vi?

touching the file first confirms that you actually have the ability to create the file, rather than wasting time in an editor only to find out that the filesystem is read-only or some other problem.
DopeGhoti's user avatar
  • 76.2k
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

Does vi silently add a newline (LF) at the end of file?

POSIX requires this behavior, so it's not in any way unusual. From the POSIX vi manual: INPUT FILES See the INPUT FILES section of the ex command for a description of the input files supported by the ...
Barefoot IO's user avatar
  • 1,946
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
42 votes
Accepted

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
33 votes
Accepted

Does vi silently add a newline (LF) at the end of file?

This is the expected vi behavior. Your file has an incomplete last line so strictly speaking (i.e. according to the POSIX standard), it is not a text file but a binary file. vi which is a text file ...
jlliagre's user avatar
  • 61.2k
29 votes

How to put current line at top/center/bottom of screen in vim?

Output of the :help scroll-cursor @mtk mentions. Note that there is a difference between zz and z.. Scrolling relative to cursor (scroll-cursor) The following commands ...
Drew Noakes's user avatar
24 votes

Use of touch and vi?

Apart from the given answers, one advantage of touch is that any other user/terminal editing the same file while you touched it , will receive a warning when they try to save any changes. WARNING: ...
amisax's user avatar
  • 3,025
22 votes
Accepted

Stop vim from messing up my indentation on comments

I'm never satisfied with the "set all these things just in case" answers. I insist on knowing which of those things were set wrong and how they got set. Here is a command that will show you the values ...
Bruno Bronosky's user avatar
20 votes

How to set and determine the command-line editing mode of Bash?

To set: set -o vi Or: set -o emacs (setting one unsets the other. You can do set -o vi +o vi to unset both) To check: if [[ -o emacs ]]; then echo emacs mode elif [[ -o vi ]]; then echo vi ...
Stéphane Chazelas's user avatar
20 votes
Accepted

Disabling vim visual mode in /etc/vim/vimrc does not work

Debian's /etc/vim/vimrc contains this comment: " Vim will load $VIMRUNTIME/defaults.vim if the user does not have a vimrc. " This happens after /etc/vim/vimrc(.local) are loaded, so it will override "...
muru's user avatar
  • 73k
19 votes

Use of touch and vi?

Apart from the accepted answer: It is worth noting that touch is used to update file timestamps. If you use touch on a file that exists, it will then update the files timestamp to the current date ...
Peschke's user avatar
  • 4,148
19 votes
Accepted

Find any line in VI that has something other than ATCG

First of all, you definitely do not want to open the file in an editor (it's much too large to edit that way). Instead, if you just want to identify whether the file contains anything other than A, T,...
Kusalananda's user avatar
  • 334k
18 votes

Vim - how to increase each number in visual block?

You do not need to leave visual mode to increase numbers, just use g 5 g Ctrl-a 5 ......... 5 times g ......... globally Ctrl-a .... increase numbers Actually I have learned this trick in a vimgolf ...
SergioAraujo's user avatar
17 votes
Accepted

In bash vi mode, map jk to exit insert mode

TL;DR Bash has a similar functionality to zsh's bindkey through bind, but it does not have several vi modes like zsh. After set -o vi you can do: bind '"jk":vi-movement-mode' which is the ...
grochmal's user avatar
  • 8,657
17 votes

How to comment a line based on the word it starts with?

In vi: :%s/^HOST/#&/ or :g/^HOST/s//#&/ The % in the first command means "in the whole buffer", and is a short way of saying 1,$, i.e. from the first line to the last. & in the ...
Kusalananda's user avatar
  • 334k
17 votes
Accepted

Opening most recently modified file in vim

vi -- "$(ls -t | head -n 1)" (that assumes file names don't contain newline characters). Or if using zsh: vi ./*(om[1]) Or: vi ./*(.om[1]) To only consider regular files. vi ./*(.Dom[1]) To ...
Stéphane Chazelas's user avatar
15 votes
Accepted

Using the caret symbol (^) in substitutions in the vi editor

In the vi editor, as well as in both ex and ed (as found on BSD systems), the ^ addresses the previous line. This means that the command ^d would delete the previous line, ^m. would swap this line ...
Kusalananda's user avatar
  • 334k
13 votes

Jump to specific character in a line in VI

if you want to go to the 12th character on a line: 12| For the 6th character in the line: 6|
Wayne Walker's user avatar
  • 1,043
13 votes
Accepted

Configure shortcut on Vim to save file

Vim comes with a file providing some mappings intended to match Windows files. This is usually in $VIMRUNTIME/mswin.vim (run :echo $VIMRUNTIME to see what it is for you). You can just do: :source $...
muru's user avatar
  • 73k
13 votes

How can I use vi to edit prompt line of a utility?

First you have to make sure to use vi as shell command line editor: set -o vi Now you can type/copy your command to the command line. To leave insert mode and enter normal mode, use Esc or Shift+Tab....
pfnuesel's user avatar
  • 5,847
13 votes
Accepted

Tmux command mode - Use vi shortcuts

You just need to run the following tmux command: set -g status-keys vi Then after entering the command line (prefix-key + :) you can, for example, press Esc and move left and right with the H and L ...
B Layer's user avatar
  • 5,171
12 votes

What is "<Esc>Kv" in bash

You don't need Shift + k. Using Esc + v will work since you are allowing shell command line editing using the built-in vi editor using set -o vi (same can be acheieved with Ctrl + x + e). This is ...
Vombat's user avatar
  • 12.9k
12 votes
Accepted

Why does ci" (change inside quotes) not work in bash with vi mode enabled?

readline's vi-mode is a subset of vi (essentially features that affect a single line, with some allowances for usability). The ci command is not part of vi; it is a vim feature. Further reading: A ...
Thomas Dickey's user avatar
12 votes

Move in crontab -e editor

You can pick any editor you like by setting the $EDITOR variable before calling crontab -e e.g. $ EDITOR=emacs crontab -e will run emacs as your editor. If you have a favourite editor then you can ...
Stephen Harris's user avatar
12 votes

Vi editor: What's the fastest way to delete multiple rows in a file?

:2,$v/0$/d deletes the lines that don't end in 0 starting from the second one.
Stéphane Chazelas's user avatar
11 votes

Searching for a specific string at beginnning and end of the line in Vi

To search for foo at the beginning or the end of a line: /^foo\|foo$ Edit: To avoid retyping the foo, you could also use a backreference (suggested by @StéphaneChazelas): /\v(foo)&(^\1|\1$) ...
dr_'s user avatar
  • 29.7k

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