⬆️ ⬇️

Two in one: how to use vim and nano?

Upd. 9.7.2017 : updated section about vim. Thank you for valuable additions.

opening speech



Why do we need another one, 1001nd publication on this topic? There are a huge number of articles about vim and nano, but as a rule they concern only one of the editors, or are a general overview. So that both editors were succinctly described in one, but without deepening the jungle, all the main control keys for full-fledged work are given, I didn’t find it. Therefore, after reading the materials found, I began to outline them, and this article was born.





Any text editor can be mastered "at random." But not vim.

How is nano better than vim?

You can exit nano without reset! (c) Internet





Initially, I did not plan to write about both editors, but I wanted to make a brief reference only on nano, but in the process of collecting information, data was accumulated on vim too. Then I discovered that Vim is not so scary if you know the commands and the concept of its use.

')

What is it and why?



When a graphical environment is not available, for example, when working on ssh, setting up the system before loading xserver, or just for the sake of perversions, you begin to feel like a flawed disabled person in a console text editor, you don’t know the control keys from the word. Even file navigation and copy-paste are starting to be an almost insurmountable obstacle. Just because many familiar keyboard shortcuts do not work in the console, being used for other needs even during diskettes 5.25 "at 320Kb.



I used the Midnight Commander mcedit editor for a long time, used nano a bit, and didn't use vim at all. To grind all these combinations beyond the limits of the possible - even after spending time on it, after some period of inactivity they are successfully forgotten. Before I started writing this article, I couldn’t force myself to master even the basics of vim , he was scared at the launch stage, “trying to get out of it right away, and you can’t!” This is how you will go to the ZX Spectrum interpreter BASIC48, do not write the basics of even the simplest PRINT program “Hello World!”. I did not cease to ask myself the question: “It is interesting, and for what do gurus vim love so much? Why waste a lot of time on retraining, and most importantly, how not to lose skills, for what purposes it is constantly used, this vim? ”



Below I will describe the most necessary key combinations and working methods in both editors.



Nano



After the Alt-6 combination, as a “copy” command, I don’t even know what to say.

Probably the author of Nano was a pianist

© of habr comments



The basis for me was an article in Habré: " Nano: And yet it will have to be learned ", as well as all the comments to it. They not only added it, but also forced them to look towards vim .



Ros Nano-to onception:



In terms of nano, a text separated by empty lines is called a paragraph. All commands are given by pressing the key combinations - function buttons or the key combination Ctrl + letter or Alt + letter. If in any case the combination with Ctrl or Alt is reserved by the system, you should use duplicate combinations with Esc.



A single press on Esc emulates the Alt key. Double Esc Esc emulates the Ctrl key. That is, instead of Alt + X, you can type Esc X , instead of Ctrl + K, you can type Esc Esc .



The most important:



Alt + U (Esc U) - Undo, undo the last action.

Alt + E (Esc E) - Redo, repeating the undone action.



F1 (Ctlr + G) - Show Help

F2 - Exit with saving file

Ctrl + X - Return to editing from help, and other dialog boxes, exit from the program.



Copy and paste text:



Alt-A (Ctrl-6) - set / remove the marker for the start of the text selection (or just the arrows with shift)

Alt + 6 (Esc 6) - copy the selection text to the clipboard. The combination of alt + 6 is busy and did not work for me.

Ctrl + K (or F9) - cut the selection to the clipboard, if there is no selection, delete the line.

Ctrl + U (or F10) - paste a fragment from the clipboard

Ctrl + Shift + V - paste from clipboard GUI

Ins - Insert text from another file or command output.



Search and replace:



Ctrl + W - Search

Alt + W - Repeat Search

Ctrl + \ - Replace. Remembers the last search as resizable by default.



Navigation:



Alt + \ (Ctrl + Home) - To the beginning of the file

Alt + / (Ctrl + End) - At the end of the file

Ctrl + Y (PgUp) - To page up

Ctrl + V (PgDn) - To page down

Alt + S - Toggles between smooth page scrolling and fixed



Duplication of the cursor keys, then what vim is so proud of, just vstapyrku:

Ctrl + B - Left on the symbol

Ctrl + F - Right to the character

Ctrl + N - Down by character

Ctrl + P - Up by symbol



