CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / File Viewing & Editing / nano

nano

Edit files with nano, the beginner-friendly editor

Beginnereditorconfigbeginner

nano is a small, modeless terminal editor: you type and the text goes in, and the key shortcuts are listed at the bottom of the screen. It's Ubuntu's default editor for crontab -e, visudo and git, and the quickest safe way to make a small config change when you don't know vim.

Syntaxnano [OPTIONS] [+LINE[,COLUMN]] FILE...

Watch it run

You need to change one setting in a server config and you're not comfortable with vim.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 4
sudo nano /etc/nginx/nginx.conf

Title bar at the top, file in the middle, [ Read 14 lines ] on the status line, and the shortcuts at the bottom (^ = Ctrl, M- = Alt). Just start typing: there's no insert mode.

Anatomy of a command

$
prefixsudo

Needed to save a root-owned file. sudoedit is the safer alternative: you edit a copy as yourself.

How it works

nano reads the file into memory and draws it with the curses library: a title bar (version, file name, and * when modified), the text, a status line for messages and prompts, and two lines of shortcuts. ^ means Ctrl and M- means Alt (or Esc then the key). Because there are no modes, every printable key inserts text; commands are always Ctrl or Alt combinations, which is why it's easier for beginners than vim. On ^O (Write Out) nano writes the buffer to the file, overwriting it in place so the inode, owner and permissions stay the same. With locking on (Ubuntu's /etc/nanorc sets it), nano creates a lock file (.name.swp, in vim's format), so a second nano or vim on the same file is warned. If the file isn't writable, nano says so on opening and the save fails with Permission denied. Many tools start the editor named in $VISUAL or $EDITOR, falling back to the system's editor alternative, which on Ubuntu is nano.

Options you'll actually use

^O then EnterWrite Out (save) to the file name shown
^SSave immediately, without the file-name prompt
^XExit; asks Save modified buffer? Y/N if changed
^W / ^\Search (Where Is) / search and replace
^K / ^UCut the current line / paste (uncut)
^/ or ^_Go to line (and column)
M-U / M-EUndo / redo
^CShow cursor position (line, column)
-lShow line numbers
+NOpen at line N
-vView mode: read-only
-BKeep a backup of the original as file~ when saving

Common mistakes

Pressing Ctrl+Z to undo out of habit

nano 7 answers '[ To suspend, type ^T^Z ]' and undoes nothing; older versions suspend the editor to the background, which looks like it vanished (fg brings it back).

M-U (Alt+U) to undo, M-E to redo; ^S saves straight away, ^O saves with a file-name prompt
Editing a YAML or Python file with nano's default tab settings

Tab inserts a literal tab character, which YAML rejects and Python may mix with spaces.

nano -E (convert tabs to spaces) with -T 2, or set tabstospaces in ~/.nanorc
sudo nano /etc/sudoers

A single typo makes sudoers unparsable, and then nobody can use sudo to fix it.

sudo visudo (opens nano on Ubuntu, but checks the syntax before installing the file)

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Open ~/data/notes.txt in nano with line numbers, add a line 'Checked by devops' at the end, save and exit.

  2. 2

    Open /etc/app/app.conf at line 9 and find the word 'level' without moving the cursor manually.

  3. 3

    Change maintenance_mode=off to on in /etc/app/features.conf the safe way.

Related commands

vimEdit files on any server with vimlessBrowse files and output: scroll, search, filter, followcrontabSchedule recurring jobs with cronsudoRun a command as root or another user
PreviousvimNextwc
Linux / File Viewing & Editing / nano

nano

Edit files with nano, the beginner-friendly editor

Beginnereditorconfigbeginner

nano is a small, modeless terminal editor: you type and the text goes in, and the key shortcuts are listed at the bottom of the screen. It's Ubuntu's default editor for crontab -e, visudo and git, and the quickest safe way to make a small config change when you don't know vim.

Syntaxnano [OPTIONS] [+LINE[,COLUMN]] FILE...

Watch it run

You need to change one setting in a server config and you're not comfortable with vim.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 4
sudo nano /etc/nginx/nginx.conf

Title bar at the top, file in the middle, [ Read 14 lines ] on the status line, and the shortcuts at the bottom (^ = Ctrl, M- = Alt). Just start typing: there's no insert mode.

Anatomy of a command

$
prefixsudo

Needed to save a root-owned file. sudoedit is the safer alternative: you edit a copy as yourself.

How it works

nano reads the file into memory and draws it with the curses library: a title bar (version, file name, and * when modified), the text, a status line for messages and prompts, and two lines of shortcuts. ^ means Ctrl and M- means Alt (or Esc then the key). Because there are no modes, every printable key inserts text; commands are always Ctrl or Alt combinations, which is why it's easier for beginners than vim. On ^O (Write Out) nano writes the buffer to the file, overwriting it in place so the inode, owner and permissions stay the same. With locking on (Ubuntu's /etc/nanorc sets it), nano creates a lock file (.name.swp, in vim's format), so a second nano or vim on the same file is warned. If the file isn't writable, nano says so on opening and the save fails with Permission denied. Many tools start the editor named in $VISUAL or $EDITOR, falling back to the system's editor alternative, which on Ubuntu is nano.

Options you'll actually use

^O then EnterWrite Out (save) to the file name shown
^SSave immediately, without the file-name prompt
^XExit; asks Save modified buffer? Y/N if changed
^W / ^\Search (Where Is) / search and replace
^K / ^UCut the current line / paste (uncut)
^/ or ^_Go to line (and column)
M-U / M-EUndo / redo
^CShow cursor position (line, column)
-lShow line numbers
+NOpen at line N
-vView mode: read-only
-BKeep a backup of the original as file~ when saving

Common mistakes

Pressing Ctrl+Z to undo out of habit

nano 7 answers '[ To suspend, type ^T^Z ]' and undoes nothing; older versions suspend the editor to the background, which looks like it vanished (fg brings it back).

M-U (Alt+U) to undo, M-E to redo; ^S saves straight away, ^O saves with a file-name prompt
Editing a YAML or Python file with nano's default tab settings

Tab inserts a literal tab character, which YAML rejects and Python may mix with spaces.

nano -E (convert tabs to spaces) with -T 2, or set tabstospaces in ~/.nanorc
sudo nano /etc/sudoers

A single typo makes sudoers unparsable, and then nobody can use sudo to fix it.

sudo visudo (opens nano on Ubuntu, but checks the syntax before installing the file)

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Open ~/data/notes.txt in nano with line numbers, add a line 'Checked by devops' at the end, save and exit.

  2. 2

    Open /etc/app/app.conf at line 9 and find the word 'level' without moving the cursor manually.

  3. 3

    Change maintenance_mode=off to on in /etc/app/features.conf the safe way.

Related commands

vimEdit files on any server with vimlessBrowse files and output: scroll, search, filter, followcrontabSchedule recurring jobs with cronsudoRun a command as root or another user
PreviousvimNextwc