how to open a text file with an editor on append mode - linux

If I do this:
touch newfile.txt
sudo chattr +a newfile.txt
I can then add content to that file using:
echo "Hello world" >> newfile.txt
Is there away I can do the above with a text editor such as vim, gedit, etc?
Note that opening the this file the normal way with text editors will fail when trying to save.

You can simply open a plain vim session and then save your text with
:w >> newfile.txt

Related

Automate a Vim script to write text to a file

I am writing a script that runs on the command line and I want to be able to automatically append some static text to a file using Vim.
This is a simplified version of what I have:
insert_text() {
vim -s ./text.txt new.txt;
}
Then it would run on the command line by typing insert_text. Inside text.txt I have tried things such as:
iSome Text:wq
This puts me in insert mode and writes the text but I don't know how to leave insert mode in this way. The :wq never works and is instead written to the new.txt.
If you just want to append text to a file you don't need any special tools. For instance you can just use:
echo "Some text" >> myfile.txt
or if you have a large block of text
>>myfile.txt cat <<EOF
some
long
block of text
EOF
If you want it at the start of a file you can use a temporary file to do this. For instance:
echo "Some Text" | cat - myfile.txt > /tmp/file && mv /tmp/file myfile.txt
This will append someText at the beginning of line 1:
vim -c "1 s/^/someText" -c "wq" test.txt
When you run a command from the terminal and you want Vim to exit after that, instead of :wq do +wq. For example:
vim +'SomeCommand' +qa

Linux vi File Contents Modifying

Suppose I have these in a file called test:
u001:x:comp111:mark_henry
u002:x:comp321:zack_mike
u003:x:comp132:chris_white
And I want to open that file go to the line that has chris_white and change it to chris_brown so it becomes u003:x:comp132:chris_brown. I'm thinking to use the sed command to do so but I'm not sure how.
Using sed, below method can replace all occurrences of chris_white to chris_brown without opening the file test.
sed -i -e 's/chris_white/chris_brown/g' test
If you want to open the file test in vi editor and replace, then follow the below steps,
1) vi test
2) Type :%s/chris_white/chris_brown/g
3) Press Enter
This will replace all occurrences of chris_white to chris_brown in your file test.
With vi you need more effort than with the basic ed.
echo "s/chris_white/chris_brown/
w
q" | ed -s test
You can use printf for writing the lines as parameters:
printf "%s\n" "s/chris_white/chris_brown/" w q | ed -s test

How to replace a string at the end of a line in a text file with another string in UNIX?

I have to replace in a text file the string ".htm" with ".html" if it is placed at the end of line. I should use sed but I don't get how to use it. I tried using grep instead but didn't work.
Use this:
sed 's/\.htm$/.html/' file
It looks for .htm (the dot has to be escaped) whenever it is followed with end of line ($). In that case, it replaces it with .html.
If you want to do an in place edit, add the -i option:
sed -i.bak 's/\.htm$/.html/' file
This will create a backup file.bak while the original will be modified with the new data.
Example
$ cat a
hello this is.htm
hello this is.htm blabla
hello this ishtm
hi!
$ sed 's/\.htm$/.html/' a
hello this is.html
hello this is.htm blabla
hello this ishtm
hi!
or do the following :
1) Open file in vi editor : vi filename
2) go to command mode : press :
3) type the following command and press enter: 1,$s/.htm$/.html/
As suggested in other posts, the same results can be achieved using sed command, but in that case you cannnot make changes in the existing file. Instaed you will have to create a new file. Using the solution above, you can make changes in the existing file.
Hope that helps!!
sed -e 's/\.htm$/\.html/' < foo
assuming foo is the file that you want to operate. output will we standard out, redirect it somewhere useful.

Execute a command within Vim from the command line

Is there a way to execute a Vim command on a file from the command line?
I know the opposite is true like this:
:!python %
But what if I wanted to :retab a file without opening it in Vim? For example:
> vim myfile.c
:retab | wq
This will open myfile.c, replace the tabs with spaces, and then save and close. I'd like to chain this sequence together to a single command somehow.
It would be something like this:
> vim myfile.c retab | wq
This works:
gvim -c "set et|retab|wq" foo.txt
set et (= set expandtab) ensures the tab characters get replaced with the correct number of spaces (otherwise, retab won't work).
I don't normally use it, but vim -c ... also works
The solution as given above presumes the default tab stop of eight is appropriate. If, say, a tab stop of four is intended, use the command sequence "set ts=4|set et|retab|wq".
You have several options:
-c "commands" : will play Ex commands as you entered them in the command line.
In your example : vim myfile -c 'retab | wq'. This is what Firstrock suggested.
-S "vim source file" : will source given vim script
(like running vim -c "source 'vim source file'"):
If you have a file script.vim containing:
retab
wq
Then you can use vim myfile.c -s script.vim (the extension does not really matter)
-s "scriptin file": will play contents of file as it contains normal mode commands: If you have script.txt containing:
:retab
ZZ
with end of lines consisting of a single ^M character (for example you saved the script using the :set fileformat=mac | w), then you can run: vim myfile.c -S script.txt (ZZ is another way to exit vim and save current file).
Note that you can record those scripts with vim my_file -W script.txt, but it suffers a bug if you happen to use gvim (the GUI).
Not a direct answer to your question, but if you want to replace tabs with spaces (or do any other regex search/replace) for a list of files, you can just use in-place sed search/replace:
sed -i 's/\t/ /g' foo1.txt foo2.txt
or
ls *.txt | xargs sed -i 's/\t/ /g'
(In this example I am replacing each tab character with three spaces.)
NOTE: the -i flag means operate in-place.
From the sed man page:
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if extension
supplied)

How can i rename a filename from vim?

vim .
Now I get the list of directory and files.
Now how can I rename a filename from that list of files?
In command mode:
:E opens up the directory view.
R renames the selected file. The shortcuts are listed above the listing.
If you use vim . you can rename with R (because it is the very same thing as above).
You can use qmv (on debian-like systems apt-get install renameutils) which does exactly that and it honours your system default editor (VISUAL, EDITOR, execvp("editor"))
qmv *.cs
opens up an editor, you can %s///g what you like, use C-a / C-x to increment/decrement numbers - in short everything you ever wanted to. You can also rename in circular fashion, e.g.
a.txt b.txt
b.txt a.txt
or
a.txt b.txt
b.txt c.txt
c.txt a.txt
etc.
You can use the external mv command like this:
:! mv oldfile newfile

Resources