“Nano” text editor tricks (for CKAD exam)

Pranay Shah
10 min readAug 19, 2020

--

I am writing this for folks who would like to use ‘nano’ text editor for Certified kubernetes application developer (CKAD) certification exam. Although tips & tricks apply to anyone how wants to use ‘nano’ text editor to edit a text file on Linux shell.

CKAD is a performance-based exam and you need to edit all files that you need to create/modify in browser base SSH shell (katacoda type shell)- and you need to do it fast/quickly. Stating the obvious these all tips & tricks also applies to nano that you use it on your laptop or on your putty/ssh session as well — I am just going to focus more on browser base SSH session.

Now you have two main choices when it comes to an editing text file in these browser-based SSH shells — vim or nano. As per me 95% folks will go for Vim(vi) text editor but somehow from the beginning, I never got used to Vi — it was not a natural way to edit/write stuff for me (I am sure a lot of folks just frown by reading this ) — nano/pico (as it was its predecessor) was very natural to me and I kept on using it (last 10+ years). But until I had to give CKAD exam I never knew that many things you can do easily in GUI based text editor is not that obvious in nano thus it started my exploration with nano to know more. In this doc I will go over what I discovered that is possible and must learn in order to use nano successfully in CKAD exam — we will quickly cover a few things :

  1. How to copy a block of text in a nano text editor and paste it in the same file
  2. How to comment a block of text (prepending each line with #)
  3. How to show spaces as “.” so you know how many hidden spaces are there
  4. How to move the cursor using a mouse instead of up and down arrow
  5. How to replace tab with 4 spaces (Kubernetes manifest files are written in YAML and tabs are not allowed in YAML )
  6. How to start next line at the same indentation as above line (very helpful for YAML files)
  7. How to show line numbers and how to go to a particular line number quickly
  8. How to find string in YAML, How to find and replace a string in YAML
  9. How to bulk indent block of code so quick easy YAML editing
  10. How to undo in nano
  11. How to cut & paste line (bulk delete/bulk cut/paste)

We will see all this using katacoda shell — at the end I will also show some things that are only picular to katacoda. In real exam the browser base terminal is much more user friendly .

Quick Note on animation: I am showing all these tricks using animation that captures my keystroke on my computer screen so you know what key combination I pressed to get that action (as that was the most confusing part when you read online articles about some of the key shortcuts of nano)

KeyCaster is a Mac tool that shows key pressed on screen.

Keystrokes will be shown in a virtual keyboard and in KeyCaster output as well

In all key shortcuts I describe below, you will see that I use the “Escape” key in many key-combinations that is because that is what Meta-key was mapped to in my keyboard — check on yours what is your Meta-key mapped to — possible options for Meta-key are ‘Alt’, ‘Cmd’ or ‘Esc’ keys.

snippet from ‘man’ page of nano

My keyboard (I use windows based keyboard for Mac computer )

My Logitech MK320 keyboard

Now let's get to business...

How to copy a block of text in a nano text editor and paste it in the same file

In this case, we want to copy 3 blocks of line and paste it again in the same file

  1. Go the start of line block — press Esc + a
  2. Use down arrow to copy as many lines we need to copy
  3. Now press Esc + 6 to copy the text in nano’s buffer
  4. Now to a go-to place where you need to put copied text and hit Ctrl + u to paste it
Block copy and paste in the same file

Above steps do look like a lot of steps but if you do enough time it becomes muscle memory

How to comment a block of text (prepending each line with #)

This one is easy — let say you have 3 blocks of lines you need to comment out steps are :

  1. First, select a block of text using Esc + 6 and then down arrow (just like above)
  2. Now to comment those line use Esc + 3 key combination (press one after other)
How to comment out a few lines

To uncomment do step 2 above again

How to remove un-comment text

How to show spaces as “.” (dots) so you know how many hidden spaces are there (visualize the space)

In case of YAML spaces/indentation is essentials so it is good to see how many spaces are there at any given line — to do that you do this

  1. Press Esc + p (this will convert each space with “.” so you can see it visually)
Show spaces as “.” dot for visual verification

To go back to spaces either exit the nano and re-open it or press Esp + p to go back to spaces

How to move the cursor using a mouse instead of up and down arrows

This is a very nifty trick that will save you a lot of speed if you have a large file and want to move your cursor to a specific place fast

  1. For this, you need to start nano with the flag -m as per ‘man’ page this does

-m, — mouse
Enable mouse support, if available for your system. When enabled, mouse clicks can be used to place the cursor, set the
mark (with a double click), and execute shortcuts. The mouse will work in the X Window System, and on the console when gpm

Use the mouse to move around the cursor in the file
If the nano editor loses focus when it gains it back cursor can move — base on where the user clicked it.

Danger .. Danger: This has some unintended consequences — that is if you want to paste something from your computers clipboard and by mistake, your mouse got clicked on other places than ‘paste’ will put text at that place instead of where you wanted to (previous cursor position)

How to replace tab with 4 spaces (Kubernetes manifest files are written in YAML and tabs are not allowed in YAML )

In YAML file ‘tabs’ are not allowed so if you have a habit of indenting faster with using ‘tab’ [\t] than nano has trick to for you

  1. Start nano with option -ET4 this will convert tab to spaces and each tab will be worth of 4 spaces

-T number, — tabsize=number
Set the size (width) of a tab to number columns. The value of number must be greater than 0. The default value is 8.

-E, — tabstospaces
Convert typed tabs to spaces.

Change Tab behavior to use spaces instead

How to show line numbers and how to go to a particular line number quickly

During the exam, if you have an error in your YAML you will be given error with line number where there is a potential issue — once you know the line number you should be able to visually know what is an issue if you can see line numbers

For this, we need to use two more options for nano -c and -l

-c, — constantshow
Constantly show the cursor position on the status bar. Note that this overrides option -U ( — quickblank).

-l, — linenumbers
Display line numbers to the left of the text area.

  1. Start nano editor with -c and -l option ( from above steps we know options -mET4 is good as well so we will combine all of them)
nano options -c & -l

How to start next line at the same indentation as above line (very helpful for YAML files)

This is most helpful in the case of YAML as YAML is formated based on indentation you need to either start next line with the same indentation or one extra (for child element)

To enable this in nano it is very easy just start with an option “-i”

-i, — autoindent
Indent new lines to the previous line’s indentation. Useful when editing source code.

Once you start with -i option you can see when you start next line (by hitting enter) the cursor start the same place as above line (this is extremely helpful and time saver)

How to find string in YAML, How to find & replace a string in YAML

This is very easy to do and essential that you remember how to do this

How to find a text

  1. Press Ctrl + w (for where !)
  2. Type in the text you are looking for
  3. Press Enter
  4. To search next occurrence (same text) just type Ctrl + w again and Enter
  5. For case sensitive search press Esc + c

How to bulk indent block of code so quick easy YAML editing

This comes handy when you copy some text from kubernetes documentation and you need to add it to your YAML file but indentation is not correct — nested pod specification is a good example

In order this to work you must start with option -ET4 so indentation uses spaces and not tab.

nano -cmilET4 <FILE>

To indent a block of lines you do this :

  1. Copy block of the test via Esc + a followed by down or up arrow and select the text
  2. to indent press Esc and then Shift + } key that will indent block by 4 spaces

To remove indentation you do the same steps as above but press Shift + { to remove indentation by 4 spaces

How to undo in nano

We all make mistakes — and when that happens we all need to undo; Undo in nano is simply Esc + u ( u for undo! )

How to cut line (bulk delete/bulk cut)

It is always good to know how to delete multiple lines one by one or select all and delete it

Select a block of text and delete it

  1. Select block of text via Esc + 6, and up and down arrow to select a block of text
  2. Use F9 key to delete text
  3. Actually F9 cuts the text and put it in nano’s buffer so if you need to paste this somewhere else in a document you can just go to that part of the document and press F10 to paste it back.

Delete individual lines

It is same as above

  1. No need to select that line — just take your cursor to that line and press F9
  2. If you need to delete 3 to 4 lines after that then just keep F9 pressed and it will delete one line at a time
  3. All the line it deletes is put in nano’s buffer so if you need to put it back just press F10

Nano and Katacoda — limitation

If you have to copy-paste text from the browser into the nano text editor inside katacoda browser base shell you can see in below animation text gets mingled (even if don’t use -cmilET4 option result is pretty much the same)

So for this, I did fallback to Vim and I used vim to copy and paste text from the browser (in Vim do set `:set paste` switch). Once the text is copied to YAML I revert back to using nano for my further editing.

Making change (semi) permanent

During the CKAD exam, you will be editing YAML files several times and you can’t remember or have time to start nano with options -cmilET4 — instead best is to save it to ~/.bashrc file and source it. This way any nano command you type in your session it will automatically start with options -cmilET4

Hope this was helpful to folks out there who want to use nano instead of Vim during CKAD exam

--

--