How to edit a text file in my terminal - linux

I'm using Linux mint and using the vi command to create text files, now that I created a text file and saved it. How do I get back into to edit the text file again?
vi helloWorld.txt

Try this command:
sudo gedit helloWorld.txt
it, will open up a text editor to edit your file.
OR
sudo nano helloWorld.txt
Here, you can edit your file in the terminal window.

Open the file again using vi. and then press " i " or press insert key ,
For save and quit
Enter Esc
and write the following command
:wq
without save and quit
:q!

Open the file again using vi. and then press the insert button to begin editing it.

If you are still inside the vi editor, you might be in a different mode from the one you want. Hit ESC a couple of times (until it rings or flashes) and then "i" to enter INSERT mode or "a" to enter APPEND mode (they are the same, just start before or after current character).
If you are back at the command prompt, make sure you can locate the file, then navigate to that directory and perform the mentioned "vi helloWorld.txt". Once you are in the editor, you'll need to check the vi reference to know how to perform the editions you want (you may want to google "vi reference" or "vi cheat sheet").
Once the edition is done, hit ESC again, then type :wq to save your work or :q! to quit without saving.
For quick reference, here you have a text-based cheat sheet.

You can open the file again using vi helloworld.txt and then use cat /path/your_file to view it.

you doesn't have root access type this
sudo su
If you want to use nano editor to create file in same directory where you headed
nano filename.format
eg:- nano attendaence.txt
Ctrl+o
Enter
Ctrl+x
If you want to use nano editor to create file in other directory where you headed
nano location/filename.txt
eg1:- nano home/ec2-user/public_html/attendence.php
eg2:- nano /home/ec2-user/public_html/attendence.php
2. Ctrl+o
3. Enter
4. Ctrl+x
Create file without editor in same directory
touch filename.format
eg:- touch helloworld.html
Create file without editor in another directory
touch location/filename.format
eg:- touch var/www/index.html

Related

How to save files in linux centos

when i'm edit a file using
sudo -e [name of file] and then i want to save and close, how to do that?
For example, when it's nano..i press ctrl key and x to save and close.
please, help me
The best tools to edit files on Centos are vi and vim. Open the desired file with one of those, press i to edit, press ESC to exit edit mode and press ZZ (capital Zs) to save.
Hope this helps.

editing file through vi editor in shell script show error

script in which I open a file in vi editor , after that user will write any thing in it then save or quite by :wq!
When I try this then it give an error message as below
e138 can't write viminfo file $home/.viminfo!
and ask for,
Press Enter or command to continue
Now when I press enter then file save. But why that error message shows.
If I open same file direct with vi editor or vim editor and do changes then it does not give any error.
also if I install vim editor then it works fine and no error shows
Please tell me what may be the issue.
my best guess is that $home is not defined properly. try running: !echo $home on both editors. If it's not defined during the script run, define it before running vim.
I came here with mostly the same error, but this answer did not work for me. I had to create more space within my $HOME directory by deleting a few files, and then vim worked normally again.

text editor mode for calling bash command

suppose I want to enter a multiline command via bash I know that I can append \ in the end of the line to enter a new line
however is it possible to enter a legitimate "text editor mode" where you don't even have to enter \ and simply press enter would suffice
eg..you type in the command into the command line then before entering the parameters you press some magic button which allows you to enter a vi like mode then you enter stuff into the "vi mode" then you exit and then the text you entered in the "vi mode" turns into the parameters of the command then you press enter then the command executes
is it possible to do that in bash command line? if so, how do I do it?
See man bash:
edit-and-execute-command (C-xC-e)
Invoke an editor on the current command line, and execute the
result as shell commands. Bash attempts to invoke $VISUAL,
$EDITOR, and emacs as the editor, in that order.
Per default bash is configured for emacs mode, hence the emacs like C-xC-e command.
If you really like vi you can also set your bash into vi mode: set -o vi. This allows you to do normal line editing the vi way without invoking an explicit editor.
Bash can emulate vim mode (though not very well) with:
set -o vi
You can edit the previous command in vi or your default editor by using the fc command. This pops open an editor window and when you exit it executes the edited command. That mode could bed used repeatedly to edit a complex command.

Can not edit cronjobs file in Debian with crontab -e

I have had several Debian servers and always edited cronjobs in this way:
crontab -e
and
Ctrl+x
Just got a new server and can not do it in this way anymore.
When I enter crontab -e, the file opens but I can't write anything. I can move cursor up and down but can't write. I even can not exit from this file because Ctr+x doesn't work.
When I open a file there is some information and the rest empty lines contain tildes ~ in the beginning of each line.
Any ideas how can I edit this file?
Thanks.
You need to turn on insert mode. After entering crontab -e, press i to turn on insert, enter your full line, press esc to finish entering, and then hold down shift and press z twice to save the file. This is how I managed to do it in vi/vim
As one other person has suggested, vim is obviously the default editor on your new server. You can test this by running
EDITOR=pico crontab -e
Substituting whatever is your actual preferred editor (sounds like it may be nano or pico). If that works, you should try one of the following:
edit your login script to set that environment variable on login (sets the editor just for that user)
Make sure your favourite editor is is installed and run the following (as root): update-alternatives --config sensible-editor
You can then choose the default editor for all users (they can override it individually by doing option 1).
~ would suggest that you are now editing your crontab using vi/vim instead of your usual editor
so Ctrl-X wont work, try Esc :wq
Do you have the right permissions? maybe you should open it as root user if not.
check it doing this:
ls -all $(which crontab)
if not you can change them..take a look here too..maybe you find something more!
hope it helps.

adding a shell script to a configuration file

I'm pretty new to shell scripting and linux in general. Basically, I need to change the configuration file for logging out so that when a user logs out, a certain shell script is run.
Now, I've located the logout configuration file and opened it with vi using this command
$ vi ~/.bash_logout
At this point, I'm experiencing some very weird behavior. When I try to type a character, the cursor jumps around seemingly erratically. What could this be due to? I'm running the latest version of ubuntu.
And once I get that figured out, what's the command to run a .sh file from within this configuration file?
If you're having trouble with vi, try using nano instead. nano .bash_logout
If you do need to use vi for some reason, "i" will put the editor into insert mode, and ESC will take it out of insert mode when you're done. ":wq" will write and quit the editor.
To run a command, just put it in the .bash_logout file as you would type it on the commandline.
Some other useful commands:
a insert after selected character
o insert at next line
O insert at previous line
r replace a single character
R replace mode
:q! quit without saving
:w save
:wq save and quit
To get familiar with Vi and its brother Vim ("VI improved") I recommend the book "A Byte of Vim", you can read it online or download for free at http://www.swaroopch.com/notes/Vim
You can permanently change your editor option. To find out what your current one is, type this:
export | grep -i edit
To change it on Ubuntu:
sudo update-alternatives –config editor
On any other BASH prompt, just do this:
export EDITOR="nano"
Replace 'nano' with 'vi', 'emacs', or any other preferred editor. You can also add this to your .bashrc by typing the following:
echo 'EDITOR="nano"' >> ~/.bashrc

Resources