Ctrl + Space - For the word forward, you can simply Ctrl + cursor . (the cursor is at the beginning of the word)

Alt + Space ( ESC Space ) - A word back.

(I have this combination causes the quick launch line KRunner in KDE, therefore I use ESC Space )



Alt + - - Scroll up without changing cursor position

Alt + = - Scroll down without changing the cursor position

Alt + 7 (Esc 7) - To the previous block of text, or just Ctrl + cursor

Alt + 8 (Esc 8) - To the next block of text .

Alt + 0 (Esc 0) - To the next paragraph (up to the first blank line down).

Alt + 9 (Esc 9) - To the previous paragraph (up to the first blank line up).



Alt + G - Go to the line with the specified number.

Alt + < - Go to the previous open file (If nano is running with multiple files)

Alt +> - Go to the next open file.

They also write about the Alt + F command, which doesn’t include such an opportunity, it’s not possible for some reason to have separate clipboards for each file, but I don’t understand how it works.

Text Editing:



Ctrl + D (Del) - erase the character under the cursor

Ctrl + H (BSp) - Erase character to the left of the cursor

Ctrl + M (Enter) - Insert a blank line.

Ctrl + I (Tab) - Insert Tab.

Alt + Shift + { - Decrease paragraph indent by removing tabs or spaces Alt + Shift +} - Increase paragraph indent by inserting tabs. If you have already indented spaces, add a tab to it and highlight it in red.

Ctrl +] - Autocompletion of words, such as commands in the program. Programmers will appreciate!

Alt + 3 (Esc 3) - For (races) to comment with the symbol # the current line, the highlighted paragraph.

Alt + J (F4) - Justify, Align Current Paragraph



Miscellanea:



Esc Esc 220 - Entering characters by decimal code. For example, 220 - enter Ăś with umlaut.

Alt + V - Enter an ASCII sequence of a combination or key.

Alt + T - Trunkate. Erases everything from the cursor to the end of the file.

Ctrl + C ( F11 ) - Singlely displays information on the current line, column, character number.

Alt + D - Count the number of words, lines, characters in the file

Ctrl + L - Refresh Screen



Nano settings:



Alt + Shift + 4 - Allow + disallow Alt + K - soft transfer of long lines Allows Ctrl + K to switch the action between deleting the entire line by default and moving from the cursor to the end of the line.

Alt + Q - Switch from tabs to entering spaces.

Alt + L - Enable and disable hard line breaks. Alt + C - Toggles the constant output of information on the line by Ctrl + C

Alt + X - Show \ hide prompt line at the bottom of the screen.

Alt + Shift + 3 - Show line numbers.

Alt + P - Show spaces with "·"

Alt + Y - Show syntax highlighting.

Alt + H (Esc H) - Enable the smart Home key (translates the cursor not to the 1st column, but to the 1st character of the line)

Alt + I (Esc I) - Enable auto-indent (new line feed with the same indent as the previous one)

Alt + B - Switch to backups and backups.

Alt + M - Enable "mouse support". better not to use nonsense!



It is said that nano by default did not support undo. I support everything in Manjaro, but just in case the solution is to run nano with the -u key: nano -u filename.

To enable syntax highlighting in nano, uncomment the include directives in the / etc / nanorc file.



That's all.



Other links used:



Times | two | three






Vim, some crypts





"Vi has two modes: beeping and spoiling everything"

© Holivarnenko

Initially, Vim was developed for the hardware terminal ADM-3A , it is on the left of the photo .



I will cite a few quotations before the article, which seem to hint to the reader: "Terror and flee. Do you hear mortal? Run! And don’t say later that you were not warned ..."





Advanced

A text editor is a nightmare for a random user. If his

manages to finally close then you sigh with relief and firm

confidence that you will not run this program anymore

of life. Perhaps Vim can not just start and start working -

requires some training. But it is worth to try it in practice and

You are amazed how you could do without it.

© https://en.wikibooks.org/wiki/Vim

* * *

Well, look, the person starts vim, crawls to the desired cursor

places, erases unnecessary, starts typing, at some point comes to

the letters 'i' / 'c', etc., after which the remainder of the entered appears on the screen

not where the person wanted. He presses esc, q, he begins

some kind of garbage, he presses esc, esc, several times, then writes: q,

