Page 1 of 1

I had to add two lines to a file but can't save.

Posted: Sat Mar 25, 2017 2:53 pm
by Sparks135
I installed an auto shutdown board, and had to add two lines to "sudo vim.tiny /etc/rc.local" I was able to edit the file but can't figure out how to save my edit. I tried Ctrl>W to save and exit it did nothing I opened the file in Terminal. I tried Ctrl>O no luck either. Any help would be greatly appreciated. The lines I added were
gpio mode 25 out
gpio write 25 1

Re: I had to add two lines to a file but can't save.

Posted: Sat Mar 25, 2017 3:06 pm
by jahboater
In vi I use ":wq"
':' to enter command mode
'w' to write the file
'q' to exit vi.

Re: I had to add two lines to a file but can't save.

Posted: Sat Mar 25, 2017 3:12 pm
by Sparks135
jahboater wrote:In vi I use ":wq"
':' to enter command mode
'w' to write the file
'q' to exit vi.
Please more info as I am a novice and not sure of the procedure. I opened the file using sudo, and entered the lines, but whatever I try to save nothing.

Re: I had to add two lines to a file but can't save.

Posted: Sat Mar 25, 2017 3:16 pm
by jahboater
Sparks135 wrote:
jahboater wrote:In vi I use ":wq"
':' to enter command mode
'w' to write the file
'q' to exit vi.
Please more info as I am a novice and not sure of the procedure. I opened the file using sudo, and entered the lines, but whatever I try to save nothing.
Did you try typing :wq ? The letters : then w then q one after the other followed by the <return> key.
If thats difficult, then try the nano editor which works with control keys that I suspect you are used to. It also has a helpful menu at the bottom of the screen.
vi (vim) is very powerful and fast and perhaps not so easy to learn.

Re: I had to add two lines to a file but can't save.

Posted: Sat Mar 25, 2017 3:20 pm
by Sparks135
jahboater wrote:
Sparks135 wrote:
jahboater wrote:In vi I use ":wq"
':' to enter command mode
'w' to write the file
'q' to exit vi.
Please more info as I am a novice and not sure of the procedure. I opened the file using sudo, and entered the lines, but whatever I try to save nothing.
Did you try typing :wq ? The letters : then w then q one after the other. If thats difficult, then try the nano editor which works with control keys that I suspect you are used to. It also has a helpful menu at the bottom of the screen.
vi (vim) is very powerful and fast and perhaps not so easy to learn.
Tried :wq all it did was type that on the screen. How do I open the file in the Nano editor?

Re: I had to add two lines to a file but can't save.

Posted: Sat Mar 25, 2017 3:22 pm
by jahboater
Sparks135 wrote: Tried :wq all it did was type that on the screen. How do I open the file in the Nano editor?
Sorry, you have to press the "return" key after the q. Its just a normal command line.

Re: I had to add two lines to a file but can't save.

Posted: Sat Mar 25, 2017 3:26 pm
by blaablaaguy
Sparks135 wrote:
jahboater wrote:
Sparks135 wrote: Please more info as I am a novice and not sure of the procedure. I opened the file using sudo, and entered the lines, but whatever I try to save nothing.
Did you try typing :wq ? The letters : then w then q one after the other. If thats difficult, then try the nano editor which works with control keys that I suspect you are used to. It also has a helpful menu at the bottom of the screen.
vi (vim) is very powerful and fast and perhaps not so easy to learn.
Tried :wq all it did was type that on the screen. How do I open the file in the Nano editor?
To use nano just type "nano" (without quotes) and then the file name.

Re: I had to add two lines to a file but can't save.

Posted: Sat Mar 25, 2017 3:29 pm
by klricks
Sparks135 wrote:
jahboater wrote:In vi I use ":wq"
':' to enter command mode
'w' to write the file
'q' to exit vi.
Please more info as I am a novice and not sure of the procedure. I opened the file using sudo, and entered the lines, but whatever I try to save nothing.
I would suggest to use nano editor instead.

Code: Select all

sudo nano /etc/rc.local
After edits use CTRL-x then Y to confirm save

Re: I had to add two lines to a file but can't save.

Posted: Sat Mar 25, 2017 3:33 pm
by stderr
Sparks135 wrote:Tried :wq all it did was type that on the screen.
That's because he forgot to tell you to hit the escape key first. In vi, you hit the escape key constantly. The only thing it doesn't do is let you escape. You start nano by typing nano.

Re: I had to add two lines to a file but can't save.

Posted: Sat Mar 25, 2017 4:07 pm
by Sparks135
So the ESC key is the missing link.

Re: I had to add two lines to a file but can't save.

Posted: Sat Mar 25, 2017 4:19 pm
by jahboater
Sparks135 wrote:So the ESC key is the missing link.
Yes, sorry, I had assumed you had done that. Vim is called bi-modal in that you are either in "insert" mode or "command" mode. ESC gets you out of insert mode.

Its very clever, you could for example type i xxxxx esc to insert a few letter x's. Then typing . does the entire insert again, and 5 . does that insert five times. "5 d w" deletes the next five words and so on.

With practice you can do much more work with fewer key presses in vi than you can in other, simpler, editors. You just have to think more!

Its even more confusing because : gets you into a command line mode like the old "ex" or "ed" editors where you can do even more complex, global, edits.

As I and others said though, nano is easier get started with.

Re: I had to add two lines to a file but can't save.

Posted: Sat Mar 25, 2017 4:35 pm
by Ferdinand
Hi Sparks135,

Here is a links to a vi manual https://www.cs.colostate.edu/helpdocs/vi.html

Re: I had to add two lines to a file but can't save.

Posted: Sat Mar 25, 2017 4:40 pm
by jahboater
Vi is short for "visual" and vim is short for "vi improved", a later version. vi has been around since the 70's.

Re: I had to add two lines to a file but can't save.

Posted: Sat Mar 25, 2017 4:55 pm
by DougieLawson
jahboater wrote:Vi is short for "visual" and vim is short for "vi improved", a later version. vi has been around since the 70's.
In just the same way that nano is a clone of the ancient pico editor (which I dislike intensely). Vi is a bit quirky. You need to learn the basics [esc][:][w][q] and [esc][:][q][!]. Vim also comes with an extensive help system.

At least nobody suggested emacs.

If you can get a terminal window open, there's always leafpad and sudo leafpad which gives you a GUI editor (running sudo leafpad runs it with root privileges, so you can edit/update/destroy any file with it).

Re: I had to add two lines to a file but can't save.

Posted: Sat Mar 25, 2017 5:46 pm
by jahboater
DougieLawson wrote: At least nobody suggested emacs.
Aha, now you mention it ....
I just tried it on a Pi3 and while it took ages to install, it actually runs really well.
The grand daddy of editors, more of an IDE or way-of-life than a text editor. It does anything and everything and is very easy to extend or customize if you are happy programming in Lisp! Emacs is short for "editor macros"

Re: I had to add two lines to a file but can't save.

Posted: Sat Mar 25, 2017 6:01 pm
by Martin Frezman
The best way to save-and-exit in vi (all versions) is ZZ.

I learned that long before I learned :wq (and things like that).