Vim and nano shortcuts cheat sheet
Vim: modes and opening files
-
vim example.confOpen the file for editing in Vim. When the file does not exist, Vim creates it only on the first save. The editor opens in Normal mode, where keys run commands instead of typing text.
-
vim +25 example.confOpen the file with the cursor on line 25. This form is useful when an error message points to a line number in a configuration file.
-
vim +/Listen example.confOpen the file with the cursor on the first occurrence of the word Listen.
-
vim -R example.confOpen the file as read-only, to prevent changes by mistake. A regular save with :w is blocked with an error message, and the :w! command overrides the block when you have write permission. The view command works the same way.
-
EscReturn to Normal mode from any other mode. When you are not sure which mode the editor is in, press Esc before you type a command.
-
iSwitch to Insert mode, where you can type text, and the text goes in before the cursor. The bottom line shows -- INSERT -- as long as the mode is active.
-
:Open the command line at the bottom of the screen, from Normal mode. On this line you type commands such as :w or :q, the Enter key runs the command, and the Esc key cancels it.
-
:help :wqOpen the built-in Vim help on the topic you specify. You can replace :wq with any command or setting. The :q command closes the help window.
-
vimtutorStart a built-in interactive lesson that teaches the basic Vim commands on a copy of a practice file, so there is no risk of harming a real file. On some distributions, the command is available only after you install the full Vim package.
Vim: saving and quitting
-
:wSave the file and stay in the editor.
-
:wqSave the file and quit Vim. The command writes the file even when it has not changed.
-
:xQuit Vim and save the file only when it has changed. Unlike :wq, a file that has not changed is not written again, and its modification time is not updated.
-
ZZA Normal mode shortcut that works like :x: it saves only when there are changes, and then quits. Type a capital Z twice, without a colon.
-
:qQuit Vim when there are no unsaved changes. When there are unsaved changes, Vim blocks the exit and shows the E37 error message.
-
:q!Quit Vim without saving, discarding all the changes made since the last save. This is the quickest way out when you make a change by mistake.
-
ZQA Normal mode shortcut that works like :q!: it quits without saving.
-
:w example.conf.newSave a copy of the content under another file name, and keep editing the original file. This is useful for saving a test version without touching the live file.
-
:saveas example.conf.newSave the content under a new file name and continue editing the new file. The original file stays as it was last saved.
-
:10,20w part.confSave only lines 10 to 20 to a new file. This is useful for extracting a section from a large file.
-
:e!Discard all unsaved changes and reload the file from disk, without leaving the editor.
-
:waSave all the open files that have changed.
-
:qa!Quit Vim and close all the files, windows, and tabs without saving. The :wqa command saves all of them and quits.
Vim: moving around a file
-
h j k lMove the cursor one character left, one line down, one line up, and one character right, respectively. The arrow keys also work, but these keys let you work without moving your hand away from the main keys.
-
wMove the cursor to the start of the next word. A capital W jumps to the next word by spaces only, and treats a path such as /etc/ssh/sshd_config as one word.
-
bMove the cursor to the start of the previous word. A capital B works by spaces only.
-
eMove the cursor to the end of the current word, or to the end of the next word when the cursor is already at the end of a word.
-
0Move the cursor to the start of the line, including any spaces at the beginning.
-
^Move the cursor to the first character in the line that is not a space. This is useful in indented lines in configuration files.
-
$Move the cursor to the end of the line.
-
ggMove the cursor to the first line of the file.
-
GMove the cursor to the last line of the file. A number before G, for example 25G, moves to line 25.
-
:25Move the cursor to line 25 through the command line. The :$ command moves to the last line.
-
Ctrl+f / Ctrl+bScroll one full screen forward and back, respectively.
-
Ctrl+d / Ctrl+uScroll half a screen down and up, respectively, so part of the previous text stays on the screen and you keep your place more easily.
-
H M LMove the cursor to the top, middle, and bottom line of the visible screen, respectively.
-
zzScroll the view so the current line appears in the middle of the screen, while the cursor stays on the same line.
-
%Jump to the bracket that matches the one under the cursor, of type (), [], or {}. This is useful for finding a missing bracket in a configuration file or in code.
-
{ }Move the cursor to the previous or next empty line, to jump between paragraphs and between blocks in the file.
-
fxMove the cursor to the next occurrence of the character x in the current line. You can replace x with any character. The ; key repeats the jump, and the , key repeats it in the opposite direction.
-
Ctrl+o / Ctrl+iIn Normal mode, go back to the previous cursor position and forward again, based on the jump history. This is useful for returning to your starting point after a search or a jump to another line.
-
g;Move the cursor to the place of the last change in the file. Pressing it again moves to earlier changes.
-
maSet a mark named a at the current position. You can use any letter. The keys 'a (an apostrophe followed by the letter) return to the line of the mark, and the keys `a (a backtick followed by the letter) return to the exact position.
Vim: inserting and editing
-
aSwitch to Insert mode, with the text going in after the cursor.
-
ISwitch to Insert mode at the start of the line, before the first character that is not a space.
-
ASwitch to Insert mode at the end of the line. This is useful for adding a value at the end of a setting line.
-
oOpen a new line below the current line and switch to it in Insert mode.
-
OOpen a new line above the current line and switch to it in Insert mode.
-
xDelete the character under the cursor. A number before the command, for example 5x, deletes five characters.
-
rReplace the character under the cursor with the next character you type, and stay in Normal mode. For example, rY replaces the character with Y.
-
RSwitch to Replace mode, where every character you type replaces the character under the cursor instead of being added before it. The Esc key ends the mode.
-
cwDelete from the cursor to the end of the word and switch to Insert mode to type the new text.
-
ciwDelete the whole word under the cursor, from any position inside it, and switch to Insert mode. The ci" form deletes the content between the quotation marks and is useful for replacing a value in a configuration file.
-
ccDelete the content of the whole line and switch to Insert mode, keeping the indentation when automatic indentation is on.
-
CDelete from the cursor to the end of the line and switch to Insert mode.
-
JJoin the next line to the current line, with a space between them.
-
~Switch the character under the cursor from lowercase to uppercase or the other way around, and move to the next character.
-
>> / <<Shift the current line one indentation level right or left, based on the shiftwidth setting. A number before the command, for example 5>>, shifts five lines.
-
.Repeat the last edit at the current cursor position. Combined with n, which moves to the next search result, it lets you make the same change in many places quickly.
-
Ctrl+a / Ctrl+xIncrease or decrease by one the number under the cursor, or the first number after it in the line. A number before the command, for example 10 followed by Ctrl+a, adds ten.
-
Ctrl+nIn Insert mode, complete the word you are typing based on words that already appear in the file and in the open files. Pressing it again cycles through the suggestions, and Ctrl+p moves in the opposite direction.
-
uUndo the last change. Each additional press undoes an earlier change.
-
Ctrl+rRedo a change that you undid with u.
-
:earlier 10mReturn the file to the state it was in ten minutes ago. The :later 10m command moves forward again. The :earlier 1f form returns to the state at the last save, and running it again returns to the save before that.
Vim: deleting, copying, and pasting
-
ddDelete the current line and keep it in memory so you can paste it elsewhere, because in Vim deleting also works as cutting. A number before the command, for example 5dd, deletes five lines starting from the current line.
-
dwDelete from the cursor to the start of the next word.
-
DDelete from the cursor to the end of the line. The command works like d$.
-
dGDelete from the current line to the end of the file. The ggdG combination deletes the entire content of the file, and you can undo the deletion with u.
-
:10,20dDelete lines 10 to 20 through the command line, without moving the cursor to them.
-
yyCopy the current line. A number before the command, for example 5yy, copies five lines.
-
ywCopy from the cursor to the start of the next word. The y$ form copies to the end of the line.
-
pPaste what you copied or deleted after the cursor. Whole lines are pasted below the current line.
-
PPaste before the cursor. Whole lines are pasted above the current line.
-
ddpSwap the current line with the line below it: delete the line and paste it one line down.
-
:10,15m$Move lines 10 to 15 to the end of the file. The :10,15t$ command copies them to the end of the file instead of moving them, and you can replace $ with the number of the target line.
-
"ayyCopy the line to a register named a. You can use any letter. This way you can keep several different snippets at the same time, and the "ap command pastes the content of register a.
-
:regShow the content of all the registers: the most recently copied text, the most recently deleted text, and the named registers. Register 0 keeps the last copy even after deletions, so "0p pastes it.
-
"+yyCopy the current line to the system clipboard, so you can paste it into other programs. In Visual mode, the "+y command copies the selected text, and the "+p command pastes from the clipboard. The command works only in a Vim build with clipboard support, and over an SSH connection to a server it is usually not available.
-
"_ddDelete the line without keeping it in memory, so the text you copied earlier stays available for pasting.
Vim: search and replace
-
/ListenSearch forward for the word Listen from the cursor position. The Enter key runs the search, and the search is case-sensitive by default.
-
?ListenSearch backward, from the cursor toward the start of the file.
-
n / NMove to the next and previous result of the last search, respectively.
-
*Search forward for the word under the cursor as a whole word, without typing it. The # key searches for it backward.
-
/listen\cSearch without case sensitivity, for the current search only. The \c marker can appear anywhere in the pattern.
-
/\<port\>Search for port as a whole word only, so words such as import or ports are not matched.
-
:nohRemove the highlighting of search results from the screen, without changing any settings. The highlighting returns with the next search.
-
:s/old/new/Replace the first occurrence of old with new in the current line only.
-
:s/old/new/gReplace all occurrences of old in the current line. The g flag replaces every occurrence in the line, not just the first one.
-
:%s/old/new/gReplace all occurrences of old with new in the whole file. The % sign stands for all the lines, and the u key undoes the entire replacement in one step.
-
:%s/old/new/gcReplace in the whole file, with a confirmation prompt for each occurrence. At each occurrence, press y to replace, n to skip, a to replace all the remaining occurrences, q to stop, or l to replace the current occurrence and stop.
-
:%s/old/new/giReplace in the whole file without case sensitivity. The i flag ignores case for this replacement only.
-
:10,20s/old/new/gReplace only in lines 10 to 20. You can also specify a relative range, for example :.,+5s/old/new/g, which applies to the current line and the five lines after it.
-
:%s#/var/www/old#/var/www/new#gReplace a path that contains slashes, using the # character as the delimiter instead of the slash. This way you do not need to escape every slash in the path with a backslash.
-
:%s/\<user\>/admin/gReplace user with admin only where user appears as a whole word, so words such as username do not change.
-
:%s/old//gnCount how many times old appears in the file, without replacing anything. The n flag shows only the number of matches.
-
:%s/\s\+$//eDelete spaces and tabs at the end of every line in the file. The e flag prevents an error message when no such spaces are found.
-
:10,20s/^/#/Add the # character at the start of lines 10 to 20, which turns them into comments in many configuration files. The :10,20s/^#// command removes the character and makes the lines active again.
-
:g/^$/dDelete all the empty lines in the file. The :g command runs a command on every line that matches the pattern, and you can use other commands instead of d.
-
:g/^\s*#/dDelete all the comment lines that start with #, including indented lines. This is useful for showing only the active settings in a copy of a long configuration file. It is best not to save the change to the original file.
-
:v/error/dDelete all the lines that do not contain the word error, leaving only the relevant lines. The command suits filtering a copy of a log file.
Vim: Visual mode
-
vStart selecting text character by character. Moving the cursor extends the selection, and then you can press d to delete, y to copy, or c to change.
-
VStart selecting whole lines. This suits copying, deleting, or indenting several lines.
-
Ctrl+vStart selecting a rectangular block, meaning a column of characters across several lines. This is useful for editing the same position in several lines at once.
-
Ctrl+v then I# then EscAdd the # character at the start of several lines, which turns them into comments. After Ctrl+v, select the lines by moving down, for example with the j key, and then type I#. The character appears in all the lines only after you press Esc.
-
> / <In Visual mode, shift the selected lines one indentation level right or left.
-
:'<,'>s/old/new/gReplace only in the selected lines. Pressing : in Visual mode adds the '<,'> range automatically, so you only need to complete the replace command. The replacement applies to the whole lines, even when only part of them is selected.
-
gvReselect the last Visual mode selection. This is useful when you need to perform another action on the same lines.
-
oIn Visual mode, move the cursor to the other end of the selection, so you can extend or shrink it from both sides.
-
U / uIn Visual mode, convert the selected text to uppercase or lowercase, respectively.
Vim: multiple files, windows, and tabs
-
:e other.confOpen another file for editing in the current window. When the current file has unsaved changes, save them first.
-
:lsShow the list of open files (buffers), with a number for each file. A + sign next to a file indicates unsaved changes.
-
:bn / :bpSwitch to the next and previous open file in the list, respectively.
-
:b 2Switch to file number 2 in the :ls list. You can also type part of the file name, for example :b other.
-
:bdClose the current file and remove it from the list of open files, without quitting Vim.
-
:sp other.confSplit the screen horizontally and open the file in a new window above the current window. Without a file name, the command shows the current file in two windows.
-
:vsp other.confSplit the screen vertically and open the file in a new window next to the current window. This is useful for comparing a configuration file with its backup.
-
Ctrl+w wMove between the open windows in order. Ctrl+w followed by h, j, k, or l moves to the window in that direction.
-
Ctrl+w qClose the current window. The :only command closes all the other windows and keeps only the current window.
-
Ctrl+w =Make all the open windows the same size.
-
vim -O a.conf b.confOpen two files side by side in vertical windows. The lowercase -o option opens them one above the other.
-
vim -p a.conf b.confOpen each file in a separate tab.
-
:tabnew other.confOpen the file in a new tab. Without a file name, an empty tab opens.
-
gt / gTSwitch to the next and previous tab, respectively. A number before gt, for example 2gt, switches to tab number 2.
-
:tabcloseClose the current tab.
-
vimdiff example.conf example.conf.bakOpen two files side by side and highlight the differences between them in color. The vim -d command works the same way.
-
]c / [cIn diff mode, move to the next and previous difference between the files. The dp command copies the current difference to the other file, and the do command copies it from the other file.
-
:r other.confInsert the content of another file below the current line.
-
:r !dateRun a shell command and insert its output below the current line. This is useful for adding a date or the output of a command to a file.
-
:!ls -lRun a shell command and show its output without leaving Vim. The Enter key returns you to the editor.
Vim: display and editing settings
-
:set numberShow line numbers at the side of the screen. The :set nonumber command hides them, and the :set nu! command toggles between the two.
-
:set relativenumberShow the distance of each line from the current line, which makes commands such as 7dd or 4j easier. Combined with :set number, the current line shows its actual number.
-
:syntax onTurn on color syntax highlighting based on the file type, which makes it easier to spot comments, strings, and typos.
-
:set hlsearchHighlight all the results of the last search in color. The :set incsearch command shows the match while you type the search pattern.
-
:set ignorecase smartcaseMake searches case-insensitive, unless the search pattern includes an uppercase letter.
-
:set pasteTemporarily turn off automatic indentation and completion, so pasting text in Insert mode does not create stepped indentation. The :set nopaste command restores normal behavior. In recent versions and supporting terminals, pasting is detected automatically, so the setting is mainly needed in older terminals.
-
:set expandtab tabstop=4 shiftwidth=4Make the Tab key insert four spaces instead of a tab character, and set one indentation level to four spaces. The :retab command converts existing tab characters in the file based on the setting.
-
:set listShow invisible characters, such as a tab (^I) and the end of a line ($). This is useful for finding tabs and extra spaces in indentation-sensitive files, such as YAML files. The :set nolist command turns the display off.
-
:set nowrapShow long lines on a single screen line, without wrapping to the next line. The :set wrap command turns wrapping back on, and the content itself does not change in either case.
-
:set ff=unixSave the line endings in Linux format. The change is written to the file when you save with :w. This is useful for a file created on Windows that causes errors in a script, and the :set ff? command shows the current format. When ^M characters appear at the end of some lines, the :%s/\r$// command removes them.
-
:set fileencoding?Show the character encoding of the file. The :set fileencoding=utf-8 command followed by :w saves the file in UTF-8 encoding.
-
:set mouse=aTurn on mouse support: clicking moves the cursor, dragging selects text, and the mouse wheel scrolls. In this mode, regular terminal copying may require holding Shift while you select.
-
~/.vimrcThe personal Vim configuration file. Settings you write in it, for example set number or syntax on, each on a separate line and without a colon, load every time the editor opens.
Vim: macros
-
qaStart recording a macro to register a. You can use any letter. Every key you press from this point is saved, and recording @a appears at the bottom of the screen. Pressing q in Normal mode stops the recording.
-
@aRun the macro recorded to register a. The @@ command repeats the last macro you ran.
-
10@aRun the macro ten times in a row. End the recording by moving to the next line, for example with the j key, so each run works on a new line.
-
:5,20norm @aRun the macro once on each line in the range 5 to 20. Each run starts at the beginning of the line, so you do not need to include a move to the next line in the recording.
Vim: root files and recovery
-
sudoedit /etc/ssh/sshd_configEdit a file owned by root the recommended way: the editor runs as your regular user on a temporary copy, and the copy replaces the original file only after you quit the editor, when the content has changed. The editor is chosen based on the SUDO_EDITOR, VISUAL, or EDITOR variables, and the sudo -e command works the same way.
-
:w !sudo tee % > /dev/nullSave a file that you opened without root privileges, when you find out at save time that you lack permission. The command requires sudo privileges. After the save, Vim shows a warning that the file changed outside the editor, and you can press L to reload it. In Neovim, the command usually does not work, because Neovim does not let you type the sudo password, so for everyday work, use sudoedit.
-
:w /tmp/example.confSave a copy of your work to a directory you have write permission to, when saving to the original file fails. You can then copy it to the original location with the sudo cp command, so your changes are not lost.
-
E325: ATTENTIONA message that appears when a swap file exists for the file you open, meaning the file is already open in another editor or a previous session did not end properly. Press O to open it as read-only, E to edit anyway, R to recover, or Q to quit. In some cases, the D option to delete the swap file is also shown.
-
vim -r example.confRecover unsaved changes from the swap file, for example after an SSH connection drops. After you check the content, save the file with :w, and then delete the swap file so the E325 message does not appear again.
-
vim -rShow the list of swap files found, with the original file name and the modification time, without opening a file.
-
rm .example.conf.swpDelete the swap file, which is usually saved as a hidden file in the same directory as the file. Caution: delete it only after you recover the changes you need and make sure the file is not open in another editor.
nano: opening, saving, and quitting
-
nano example.confOpen the file for editing in nano. Unlike Vim, you can start typing right away, and the help lines at the bottom of the screen show the main shortcuts.
-
nano +25 example.confOpen the file with the cursor on line 25. The nano +25,10 example.conf form also places the cursor in column 10.
-
nano -v example.confOpen the file as read-only, with no way to change the content.
-
Ctrl+OSave the file. nano shows the file name for confirmation, and the Enter key completes the save. You can also change the name before you press Enter, to save the content to another file.
-
Ctrl+SSave the file immediately, without asking for the file name, when the file already has a name. The shortcut is available in recent versions of nano, and in older versions, use Ctrl+O.
-
Ctrl+XExit nano. When there are unsaved changes, nano asks whether to save them: press Y to save (and then Enter to confirm the file name), N to exit without saving, or Ctrl+C to cancel the exit.
-
Ctrl+RInsert the content of another file at the cursor position, after you type the file name and press Enter.
-
Ctrl+GShow the full help screen, with all the shortcuts of the installed version. Ctrl+X closes the help screen.
-
Ctrl+CAt a prompt on the bottom line, such as search or save, cancel the action. During regular editing, the shortcut shows the cursor position: the line, column, and character number.
nano: moving around a file
-
Ctrl+A / Ctrl+EMove the cursor to the start and end of the line, respectively.
-
Ctrl+Y / Ctrl+VScroll one screen up and one screen down, respectively. The Page Up and Page Down keys also work.
-
Alt+\ / Alt+/Move the cursor to the first and last line of the file, respectively.
-
Ctrl+_Go to a specific line after you type the line number and press Enter. You can also type a line and a column, for example 25,10. In recent versions, the Alt+G shortcut works the same way, and on many keyboards you type Ctrl+_ as Ctrl+Shift+minus.
-
Ctrl+Left / Ctrl+RightMove the cursor one word back and one word forward, respectively, in recent versions of nano.
-
Ctrl+P / Ctrl+NMove the cursor one line up and one line down, as an alternative to the arrow keys.
-
Alt+]Jump to the bracket that matches the one under the cursor. This is useful for finding a missing bracket in a configuration file or in code.
nano: cutting, copying, and editing
-
Ctrl+KCut the whole current line. Several presses in a row cut several lines, and the next paste inserts all of them together.
-
Ctrl+UPaste what you last cut or copied at the cursor position. You can paste the same content several times.
-
Alt+6Copy the current line, or the selected text, without deleting it. You paste it with Ctrl+U.
-
Alt+AStart selecting text from the current position. Moving the cursor extends the selection, and then Ctrl+K cuts the selected text and Alt+6 copies it. Pressing Alt+A again cancels the selection, and the Ctrl+6 shortcut works the same way.
-
Alt+TCut all the text from the cursor to the end of the file. Caution: in a long file, this removes a large part of the content. You can paste it back with Ctrl+U or undo the action with Alt+U.
-
Alt+UUndo the last action. Each additional press undoes an earlier action.
-
Alt+ERedo an action that you undid with Alt+U.
-
Alt+3In recent versions, comment out the current line or the selected lines, and pressing it again uncomments them. The comment character depends on the file type, and in most configuration files it is #.
-
Alt+} / Alt+{Shift the current line, or the selected lines, one indentation level right or left.
-
Ctrl+DDelete the character under the cursor, like the Delete key.
-
Ctrl+JJustify the current paragraph and join its lines based on the configured line width. Caution: in configuration files, this can join separate lines, and you can undo it with Alt+U.
-
Alt+DShow the number of lines, words, and characters in the file, or only in the selected text.
-
Alt+VInsert the next key as is, without running its shortcut. This is useful for inserting a real tab character when nano is set to convert tabs to spaces.
nano: search and replace
-
Ctrl+WSearch for text in the file, after you type the text and press Enter. Pressing Enter at an empty search prompt repeats the previous search.
-
Alt+W / Alt+QAlt+W moves to the next result of the last search, and in recent versions Alt+Q moves to the previous result.
-
Ctrl+\Replace text: type the text to search for, press Enter, type the new text, and press Enter. At each occurrence, press Y to replace, N to skip, A to replace all occurrences, or Ctrl+C to stop. The Alt+R shortcut works the same way.
-
Alt+CAt the search prompt, toggle case sensitivity on or off. By default, search in nano is not case-sensitive.
-
Alt+RAt the search prompt, toggle regular expression search on or off, for example ^# to find lines that start with a comment.
-
Alt+BAt the search prompt, switch the search direction so it runs backward, toward the start of the file.
nano: options and settings
-
nano -l example.confOpen the file with line numbers at the side of the screen. In recent versions, the Alt+N shortcut shows or hides line numbers while you edit.
-
nano -m example.confTurn on mouse support: clicking moves the cursor, and double-clicking selects text. In this mode, regular terminal copying may require holding Shift while you select.
-
nano -B example.confSave a copy of the previous version of the file on every save, under the file name with a ~ suffix, for example example.conf~. The -C option followed by a directory path sets where the copies are saved.
-
nano -i example.confTurn on automatic indentation: a new line starts with the same indentation as the line above it.
-
nano -E -T 4 example.confMake the Tab key insert spaces instead of a tab character, and set the tab width to four characters. This suits indentation-sensitive files, such as YAML files.
-
nano -w example.confPrevent automatic wrapping of long lines while you type. In older versions, this wrapping can break configuration files, and in recent versions it is off by default.
-
nano -c example.confShow the cursor position on the bottom line at all times: the line, column, and character number.
-
Alt+PShow or hide visible markers for tab characters and spaces. This is useful for finding tabs and extra spaces in indentation-sensitive files.
-
Alt+XHide or show the help lines at the bottom of the screen, to make room for text.
-
~/.nanorcThe personal nano configuration file. Write each setting on a separate line, for example set linenumbers, set tabsize 4, or set autoindent, and it loads every time the editor opens.
On the server: safe editing and the default editor
-
cp -p example.conf example.conf.bakBack up the configuration file before you edit it, keeping the permissions and the modification time. If something goes wrong, you can restore the backup and restart the service.
-
diff -u example.conf.bak example.confShow the changes made to the file compared with the backup. Added lines are marked with a + sign, and removed lines are marked with a - sign.
-
export EDITOR=nanoSet nano as the default editor in the current terminal session, for commands that open an editor, such as crontab -e. You can replace nano with vim.
-
echo 'export EDITOR=vim' >> ~/.bashrcSet Vim as the default editor permanently for the current user. The change takes effect at the next login or after you run source ~/.bashrc. Some commands prefer the VISUAL variable, so set it to the same value as well.
-
echo $EDITORShow the default editor currently set. Empty output means that the variable is not set, and each command then chooses an editor based on its own default.
-
select-editorOn Debian/Ubuntu distributions, show a list of the installed editors and set the selected editor for the current user. The choice is saved in the ~/.selected_editor file.
-
sudo update-alternatives --config editorOn Debian/Ubuntu distributions, set the default editor for all users on the system from the list of installed editors. A user who set their own EDITOR or VISUAL keeps working with the editor they chose.
-
EDITOR=nano crontab -eOpen the list of scheduled tasks for editing in nano, for this command only, without changing the default. When the VISUAL variable is set, it takes precedence over EDITOR, so set VISUAL instead.
-
sudo visudoEdit the sudo permissions file the safe way: visudo checks the syntax before saving and prevents saving a broken file that could block the use of sudo. When it finds an error, press e to go back to editing.
-
sudo apt install vim nanoInstall Vim and nano on Debian/Ubuntu distributions. On Red Hat-based distributions, the equivalent command is sudo dnf install vim-enhanced nano, which installs the full Vim, because a basic installation sometimes includes only a minimal version of it.
No matching shortcuts.
Vim and nano are installed on most Linux servers, and you use them to edit configuration files, scripts, and scheduled tasks directly over an SSH connection. This page is organized by editor and by topic, and the last section brings together commands for working safely on the server and choosing the default editor. The vi editor supports most of the basic Vim commands on this page.
In the nano help lines, the ^ character stands for the Ctrl key and M- stands for the Alt key, so ^X means Ctrl+X and M-U means Alt+U. When the Alt key does not work in your terminal, as in some terminals on Mac computers, press Esc and then the key you want. Some shortcuts changed between nano versions, so Ctrl+G shows the exact list for the installed version. In Vim, you type the commands that start with a colon from Normal mode, so press Esc before you type them.
A text editor does not check whether a configuration file is valid, so back up the file before you edit it. After you save it, run the syntax check command of the service, such as sshd -t for the SSH service, before you restart the service. When you edit scheduled tasks with crontab -e, you can use the Cron expression generator to build the schedule expression.
Related tools
Vim and nano shortcuts FAQ
How do I exit Vim?
Press Esc to return to Normal mode, and then type :wq and press Enter to save and quit, or type :q! and press Enter to quit without saving. When several files or windows are open, the :qa! command quits all of them without saving.
How do I save and exit nano?
The Ctrl+O shortcut followed by Enter saves the file, and the Ctrl+X shortcut exits the editor. When there are unsaved changes, nano asks on exit whether to save them: press Y and Enter to save, or N to exit without saving.
What is the difference between Vim and nano, and which editor should I use?
nano suits quick edits and anyone who edits files on a server only occasionally, because you can start typing right away and the main shortcuts are shown at the bottom of the screen. Vim suits anyone who works on servers regularly: it is based on modes and takes a little time to learn, but it makes moving around, editing, searching, and replacing much faster. In addition, the vi editor is available on almost every system, even when nano is not installed.
Why do keys in Vim run actions instead of typing text?
Vim opens in Normal mode, where every key is a command: for example, x deletes a character and dd deletes a line. To type text, press i, and to return to command mode, press Esc. When you make a change by mistake, the u key undoes it.
How do I replace a word in the whole file in Vim?
The :%s/old/new/g command replaces every occurrence of old with new in the whole file, and adding the c flag at the end, as in :%s/old/new/gc, asks for confirmation before each replacement. In nano, you replace text with the Ctrl+\ shortcut, and after you type the old and new text, you can press A to replace all occurrences.
How do I recover unsaved changes in Vim after a disconnection?
The vim -r example.conf command recovers the changes from the swap file left after the disconnection. After you check the content, save with :w and delete the swap file. As long as the swap file exists, Vim shows the E325: ATTENTION message when you open the file, and in that message the R key recovers the changes. When the message appears because the file is open in another window, press Q and keep working in that window.