then presses Esc three times, writes Q!, then presses Esc a couple more times,

writes: q !, then vim again and edit already. (q) An example of hell with vim



* * *

By the way, I had the opposite situation when I approached an employee

to prompt what to correct in a config and in the Farovsky editor printed

he had half a line of letters j before he realized that something was wrong, the cursor

moves to the right, not down, and some garbage appears.

(q) Tale of Habit Power



* * *

Judging by what I read now, users ... no, rather “activists”

vi - not just assholes, and selected masochists. I don't know, maybe someone in

this article sees the benefits of vi, but I see the killer hemorrhoids,

re-complicate simple code entry to the level of DNA sequencing. ( d ) +15 votes in habrokommentah



* * *

Now the funds are divided into editors (type text) and IDE (work

with the project). Wim is obtained in the middle, like a duck. But this is no longer necessary.

If I want to type the text - I do not need an editor to learn

longer than IDE, i need ee / nano. And if I need to work with the project -

I need a normal IDE with great features and a low threshold

occurrences, but not the school of the white ninja for the development of the clever text

the editor.

( q ) killer habrocom







Above is an old IBM PC AT original keyboard. Take a closer look.

Vim was developed back in the days when the Ctrl key was in place of CapsLock, where it is more convenient to press with your little finger. How often do you use this key? And the Ctrl key? But IBM decided otherwise, and it all came together as it did.



However, we will try!



VIM is a blind, ten-finger text editing method. (th) youtube





It should be remembered that Vim has two modes: command and modal. In the first one, commands are entered, for example, simply by entering a colon : you can use the up and down arrows to sort through the history of previously entered commands. Having entered the beginning of the command, by Ctrl + d you can see a list of similar commands, and by Tab add the abbreviation to the full name of the command. All commands starting with : must end with ENTER , the rest are executed instantly. In the second mode, the text is corrected. Enter modal mode by a , i . After editing a piece of text, always press Esc to return to command mode, and you will never have a question about the mode in which the editor is now.



Explanatory quote about entering commands: Commands in vim are conceived combinable: ' d ' means "delete", ' e ' means

"Move the cursor to the end of the word", so the ' de ' is a complete command,

meaning "delete everything from the current position of the cursor to the next

the end of the word "Teams are case-sensitive !



For example, entering the modal mode (editing the text) by i , typing "True", and going into the command mode by Esc ,

you can navigate the file with all the navigation commands and press' . '

wherever you need to repeat the command, that is, insert the word "True" in

current cursor position in this case. It takes into account all entered

fragment with cursor movement and erasing characters. So you can

point to automate the input of forms in the same blocks.



Vim commands:







Perhaps it was only for me a sudden discovery ,

but all the help in Russian on the vim keys is available inside

terminal in the form of lessons on the team (including on it I did this

abstract):
  vimtutor ru 




Move :



hjkl - duplication of the cursor keys to the left, down, up, right.

^ or 0 (zero) - To the beginning of the line

$ - At the end of the line

w - Go to the beginning of the next word. e - Jump to the end of the current, then the next, word. E (Shift + e) —As e only without regard to punctuation. % - Move in line between brackets: () {} []

Entering a number before the move operator will cause it to repeat a specified number of times.

2w - Move the cursor forward to the beginning of the second word.

3e - Move the cursor forward to the end of the third word.



Combining commands makes navigation easier: j $ - Down to the end of the next line.



Alt + { ( Alt + Shift + [ ) - Move to the empty line before the current paragraph.

Alt +} ( Altl + Shift +] ) - Move to the empty line after the current paragraph.



Shift + g - Go to end of file. If you enter a line number before this, then go to it.

507 Shift + g - For example, here’s the transition not to the end, but to line number 507

: 45 Enter or 45gg - Go to line number 45

Instead of k, there can be any small letter for local labels, and a large one for global ones, which can be accessed from another file. mk - Mark current position as k

'k - Go to the previously created label k

'. - (backquote, dot) go to the automatic label at the beginning of the last edited line.



H , M , L - move the cursor to the top, middle and bottom line of the screen.

zt , zz , zb - move the cursor along with the text to the top, middle and bottom line of the screen.

* - Move the cursor to the next word, the same as under the cursor

# - Move the cursor to the PREVIOUS word, the same as under the cursor



Window navigation - windows and tabs :



Each open file has its own editing buffer, which the user operates on. With several open files, you can switch between them, that is, between their buffers:



: bn - open the next buffer in the current window

: bp - previous

: ls - view the list of open buffers

: b buffer_name - switch to the open file buffer, Tab completion works.

: bd buffer_name - delete buffer by name : bd - delete the current buffer, or exit vim if this buffer is the only window.



Breaking the window into several subwindows and navigating between them:

Ctrl + wv - ( : vsplit ) split window vertically

Ctrl + ws - ( : split ) split window horizontally

Ctrl + wf - split the window and in the new open the file, the name and path to which is under the cursor

Ctrl + wq or c - close the current window

Ctrl + wn - create a new window (: new filename)



trl + w o - maximize the current window to full screen, closing the other sub-windows.

trl + w _ - (underscore) maximize the window

trl + w = - Make all windows of the current tab of the same size

Ctrl + w + or Ctrl + w - - increase or decrease the size of the current window

Esc 5 Ctrl + w + or Ctrl + w 5+ - Increase the size of the current window by 5 rows / columns at once. Esc 5 Ctrl + w - or trl + w 5- - Reduce the size of the current window by 5 rows / columns at once.



Ctrl + w Ctrl + w - go to the next pane

Ctrl + w HJKL - move the window left / right / up / down, plus rearrange the window layout Ctrl + w hjkl or cursor - move the cursor to the window left / right / up / down



Vim startup options:

vim -o filename1 filename2 - run vim with two files open in horizontal windows

vim -O filename1 filename2 - run vim with two files open in vertical windows

vim -d filename1 filename2 - run vim with two files and compare them, highlighting the differences.



In addition to windows, there is another option to expand open files into separate tabs (Tabs)

: tab ball - Put all open files in tabs

: tabnew - Create a new tab, so as not to beat the current window on a large number of small subwindows

gt - ( g o to t ab) Go to next tab

gT - ( g o to t ab) Go to previous tab

: tabn - Next tab

: tabp - Previous Tab

Ctrl + PageUp and Ctrl + PageDown are standard hot keys for moving between tabs, but they can be used by a terminal emulator.



: set mouse = a - If enabled, it will be possible with the mouse to select tabs, windows, and resize them.



Entering the modal text editing mode :



i - Insert, go to the text insertion mode to the left of the cursor

a - Append, switch to the text insertion mode to the right of the cursor

A - App_end, switch to text insertion mode at the end of a line.

R - Replace, switch to text replacement mode.

o - create an empty line under the current one and go to its editing.

O - create an empty line OVER the current one and go to its editing.



Editing :



u - UNDO, undo last action

U - UNDO, undoing changes only in the last line

Ctrl + r - Redo, redo the undone action of the current modal editing session.



Many text-modifying commands consist of an operator and an object . For example, the delete operator d below is combined with the object of the removal direction:

dw - ( d elete w ord) delete from the cursor to the end of the word, including the subsequent space.

de - delete from the cursor to the end of the word, NOT including the subsequent space.

d $ - delete from the cursor to the end of the line.

d ^ - delete from the cursor to the beginning of the line.

ce - removes the word and translates into insert mode.

c $ - delete from the cursor to the end of the line and go to insert mode (text editing).



Entering a number in front of the operator will cause it to repeat a specified number of times.

d2w - Remove two words in a row

x - Del, erase the character under the cursor.

dd - delete line

D (Shift + d) - Buffer the contents from the cursor to the end of the line.

C (Shift + c) - Buffer the contents from the cursor to the end of the line and switch to edit mode.



r - replace the character under the cursor following r

After pressing r +, the character returns to command mode, which makes it inconvenient

use to edit the Russian text without adapting the command mode

vim to Russian keyboard layout.



diw - ( d elete i nner w ord) delete the word under the cursor

ciw - ( c hange i nner w ord) replace the word under the cursor (delete and switch to edit mode)

cib - ( c hange i n b rackets) Delete the expression inside the parentheses () and go to edit

c% - Cutting the expression in paired brackets from the cursor to the following brackets. (% is Shift + 5)

dit - ( d elete i n t ag) Delete the text inside the html or xml tag under the cursor

cit - ( c hange i n t ag) Replace the text inside the html or xml tag under the cursor, (delete and trans. in red.)

ce WORD Esc - enter on the first character of a word to replace it with WORD. After the point it will be possible to do this with anyone.



d $ or D (Shift + d) - deletes the line after the cursor



db - delete the word BEFORE the cursor

di ( - removes what's in parentheses.

di " - what's in quotes

ya ( - copy content inside brackets () along with the brackets themselves



s - Delete the character under the cursor and go into edit mode.

S - Delete everything to the end of the line and go into edit mode.

d5d - delete 5 lines from the cursor down



Ctrl + a - Increment by one the number nearest to the right.

Ctrl + x - Decrement the nearest number to the right.

Vu - Translate a string to lower case

VU - Translate a string to upper case

g ~~ - Invert case





Autocompletions:



Ctrl + p or Ctrl + n - autocompletion of programming language commands

Ctrl + x Ctrl + f - files, they are searched in the current directory

Ctrl + x Ctrl + d - Defines

Ctrl + x Ctrl + i - words from the current and open files

Ctrl + x Ctrl + k - from the dictionary

Ctrl + x Ctrl +] - all tags

Ctrl + x Ctrl + o - omni completion, works great with C, Python, etc.      



Search:

When the end of the file is reached, the search will continue from the beginning.



f - Search for the character following the f on the right in the current line.

; - Repeat search on f, the next match found in the current line

/ - Search text by pattern forward

? - Search for text on the pattern back

n - Next match found

N - Previous match found

Ctrl + o - go back to where you started the search

Ctrl + i - go back to what was found after ctrl + o

% - Search for paired brackets: (), [] or {}



Search and replace :



: s / was / became - Search and replace only the first match found in the current line

: s / was / became / g - Search and replace all matches found in the current line

: g / string / d Delete all strings containing “string”

: v / string / d Delete all lines that do not contain „string” : 30.90 s / was / became / gc - search and replace from 30 to 90 strings along their entire length, with confirmation

:% s / was / became / gc - search and replace in the entire file with confirmation.



That is : s / - current line ,: 30.90 s / - range, :% s / - whole file.

g - in the whole line, c - with confirmation, otherwise only the first match in the line, and without confirmations.

It is possible not to write a command each time anew, it is enough to enter : a colon and use the up and down cursor to scroll through the previously entered options.

Allocation :



v - ( v isual) visual selection, selection of a text fragment for an operation on it.

Ctrl + v - selection of rectangular blocks .

gv - Re-select the last selection. d - delete the selected fragment

c - Cut the selection to the clipboard

y - ( y ank) copy selected fragment

yw - copy one word from the position of the cursor to the end of the word, with a space.

p - ( p aste) insertion of the copied fragment

vip - Select the entire current paragraph (text separated by empty lines)

vgU - select a fragment in visual mode and translate it into upper case.

vg ~ - Select a fragment in visual mode and INVERT its register.



The combinations below work for the selection. For a single line, press twice . You can change the indent length from 8 to 4 through the config, or by entering in command mode: set tabstop = 4

For some reason, do not work for comments

<(Shift +,) - Reduce the indentation of the selected.

> (Shift +.) - Increase the indent of the selected.



File operations :



: w - save changes to current file

: w file.txt - save to file.txt

: w! file.txt - save and overwrite file.txt, if it exists.

: w ~ / file.txt - save to file.txt in your home folder

: e - Reopen the current file.

: e file.txt - open file file.txt.

: ene - Create a new file.

: ene file.txt - Create a file file.txt.



To save a fragment, select it by pressing the v key and save it with the name as above. The command line will look like : '<,'> w file.txt

: r file.txt - paste the contents of file.txt at the position below the cursor

: r! ls - insert the output of the external ls command.

:! ls - execute any external command, in this case ls



Settings :



According to the mind, the settings should be written in ~ / .vimrc (there they are written without a colon!) Then you will not have to enter them again.



: set ic - IgnoreCase, ignoring case when searching or replacing

: set noic - return case-sensitive search or replace

: set hls - Hlsearch, highlight what was found

: set nohls - DO NOT highlight what was found

: set is - Incsearch, incremental search - displays partial matches when searching

: set nois - Disable incremental search, search by enter.

: set nu - number lines

: set mouse = a - Use mouse to select window, cursor position.

: syntax on - code highlighting



Miscellaneous :



Ctrl + g - Shows the status bar with the name of the open file, the current line number.

Esc or Ctrl + C or Ctrl + [ - exit to command mode. : e

Enter - Exit. It is with the dog! I myself could not get out for a long time. I decided,

that ':' is part of the sentence, not part of the combination that is needed

type and pressed a long 'q'.

: e! Enter - Quit without saving

: x and: wq - output with preservation, and the first saves only if there were changes. : w! or: wq! forced save in readonly file, 2nd version with exit.

ZQ - Instant exit without saving and confirmation.



The default settings can be written to the configuration file ~ / .vimrc

For example, without this I start to write in vim is no program on the Python:

set tabstop = 4 # Length tab

set expandtab # Replace tab spaces



Useful Tricks :



:% s #> [^ <] \ +> ## g - Clear text from HTML tags

:% s / ^ \ (. * \) \ n \ 1 $ / \ 1 / - Delete lines duplicated twice

:% s / \ s \ + $ // - Remove spaces at the end of lines, and lines from single spaces ( link )

: 1, $ g / ^ $ / d - Delete empty lines

: g / ^ $ / d - Delete empty lines



Application:



Educational maps of the location of teams in pictures. Taken from here:

http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html





Lesson 1

Lesson 2

Lesson 3

Lesson 4 Lesson 5 Lesson 6 Lesson 7



Problem solving:

A simple solution to the problem of the command mode in the Russian layout, put in the file ~ / .vimrc with the contents of one line:

set langmap = FISVUAPPRSHOLDTSCHYKYGMTSCHNYAZH;

ABCDEFGHIJKLMNOPQRSTUVWXYZ :, fiscvrssholdtschzykymmtschnya; abcdefghijklmnopqrstuvwxyz



However, I noticed the only negative - in the lower right corner no longer displayed characters in command mode.



* * *



This couple of lines saves time on digging into .vimrc

“## Edit .vimrc ##

map, v: vsp $ MYVIMRC <CR>

map, V: source $ MYVIMRC <CR>



First map on, v opening .vimrc in vertical window and the second asks VIM to recalculate the file.



* * *



On the difference, which key to go into command mode: Ctlr-C just goes into normal mode. If

any plug-ins hang up on the output from insert mode , then when you press

Ctrl-C, vim will ignore them, and when you press Esc or Ctrl- [, it will launch. ( th )



* * *



Q (shift + q) is not a command entry mode. This transition to the outdated Ex mode is supported only for compatibility. Do not use it. To return to normal mode, type „visual“.



* * *





List of used and recommended literature:

  1. Articles on habre:

    https://habrahabr.ru/hub/vim/



  2. Why, oh why, these #? @! assholes use vi? (great article! And the comments are just a bundle)

    https://habrahabr.ru/post/307084/



  3. Vim and Cyrillic: a couple of tricks

    https://habrahabr.ru/post/98393/



  4. Already a whole book was written down on this vim, I am surprised. Found on the file here:

    http://mirrors.pdp-11.ru/_books/desktop%20soft/vim/



  5. Direct link to the book:

    O'Reilly - Learning the vi and Vim Editors 7th Edition (translation) .pdf



  6. The book "Just about Vim" ("A Byte of Vim") Russian translation.

    Download in .odt or .pdf format



  7. Vimtouch Android. googleplay:

    https://github.com/momodalo/vimtouch



  8. 100 vim,

    http://scabere.livejournal.com/62886.html



  9. —

    https://habrahabr.ru/post/28572/



  10. Vim Golf — vim

    http://www.vimgolf.com/



  11. vim

    http://najomi.org/vim



  12. Vim Python

    https://habrahabr.ru/post/74128/



  13. VIM IDE Python

    https://habrahabr.ru/post/224979/



  14. Vim IDE -

    https://habrahabr.ru/post/53435/





  15. https://habrahabr.ru/post/28572



  16. Vim

    https://www.opennet.ru/tips/1714_vip_edit_text.shtml



  17. , vimtutor, vim.

    https://www.youtube.com/playlist?list=PLcjongJGYetkY4RFSVftH43F91vgzqB7U





Source: https://habr.com/ru/post/331600/



All Articles